Source

crosswiki / crosswiki / views.py

Full commit
from deform.form import Form
from pyramid.view import view_config
from pyramid.httpexceptions import HTTPFound, HTTPNotFound
from .models import Page
from .helpers import page_url, create_page_url
from . import forms


@view_config(route_name='page', renderer='page.mak')
@view_config(route_name='edit_page', renderer='edit_page.mak', request_method="GET")
def show_page(request):
    page_name = request.matchdict['page_name']
    page = request.context.get_page(page_name)
    if page is None:
        return HTTPFound(location=create_page_url(request, page_name))
    return dict(page_name=page.page_name, content=page.html_content)

@view_config(route_name='create_page', request_method='GET', renderer='create_page_form.mak')
def create_page_form(request):
    form = Form(forms.PageSchema(), buttons='create')
    return dict(form=form)

@view_config(route_name='create_page', request_method='POST', renderer='create_page.mak')
def create_page(request):
    form = Form(forms.PageSchema(), buttons='create')
    controls = request.params.items()
    try:
        params = form.validate(controls)
    except ValidationFailure, e:
        return dict(form=e)

    page = Page(page_name=request.matchdict['page_name'],
        content=params['content'])
    request.context.add(page)

    return HTTPFound(location=page_url(request, page))

@view_config(route_name='edit_page', renderer='page.mak', request_method="POST")
def update_page(request):
    page_name = request.matchdict['page_name']
    page = request.context.get_page(page_name)
    if page is None:
        raise HTTPNotFound(location=request.url)

    page.content = request.params['content']

    return HTTPFound(location=page_url(request, page))