Source

PylonsWikiNG / src / views / tutorial / handlers.py

import re

from docutils.core import publish_parts

from webob.exc import HTTPFound

from pylons.views import action
from pylons.url import route_url

from tutorial.models import DBSession
from tutorial.models import Page

# regular expression used to find WikiWords
wikiwords = re.compile(r"\b([A-Z]\w+[A-Z]+\w+)")

class WikiHandler(object):
    def __init__(self, request):
        self.request = request

    def __call__(self):
        return HTTPFound(
            location=route_url('wiki_action',
                               self.request,
                               pagename='FrontPage',
                               action='view')
            )
    
    @action(renderer='view.mak', permission='view')
    def view(self):
        matchdict = self.request.matchdict
        session = DBSession()
        page = session.query(Page).filter_by(name=matchdict['pagename']).one()
        def check(match):
            word = match.group(1)
            exists = session.query(Page).filter_by(name=word).all()
            if exists:
                view_url = route_url('wiki_action',
                                     self.request,
                                     pagename=word,
                                     action='view')
                return '<a href="%s">%s</a>' % (view_url, word)
            else:
                add_url = route_url('wiki_action',
                                    self.request,
                                    pagename=word,
                                    action='add')
                return '<a href="%s">%s</a>' % (add_url, word)
    
        content = publish_parts(page.data, writer_name='html')['html_body']
        content = wikiwords.sub(check, content)
        edit_url = route_url('wiki_action',
                             self.request,
                             pagename=matchdict['pagename'],
                             action='edit')
        return dict(
            page=page,
            content=content,
            edit_url=edit_url
        )
    
    @action(renderer='edit.mak', permission="edit")
    def add(self):
        name = self.request.matchdict['pagename']
        if 'form.submitted' in self.request.params:
            session = DBSession()
            body = self.request.params['body']
            page = Page(name, body)
            session.add(page)
            return HTTPFound(
                location = route_url(
                    'wiki_action',
                    self.request,
                    pagename=name,
                    action='view')
                )
        save_url = route_url(
            'wiki_action',
            self.request,
            pagename=name,
            action='add')
        page = Page('', '')
        return dict(
            page=page,
            save_url=save_url
        )
    
    @action(renderer='edit.mak', permission="edit")    
    def edit(self):
        name = self.request.matchdict['pagename']
        session = DBSession()
        page = session.query(Page).filter_by(name=name).one()
        if 'form.submitted' in self.request.params:
            page.data = self.request.params['body']
            session.add(page)
            return HTTPFound(
                location=route_url(
                    'wiki_action',
                    self.request,
                    pagename=name,
                    action='view')
                )
        return dict(
            page=page,
            save_url = route_url('wiki_action',
                                 self.request,
                                 pagename=name,
                                 action='edit')
        )