view MoinMoin/action/ @ 2966:ba14d391c2ba

Refactor all modules to use the new add_msg interface in 1.7 (done by Frederico Lorenzi). Should not be backported to 1.6 but instead the separate patch from should be used.
author Alexander Schremmer <alex AT alexanderweb DOT de>
date Sun, 23 Dec 2007 23:52:43 +0100
parents bb2e053067fb
children 948aac85d4d3
line wrap: on
line source

    MoinMoin - newpage action

    Create a new page with optional template. Can be used with macro.

    @copyright: 2004 Vito Miliano (,
                2004 Nir Soffer <>,
                2004 Alexander Schremmer <alex AT alexanderweb DOT de>
    @license: GNU GPL, see COPYING for details.

import time
from MoinMoin.Page import Page

class NewPage:
    """ Open editor for a new page, using template """

    def __init__(self, request, referrer):
        self.request = request
        self.referrer = referrer # The page the user came from
        self.pagename = self.request.form.get('pagename', [None])[0]
        self.nametemplate = self.request.form.get('nametemplate', ['%s'])[0]
        self.nametemplate = self.nametemplate.replace('\x00', '')

    def checkAndCombineArguments(self):
        """ Check arguments in form, return error msg

        @rtype: unicode
        @return: error message
        _ = self.request.getText
        need_replace = '%s' in self.nametemplate
        if not self.pagename and need_replace:
            return _("Cannot create a new page without a page name."
                     "  Please specify a page name.")
        if need_replace:
        # generate a string that can be safely used as the pagename
        # template variable
            repl = 'A@'
            i = 0
            while repl in self.nametemplate:
                repl += ['#', '&', '$', 'x', 'X', ':', '@'][i]
                i += 1
                i = i % 7
            template = self.nametemplate.replace('%s', repl)
            template = self.nametemplate
        template = template.encode('utf-8')
        template = time.strftime(template, self.request.user.getTime(time.time()))
        template = template.decode('utf-8')
        if need_replace:
            self.pagename = template.replace(repl, self.pagename)
            self.pagename = template
        return ''

    def checkPermissions(self):
        """ Check write permission in form, return error msg

        @rtype: unicode
        @return: error message
        _ = self.request.getText
        page = Page(self.request, self.pagename)
        if not (page.isWritable() and
            # Same error as the edit page for localization reasons
            return _('You are not allowed to edit this page.')
        return ''

    def render(self):
        """ Redirect to the new page, using edit action and template """
        error = self.checkAndCombineArguments() or self.checkPermissions()
        if error:
            # Send back to the page you came from, with an error msg
            page = Page(self.request, self.referrer)
            request.theme.add_msg(error, "error")
            # Redirect to new page using edit action. No error checking
            # is needed because it is done later in new request.
            pagename = self.pagename
            query = {'action': 'edit', 'backto': self.referrer}

            template = self.request.form.get('template', [''])[0]
            if template:
                from MoinMoin.wikiutil import quoteWikinameURL
                query['template'] = quoteWikinameURL(template)

            parent = self.request.form.get('parent', [''])[0]
            if parent:
                pagename = "%s/%s" % (parent, pagename)

            url = Page(self.request, pagename).url(self.request, query, relative=False)

        return ''

def execute(pagename, request):
    """ Temporary glue code for current moin action system """
    return NewPage(request, pagename).render()