view MoinMoin/action/Load.py @ 3586:35366465d4fe

AttachFile/Load: remove misleading/outdated text, Load: fix UI cosmetics, reuse i18n texts
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 12 May 2008 14:46:02 +0200
parents 6fe1ea4d9d1a
children 15e5ca7240ab
line wrap: on
line source
# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - Action macro for page creation from file or attach file to current page

    @copyright: 2007-2008 MoinMoin:ReimarBauer
    @license: GNU GPL, see COPYING for details.
"""
import os
from MoinMoin import wikiutil, config
from MoinMoin.action import ActionBase, AttachFile
from MoinMoin.PageEditor import PageEditor
from MoinMoin.Page import Page

class Load(ActionBase):
    """ Load page action

    Note: the action name is the class name
    """
    def __init__(self, pagename, request):
        ActionBase.__init__(self, pagename, request)
        self.use_ticket = True
        _ = self._
        self.form_trigger = 'Load'
        self.form_trigger_label = _('Load')
        self.pagename = pagename
        self.method = 'POST'
        self.enctype = 'multipart/form-data'

    def do_action(self):
        """ Load """
        status = False
        _ = self._
        form = self.form
        author = self.request.user.name

        rename = form.get('rename', [u''])[0]

        filename = None
        if 'file__filename__' in form:
            filename = form['file__filename__']

        filecontent = form['file'][0]

        overwrite = False
        if 'overwrite' in form:
            overwrite = True

        target = filename
        if rename:
            target = wikiutil.clean_input(rename.strip())

        # preprocess the filename
        # strip leading drive and path (IE misbehaviour)
        if len(target) > 1 and (target[1] == ':' or target[0] == '\\'): # C:.... or \path... or \\server\...
            bsindex = target.rfind('\\')
            if bsindex >= 0:
                target = target[bsindex+1:]

        if 'attachment' in self.request.form and self.request.user.may.write(self.pagename):
            attach_dir = AttachFile.getAttachDir(self.request, self.pagename, create=1)
            fpath = os.path.join(attach_dir, target).encode(config.charset)
            exists = os.path.exists(fpath)
            if exists and not overwrite:
                msg = _("Attachment '%(target)s' (remote name '%(filename)s') already exists.") % {
                         'target': target, 'filename': filename}
                return status, msg

            if exists and overwrite:
                if self.request.user.may.delete(self.pagename):
                    try:
                        os.remove(fpath)
                    except:
                        pass
                else:
                    msg = _("You are not allowed to delete attachments on this page.")
                    return status, msg

            target, bytes = AttachFile.add_attachment(self.request, self.pagename, target, filecontent)
            msg = _("Attachment '%(target)s' (remote name '%(filename)s') with %(bytes)d bytes saved.") % {
                   'target': target, 'filename': filename, 'bytes': bytes}
            status = True

        else:
            if isinstance(filecontent, file):
                filecontent = filecontent.read() # XXX reads complete file into memory!
            if isinstance(filecontent, str):
                filecontent = unicode(filecontent, config.charset)
            self.pagename = target
            page = Page(self.request, self.pagename)
            pagedir = page.getPagePath("", check_create=0)
            rev = Page.get_current_from_pagedir(page, pagedir)
            pg = PageEditor(self.request, self.pagename, do_editor_backup=0, uid_override=author)
            try:
                msg = pg.saveText(filecontent, rev)
                status = True
            except pg.EditConflict, e:
                msg = e.message
            except pg.SaveError, msg:
                msg = unicode(msg)

        return status, msg

    def do_action_finish(self, success):
        if success:
            url = Page(self.request, self.pagename).url(self.request)
            self.request.http_redirect(url)
            self.request.finish()
        else:
            self.render_msg(self.make_form(), "dialog")

    def get_form_html(self, buttons_html):
        _ = self._
        return """
%(querytext_pages)s
<dl>
<dt>%(upload_label_file)s</dt>
<dd><input type="file" name="file" size="50" value=""></dd>
<dt>%(upload_label_rename)s</dt>
<dd><input type="text" name="rename" size="50" value=""></dd>
%(querytext_attachment)s
<dt><input type="checkbox" name="attachment" value="off"> %(upload)s</dt>
<dt><input type="checkbox" name="overwrite" value="off"> %(overwrite)s</dt>
</dl>
<p>
<input type="hidden" name="action" value="%(action_name)s">
<input type="hidden" name="do" value="upload">
</p>
<td class="buttons">
%(buttons_html)s
</td>""" % {
    'querytext_pages': '<h2>' + _("New Page or New Attachment") + '</h2><p>' +
_("""You can upload a file to a new page or choose to upload a file as attachment for the current page""") + '</p>',
    'querytext_attachment': '<h2>' + _("New Attachment") + '</h2>',
    'buttons_html': buttons_html,
    'upload': _('attachment'),
    'overwrite': _('Overwrite existing attachment of same name'),
    'action_name': self.form_trigger,
    'upload_label_file': _('Upload'),
    'upload_label_rename': _('New Name'),
}

def execute(pagename, request):
    """ Glue code for actions """
    Load(pagename, request).render()