Back to index

moin  1.9.0~rc2
revert.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - revert a page to a previous revision
00004 
00005     @copyright: 2000-2004 Juergen Hermann <jh@web.de>,
00006                 2006-2008 MoinMoin:ThomasWaldmann,
00007                 2007 MoinMoin:ReimarBauer,
00008                 2008 MoinMoin:JohannesBerg
00009     @license: GNU GPL, see COPYING for details.
00010 """
00011 from MoinMoin import wikiutil
00012 from MoinMoin.Page import Page
00013 from MoinMoin.PageEditor import PageEditor
00014 from MoinMoin.action import ActionBase
00015 
00016 class revert(ActionBase):
00017     """ revert page action
00018 
00019     Note: the action name is the class name
00020     """
00021     def __init__(self, pagename, request):
00022         ActionBase.__init__(self, pagename, request)
00023         self.use_ticket = True
00024         _ = self._
00025         self.form_trigger = 'revert'
00026         self.form_trigger_label = _('Revert')
00027         self.method = 'POST'
00028 
00029     def is_allowed(self):
00030         # this is not strictly necessary because the underlying storage code checks
00031         # as well
00032         _ = self._
00033         may = self.request.user.may
00034         allowed = may.write(self.pagename) and may.revert(self.pagename)
00035         return allowed, _('You are not allowed to revert this page!')
00036 
00037     def check_condition(self):
00038         """ checks valid page and rev """
00039         _ = self._
00040         if not self.request.rev or Page(self.request, self.pagename).current_rev() == self.request.rev:
00041             # same string as in PageEditor...
00042             note = _('You were viewing the current revision of this page when you called the revert action. '
00043                      'If you want to revert to an older revision, first view that older revision and '
00044                      'then call revert to this (older) revision again.')
00045             return note
00046         else:
00047             return None
00048 
00049     def do_action(self):
00050         """ revert pagename """
00051         form = self.form
00052         comment = form.get('comment', u'')
00053         comment = wikiutil.clean_input(comment)
00054 
00055         if self.request.method != 'POST':
00056             return False, u''
00057 
00058         rev = self.request.rev
00059         pg = PageEditor(self.request, self.pagename)
00060 
00061         try:
00062             msg = pg.revertPage(rev, comment)
00063             # make it show the current version...
00064             self.request.rev = None
00065         except PageEditor.RevertError, error:
00066             msg = unicode(error)
00067         except PageEditor.Unchanged, error:
00068             msg = unicode(error)
00069 
00070         return True, msg
00071 
00072     def get_form_html(self, buttons_html):
00073         """ creates the form """
00074         _ = self._
00075 
00076         d = {
00077             'pagename': self.pagename,
00078             'comment_label': _("Optional reason for reverting this page"),
00079             'buttons_html': buttons_html,
00080             'querytext': _('Really revert this page?'),
00081             'rev': self.request.rev
00082             }
00083 
00084         return '''
00085 <strong>%(querytext)s</strong>
00086 <table>
00087     <tr>
00088         <td class="label"><label>%(comment_label)s</label></td>
00089         <td class="content">
00090             <input type="text" name="comment" size="80" maxlength="200">
00091             <input type="hidden" name="rev" value="%(rev)d">
00092         </td>
00093     </tr>
00094     <tr>
00095         <td></td>
00096         <td class="buttons">
00097             %(buttons_html)s
00098         </td>
00099     </tr>
00100 </table>
00101 ''' % d
00102 
00103 
00104 
00105 def execute(pagename, request):
00106     """ Glue code for actions """
00107     revert(pagename, request).render()