Attachment 'DiffPage.py'

Download

   1 """
   2     MoinMoin - DiffPage action
   3 
   4     Based on the DeletePage action by Jürgen Hermann <jh@web.de>
   5 
   6     A hack of the RenamPage action that is Copyright (c) 2002 Michael Reinsch <mr@uue.org>
   7     All rights reserved, see COPYING for details.
   8 
   9 
  10     This action allows you to diff the actual page with another page
  11 
  12     $Id$
  13 """
  14 
  15 import os
  16 from MoinMoin import wikiutil
  17 from MoinMoin.util.diff import diff
  18 from MoinMoin.Page import Page
  19 
  20 class DiffPage:
  21     """ Diff page action
  22 
  23     Note: the action name is the class name
  24     """
  25     def __init__(self, pagename, request):
  26         self.request = request
  27         self.pagename = pagename
  28         self.page = Page(request, pagename)
  29         self.otherpage = None
  30         self.error = ''
  31 
  32     def allowed(self):
  33         """ Check if user is allowed to do this
  34 
  35         This could be a standard method of the base action class, doing
  36         the filtering by action class name and cfg.actions_excluded.
  37         """
  38         may = self.request.user.may
  39         return (not self.__class__.__name__ in self.request.cfg.actions_excluded and
  40                 may.read(self.pagename))
  41     
  42     def render(self):
  43         """ Render action
  44 
  45         This action return a wiki page with optional message, or
  46         redirect to other page.
  47         """
  48         _ = self.request.getText
  49         form = self.request.form
  50         
  51         if form.has_key('cancel'):
  52             # User canceled
  53             return self.page.send_page(self.request)
  54 
  55         # Validate user rights and page state. If we get error here, we
  56         # return an error message, without the Diff form.
  57         error = None
  58         if not self.allowed():
  59             error = _('You are not allowed to Diff pages in this wiki!')
  60         elif not self.page.exists():
  61             error = _('This page does not exist!')
  62         if error:
  63             # Send page with an error message
  64             return self.page.send_page(self.request, msg=error)
  65 
  66         # Try to Diff. If we get an error here, we return the error
  67         # message with a Diff form.
  68         elif (form.has_key('Diff') and form.has_key('otherpagename')):
  69             # User replied to the Diff form with all required items
  70             self.Diff()
  71             if not self.error:
  72 #                self.request.http_redirect(self.otherpage.url(self.request))
  73                 return self.request.finish()    
  74 
  75         # A other form request, or form has missing data, or Diff
  76         # failed. Return a other form with optional error.
  77         return self.page.send_page(self.request, msg=self.makeform())
  78 
  79     def Diff(self):
  80         """ Diff pagename and return the other page """
  81         _ = self.request.getText
  82         form = self.request.form
  83         
  84  
  85         # Get other name from form and normalize.
  86         otherpagename = form.get('otherpagename')[0]
  87         otherpagename = self.request.normalizePagename(otherpagename)
  88 
  89         # Name might be empty after normalization. To save translation
  90         # work for this extreme case, we just use "EmptyName".
  91         if not otherpagename:
  92             otherpagename = "EmptyName"
  93 
  94         # Valid other name
  95         otherpage = Page(self.request, otherpagename)
  96 
  97 
  98         self.request.setContentLanguage(self.request.lang)
  99 
 100         self.request.http_headers()
 101         wikiutil.send_title(self.request, _('Diff for "%s" and "%s"') % (self.pagename,otherpagename), pagename=self.pagename, allow_doubleclick=0)
 102         # this should use the formatter, but there is none?
 103         self.request.write('<div id="content">\n') # start content div
 104 #        self.request.write('<p class="diff-header">')
 105 #        self.request.write('</p>')
 106         self.request.write(diff(self.request,  self.page.get_raw_body(),otherpage.get_raw_body()))
 107         self.page.send_page(self.request, count_hit=0, content_only=1, content_id="content-below-diff")
 108         self.request.write('</div>\n') # end content div
 109         wikiutil.send_footer(self.request, self.pagename)
 110 
 111 
 112                         
 113     def makeform(self):
 114         """ Display a Diff page form
 115 
 116         The form might contain an error that happened when trying to Diff.
 117         """
 118         from MoinMoin.widget.dialog import Dialog
 119         _ = self.request.getText
 120 
 121         error = ''
 122         if self.error:
 123             error = u'<p class="error">%s</p>\n' % self.error
 124 
 125         d = {
 126             'error': error,
 127             'action': self.__class__.__name__,
 128 #            'ticket': wikiutil.createTicket(),
 129             'pagename': self.pagename,
 130             'Diff': _('Show Differences'),
 131             'cancel': _('Cancel'),
 132             'othername_label': _("Other Page"),
 133             'comment_label': _("Optional reason for the renaming"),
 134         }
 135         form = '''
 136 %(error)s
 137 <form method="post" action="">
 138 <input type="hidden" name="action" value="%(action)s">
 139 <table>
 140     <tr>
 141         <td class="label"><label>%(othername_label)s</label></td>
 142         <td class="content">
 143             <input type="text" name="otherpagename" value="%(pagename)s">
 144         </td>
 145     </tr>
 146     <tr>
 147         <td></td>
 148         <td class="buttons">
 149             <input type="submit" name="Diff" value="%(Diff)s">
 150             <input type="submit" name="cancel" value="%(cancel)s">
 151         </td>
 152     </tr>
 153 </table>
 154 </form>''' % d
 155         
 156         return Dialog(self.request, content=form)        
 157     
 158     
 159 def execute(pagename, request):
 160     """ Glue code for actions """
 161     DiffPage(pagename, request).render()
 162     

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2007-06-06 21:47:40, 5.5 KB) [[attachment:DiffPage.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.