Back to index

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