Source

crosswiki / crosswiki / views.py

from pyramid.view import view_config
from pyramid.httpexceptions import HTTPFound, HTTPNotFound
from .models import Page
from .helpers import page_url, create_page_url

@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):
    return dict()

@view_config(route_name='create_page', request_method='POST')
def create_page(request):
    page = Page(page_name=request.matchdict['page_name'],
        content=request.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))