Back to index

moin  1.9.0~rc2
Load.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - Action to load page content from a file upload
00004 
00005     @copyright: 2007-2008 MoinMoin:ReimarBauer,
00006                 2008 MoinMoin:ThomasWaldmann
00007     @license: GNU GPL, see COPYING for details.
00008 """
00009 
00010 import os
00011 
00012 from MoinMoin import wikiutil, config
00013 from MoinMoin.action import ActionBase, AttachFile
00014 from MoinMoin.PageEditor import PageEditor
00015 from MoinMoin.Page import Page
00016 from MoinMoin.security.textcha import TextCha
00017 
00018 class Load(ActionBase):
00019     """ Load 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 = 'Load'
00028         self.form_trigger_label = _('Load')
00029         self.pagename = pagename
00030         self.method = 'POST'
00031         self.enctype = 'multipart/form-data'
00032 
00033     def do_action(self):
00034         """ Load """
00035         status = False
00036         _ = self._
00037         form = self.form
00038         request = self.request
00039         # Currently we only check TextCha for upload (this is what spammers ususally do),
00040         # but it could be extended to more/all attachment write access
00041         if not TextCha(request).check_answer_from_form():
00042             return status, _('TextCha: Wrong answer! Go back and try again...')
00043 
00044         comment = form.get('comment', u'')
00045         comment = wikiutil.clean_input(comment)
00046 
00047         file_upload = request.files.get('file')
00048         if not file_upload:
00049             # This might happen when trying to upload file names
00050             # with non-ascii characters on Safari.
00051             return False, _("No file content. Delete non ASCII characters from the file name and try again.")
00052 
00053         filename = file_upload.filename
00054         rename = form.get('rename', '').strip()
00055         if rename:
00056             target = rename
00057         else:
00058             target = filename
00059 
00060         target = wikiutil.clean_input(target)
00061 
00062         if target:
00063             filecontent = file_upload.stream.read() # XXX reads complete file into memory!
00064             filecontent = wikiutil.decodeUnknownInput(filecontent)
00065 
00066             self.pagename = target
00067             pg = PageEditor(request, self.pagename)
00068             try:
00069                 msg = pg.saveText(filecontent, 0, comment=comment)
00070                 status = True
00071             except pg.EditConflict, e:
00072                 msg = e.message
00073             except pg.SaveError, msg:
00074                 msg = unicode(msg)
00075         else:
00076             msg = _("Pagename not specified!")
00077         return status, msg
00078 
00079     def do_action_finish(self, success):
00080         if success:
00081             url = Page(self.request, self.pagename).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         return """
00089 <h2>%(headline)s</h2>
00090 <p>%(explanation)s</p>
00091 <dl>
00092 <dt>%(upload_label_file)s</dt>
00093 <dd><input type="file" name="file" size="50" value=""></dd>
00094 <dt>%(upload_label_rename)s</dt>
00095 <dd><input type="text" name="rename" size="50" value="%(pagename)s"></dd>
00096 <dt>%(upload_label_comment)s</dt>
00097 <dd><input type="text" name="comment" size="80" maxlength="200"></dd>
00098 </dl>
00099 %(textcha)s
00100 <p>
00101 <input type="hidden" name="action" value="%(action_name)s">
00102 <input type="hidden" name="do" value="upload">
00103 </p>
00104 <td class="buttons">
00105 %(buttons_html)s
00106 </td>""" % {
00107     'headline': _("Upload page content"),
00108     'explanation': _("You can upload content for the page named below. "
00109                      "If you change the page name, you can also upload content for another page. "
00110                      "If the page name is empty, we derive the page name from the file name."),
00111     'upload_label_file': _('File to load page content from'),
00112     'upload_label_comment': _('Comment'),
00113     'upload_label_rename': _('Page Name'),
00114     'pagename': self.pagename,
00115     'buttons_html': buttons_html,
00116     'action_name': self.form_trigger,
00117     'textcha': TextCha(self.request).render(),
00118 }
00119 
00120 def execute(pagename, request):
00121     """ Glue code for actions """
00122     Load(pagename, request).render()
00123