Back to index

moin  1.9.0~rc2
RenamePage.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - RenamePage action
00004 
00005     This action allows you to rename a page.
00006 
00007     @copyright: 2002-2004 Michael Reinsch <mr@uue.org>,
00008                 2006-2007 MoinMoin:ThomasWaldmann,
00009                 2007 MoinMoin:ReimarBauer
00010     @license: GNU GPL, see COPYING for details.
00011 """
00012 import re
00013 from MoinMoin import wikiutil
00014 from MoinMoin.Page import Page
00015 from MoinMoin.PageEditor import PageEditor
00016 from MoinMoin.action import ActionBase
00017 
00018 class RenamePage(ActionBase):
00019     """ Rename page action
00020 
00021     Note: the action name is the class name
00022     """
00023     def __init__(self, pagename, request):
00024         ActionBase.__init__(self, pagename, request)
00025         self.use_ticket = True
00026         _ = self._
00027         self.form_trigger = 'rename'
00028         self.form_trigger_label = _('Rename Page')
00029         filterfn = re.compile(ur"^%s/.*$" % re.escape(pagename), re.U).match
00030         subpagenames = request.rootpage.getPageList(user='', exists=1, filter=filterfn)
00031         self.subpages = [pagename for pagename in subpagenames if self.request.user.may.delete(pagename)]
00032 
00033     def is_allowed(self):
00034         may = self.request.user.may
00035         return may.write(self.pagename) and may.delete(self.pagename)
00036 
00037     def check_condition(self):
00038         _ = self._
00039         if not self.page.exists():
00040             return _('This page is already deleted or was never created!')
00041         else:
00042             return None
00043 
00044     def do_action(self):
00045         """ Rename this page to "pagename" """
00046         _ = self._
00047         form = self.form
00048         newpagename = form.get('newpagename', u'')
00049         newpagename = wikiutil.normalize_pagename(newpagename, self.cfg)
00050         comment = form.get('comment', u'')
00051         comment = wikiutil.clean_input(comment)
00052 
00053         self.page = PageEditor(self.request, self.pagename)
00054         success, msgs = self.page.renamePage(newpagename, comment)
00055 
00056         if not success:
00057             return success, msgs
00058 
00059         rename_subpages = 0
00060         try:
00061             rename_subpages = int(form['rename_subpages'])
00062         except:
00063             pass
00064 
00065         if rename_subpages and self.subpages:
00066             for name in self.subpages:
00067                 self.page = PageEditor(self.request, name)
00068                 new_subpagename = name.replace(self.pagename, newpagename, 1)
00069                 success_i, msg = self.page.renamePage(new_subpagename, comment)
00070                 msgs = "%s %s" % (msgs, msg)
00071 
00072         self.newpagename = newpagename # keep there for finish
00073         return success, msgs
00074 
00075     def do_action_finish(self, success):
00076         if success:
00077             url = Page(self.request, self.newpagename).url(self.request)
00078             self.request.http_redirect(url, code=301)
00079         else:
00080             self.render_msg(self.make_form(), "dialog")
00081 
00082     def get_form_html(self, buttons_html):
00083         _ = self._
00084         if self.subpages:
00085             subpages = ' '.join(self.subpages)
00086             d = {
00087                 'subpage': subpages,
00088                 'subpages_checked': ('', 'checked')[self.request.args.get('subpages_checked', '0') == '1'],
00089                 'subpage_label': _('Rename all /subpages too?'),
00090                 'pagename': wikiutil.escape(self.pagename, True),
00091                 'newname_label': _("New name"),
00092                 'comment_label': _("Optional reason for the renaming"),
00093                 'buttons_html': buttons_html,
00094                 'querytext': _('Really rename this page?')
00095                 }
00096 
00097             return '''
00098 <strong>%(querytext)s</strong>
00099 <br>
00100 <br>
00101 <table>
00102     <tr>
00103     <dd>
00104         %(subpage_label)s<input type="checkbox" name="rename_subpages" value="1" %(subpages_checked)s>
00105     </dd>
00106     <dd>
00107         <class="label"><subpage> %(subpage)s</subpage>
00108     </dd>
00109     </tr>
00110 </table>
00111 <table>
00112     <tr>
00113         <td class="label"><label>%(newname_label)s</label></td>
00114         <td class="content">
00115             <input type="text" name="newpagename" value="%(pagename)s" size="80">
00116         </td>
00117     </tr>
00118     <tr>
00119         <td class="label"><label>%(comment_label)s</label></td>
00120         <td class="content">
00121             <input type="text" name="comment" size="80" maxlength="200">
00122         </td>
00123     </tr>
00124     <tr>
00125         <td></td>
00126         <td class="buttons">
00127             %(buttons_html)s
00128         </td>
00129     </tr>
00130 </table>
00131 ''' % d
00132 
00133         else:
00134             d = {
00135                 'pagename': wikiutil.escape(self.pagename, True),
00136                 'newname_label': _("New name"),
00137                 'comment_label': _("Optional reason for the renaming"),
00138                 'buttons_html': buttons_html,
00139                 }
00140             return '''
00141 <table>
00142     <tr>
00143         <td class="label"><label>%(newname_label)s</label></td>
00144         <td class="content">
00145             <input type="text" name="newpagename" value="%(pagename)s" size="80">
00146         </td>
00147     </tr>
00148     <tr>
00149         <td class="label"><label>%(comment_label)s</label></td>
00150         <td class="content">
00151             <input type="text" name="comment" size="80" maxlength="200">
00152         </td>
00153     </tr>
00154     <tr>
00155         <td></td>
00156         <td class="buttons">
00157             %(buttons_html)s
00158         </td>
00159     </tr>
00160 </table>
00161 ''' % d
00162 
00163 def execute(pagename, request):
00164     """ Glue code for actions """
00165     RenamePage(pagename, request).render()
00166