~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/action/Load.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mfrom: (0.9.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080622211713-fpo2zrq3s5dfecxg
Tags: 1.7.0-3
Simplify /etc/moin/wikilist format: "USER URL" (drop unneeded middle
CONFIG_DIR that was wrongly advertised as DATA_DIR).  Make
moin-mass-migrate handle both formats and warn about deprecation of
the old one.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: iso-8859-1 -*-
 
2
"""
 
3
    MoinMoin - Action to load page content from a file upload
 
4
 
 
5
    @copyright: 2007-2008 MoinMoin:ReimarBauer,
 
6
                2008 MoinMoin:ThomasWaldmann
 
7
    @license: GNU GPL, see COPYING for details.
 
8
"""
 
9
 
 
10
import os
 
11
 
 
12
from MoinMoin import wikiutil, config
 
13
from MoinMoin.action import ActionBase, AttachFile
 
14
from MoinMoin.PageEditor import PageEditor
 
15
from MoinMoin.Page import Page
 
16
 
 
17
class Load(ActionBase):
 
18
    """ Load page action
 
19
 
 
20
    Note: the action name is the class name
 
21
    """
 
22
    def __init__(self, pagename, request):
 
23
        ActionBase.__init__(self, pagename, request)
 
24
        self.use_ticket = True
 
25
        _ = self._
 
26
        self.form_trigger = 'Load'
 
27
        self.form_trigger_label = _('Load')
 
28
        self.pagename = pagename
 
29
        self.method = 'POST'
 
30
        self.enctype = 'multipart/form-data'
 
31
 
 
32
    def do_action(self):
 
33
        """ Load """
 
34
        status = False
 
35
        _ = self._
 
36
        form = self.form
 
37
        request = self.request
 
38
 
 
39
        comment = form.get('comment', [u''])[0]
 
40
        comment = wikiutil.clean_input(comment)
 
41
 
 
42
        filename = form.get('file__filename__')
 
43
        rename = form.get('rename', [''])[0].strip()
 
44
        if rename:
 
45
            target = rename
 
46
        else:
 
47
            target = filename
 
48
 
 
49
        target = AttachFile.preprocess_filename(target)
 
50
        target = wikiutil.clean_input(target)
 
51
 
 
52
        if target:
 
53
            filecontent = form['file'][0]
 
54
            if hasattr(filecontent, 'read'): # a file-like object
 
55
                filecontent = filecontent.read() # XXX reads complete file into memory!
 
56
            filecontent = wikiutil.decodeUnknownInput(filecontent)
 
57
 
 
58
            self.pagename = target
 
59
            pg = PageEditor(request, self.pagename)
 
60
            try:
 
61
                msg = pg.saveText(filecontent, 0, comment=comment)
 
62
                status = True
 
63
            except pg.EditConflict, e:
 
64
                msg = e.message
 
65
            except pg.SaveError, msg:
 
66
                msg = unicode(msg)
 
67
        else:
 
68
            msg = _("Pagename not specified!")
 
69
        return status, msg
 
70
 
 
71
    def do_action_finish(self, success):
 
72
        if success:
 
73
            url = Page(self.request, self.pagename).url(self.request)
 
74
            self.request.http_redirect(url)
 
75
            self.request.finish()
 
76
        else:
 
77
            self.render_msg(self.make_form(), "dialog")
 
78
 
 
79
    def get_form_html(self, buttons_html):
 
80
        _ = self._
 
81
        return """
 
82
<h2>%(headline)s</h2>
 
83
<p>%(explanation)s</p>
 
84
<dl>
 
85
<dt>%(upload_label_file)s</dt>
 
86
<dd><input type="file" name="file" size="50" value=""></dd>
 
87
<dt>%(upload_label_rename)s</dt>
 
88
<dd><input type="text" name="rename" size="50" value="%(pagename)s"></dd>
 
89
<dt>%(upload_label_comment)s</dt>
 
90
<dd><input type="text" name="comment" size="80" maxlength="200"></dd>
 
91
</dl>
 
92
<p>
 
93
<input type="hidden" name="action" value="%(action_name)s">
 
94
<input type="hidden" name="do" value="upload">
 
95
</p>
 
96
<td class="buttons">
 
97
%(buttons_html)s
 
98
</td>""" % {
 
99
    'headline': _("Upload page content"),
 
100
    'explanation': _("You can upload content for the page named below. "
 
101
                     "If you change the page name, you can also upload content for another page. "
 
102
                     "If the page name is empty, we derive the page name from the file name."),
 
103
    'upload_label_file': _('File to load page content from'),
 
104
    'upload_label_comment': _('Comment'),
 
105
    'upload_label_rename': _('Page Name'),
 
106
    'pagename': self.pagename,
 
107
    'buttons_html': buttons_html,
 
108
    'action_name': self.form_trigger,
 
109
}
 
110
 
 
111
def execute(pagename, request):
 
112
    """ Glue code for actions """
 
113
    Load(pagename, request).render()
 
114