view MoinMoin/action/ @ 3366:80ecec6e008b

action.revert: keep revert action in log
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Thu, 20 Mar 2008 21:14:58 +0100
parents bb04265ce89a
children 48acb9e0a097
line wrap: on
line source

# -*- coding: iso-8859-1 -*-
    MoinMoin - revert a page to a previous revision

    @copyright: 2000-2004 Juergen Hermann <>,
                2006 MoinMoin:ThomasWaldmann,
                2007 MoinMoin:ReimarBauer,
                2008 MoinMoin:JohannesBerg
    @license: GNU GPL, see COPYING for details.
from MoinMoin import wikiutil
from MoinMoin.PageEditor import PageEditor
from MoinMoin.action import ActionBase

class revert(ActionBase):
    """ revert page action

    Note: the action name is the class name
    def __init__(self, pagename, request):
        ActionBase.__init__(self, pagename, request)
        self.use_ticket = True
        _ = self._
        self.form_trigger = 'revert'
        self.form_trigger_label = _('Revert')
        self.method = 'POST'

    def is_allowed(self):
        # this is not strictly necessary because the underlying storage code checks
        # as well
        _ = self._
        may = self.request.user.may
        allowed = may.write(self.pagename) and may.delete(self.pagename)
        return allowed, _('You are not allowed to revert this page!')

    def check_condition(self):
        _ = self._
        if not self.request.rev:
            # same string as in PageEditor...
            note = _('You were viewing the current revision of this page when you called the revert action. '
                     'If you want to revert to an older revision, first view that older revision and '
                     'then call revert to this (older) revision again.')
            return note

        if not
            return _('This page is already deleted or was never created!')
            return None

    def do_action(self):
        """ Delete pagename """
        _ = self._

        if self.request.request_method != 'POST':
            return False, u''

        form = self.form
        rev = self.request.rev
        comment = form.get('comment', [u''])[0]
        system_comment = _(u"Revert to revision %(rev)d.") % {'rev': int(rev)}
        if not comment:
            comment = system_comment
            comment = "%(comment)s (%(system_comment)s)" % {"comment": comment,
                                                            "system_comment": system_comment}

        comment = wikiutil.clean_input(comment)
        pg = PageEditor(self.request, self.pagename)

            msg = pg.revertPage(rev, comment)
            # make it show the current version...
            self.request.rev = None
        except PageEditor.RevertError, error:
            msg = unicode(error)
        except PageEditor.Unchanged, error:
            msg = unicode(error)

        return True, msg

    def get_form_html(self, buttons_html):
        _ = self._

        d = {
            'pagename': self.pagename,
            'comment_label': _("Optional reason for reverting this page"),
            'buttons_html': buttons_html,
            'querytext': _('Really revert this page?'),
            'rev': self.request.rev

        return '''
        <td class="label"><label>%(comment_label)s</label></td>
        <td class="content">
            <input type="text" name="comment" size="80" maxlength="200">
            <input type="hidden" name="rev" value="%(rev)d">
        <td class="buttons">
''' % d

def execute(pagename, request):
    """ Glue code for actions """
    revert(pagename, request).render()