changeset 5107:26379ee2aa74

anywikidraw/twikidraw action: more cleanup
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 13 Sep 2009 20:13:06 +0200
parents fecbfc7f341d
children 6c31e9dfdd99
files MoinMoin/action/anywikidraw.py MoinMoin/action/twikidraw.py
diffstat 2 files changed, 41 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/action/anywikidraw.py	Sun Sep 13 17:47:59 2009 +0200
+++ b/MoinMoin/action/anywikidraw.py	Sun Sep 13 20:13:06 2009 +0200
@@ -20,7 +20,6 @@
 from MoinMoin import config, wikiutil
 from MoinMoin.action import AttachFile, do_show
 from MoinMoin.action.AttachFile import _write_stream
-from MoinMoin.Page import Page
 
 action_name = __name__.split('.')[-1]
 
@@ -80,23 +79,20 @@
 class AnyWikiDraw(object):
     """ anywikidraw action """
     def __init__(self, request, pagename, target):
-        self._ = request.getText
         self.request = request
         self.pagename = pagename
         self.target = target
 
-    def render_msg(self, msg, msgtype):
-        """ Called to display some message (can also be the action form) """
-        self.request.theme.add_msg(msg, msgtype)
-        do_show(self.pagename, self.request)
-
     def save(self):
-        _ = self._
+        request = self.request
+        _ = request.getText
         pagename = self.pagename
-        request = self.request
         target = self.target
         if not request.user.may.write(pagename):
             return _('You are not allowed to save a drawing on this page.')
+        if not target:
+            return _("Empty target given.")
+
         file_upload = request.files.get('filepath')
         if not file_upload:
             # This might happen when trying to upload file names
@@ -131,10 +127,15 @@
             ci.put('drawing' + ext, filecontent, content_length)
 
     def render(self):
-        _ = self._
         request = self.request
+        _ = request.getText
         pagename = self.pagename
         target = self.target
+        if not request.user.may.read(pagename):
+            return _('You are not allowed to view attachments of this page.')
+        if not target:
+            return _("Empty target given.")
+
         ci = AttachFile.ContainerItem(request, pagename, target)
         if ci.exists():
             drawurl = ci.member_url('drawing.svg')
@@ -191,24 +192,15 @@
 
 
 def execute(pagename, request):
-    _ = request.getText
-    msg = None
-    if not request.user.may.read(pagename):
-        msg = '<p>%s</p>' % _('You are not allowed to view this page.')
-        AnyWikiDraw(request, pagename, target).render_msg(msg, 'error')
-        return
+    target = request.values.get('target')
+    awd = AnyWikiDraw(request, pagename, target)
 
-    target = request.values.get('target', '')
-    if not target:
-        msg = '<p>%s</p>' % _("Empty target given.")
-        AnyWikiDraw(request, pagename, target).render_msg(msg, 'error')
-        return
+    do = request.values.get('do')
+    if do == 'save':
+        msg = awd.save()
+    else:
+        msg = awd.render()
+    if msg:
+        request.theme.add_msg(msg, 'error')
+        do_show(pagename, request)
 
-    do = request.values.get('do', '')
-    if do == 'save' and request.user.may.write(pagename):
-        msg = AnyWikiDraw(request, pagename, target).save()
-        AnyWikiDraw(request, pagename, target).render_msg(msg, 'error')
-        return
-
-    AnyWikiDraw(request, pagename, target).render()
-
--- a/MoinMoin/action/twikidraw.py	Sun Sep 13 17:47:59 2009 +0200
+++ b/MoinMoin/action/twikidraw.py	Sun Sep 13 20:13:06 2009 +0200
@@ -20,7 +20,6 @@
 from MoinMoin import wikiutil, config
 from MoinMoin.action import AttachFile, do_show
 from MoinMoin.action.AttachFile import _write_stream
-from MoinMoin.Page import Page
 from MoinMoin.security.textcha import TextCha
 
 action_name = __name__.split('.')[-1]
@@ -102,25 +101,21 @@
 class TwikiDraw(object):
     """ twikidraw action """
     def __init__(self, request, pagename, target):
-        self._ = request.getText
         self.request = request
         self.pagename = pagename
         self.target = target
 
-    def render_msg(self, msg, msgtype):
-        """ Called to display some message (can also be the action form) """
-        self.request.theme.add_msg(msg, msgtype)
-        do_show(self.pagename, self.request)
-
     def save(self):
-        _ = self._
+        request = self.request
+        _ = request.getText
         pagename = self.pagename
-        request = self.request
         target = self.target
         if not TextCha(request).check_answer_from_form():
             return _('TextCha: Wrong answer! Go back and try again...')
         if not request.user.may.write(pagename):
             return _('You are not allowed to save a drawing on this page.')
+        if not target:
+            return _("Empty target given.")
 
         file_upload = request.files.get('filepath')
         if not file_upload:
@@ -152,14 +147,18 @@
             filecontent = filecontent.read()
 
         ci.put('drawing' + ext, filecontent, content_length)
-        #return _("Thank you for your changes. Your attention to detail is appreciated.")
 
 
     def render(self):
-        _ = self._
         request = self.request
+        _ = request.getText
         pagename = self.pagename
         target = self.target
+        if not request.user.may.read(pagename):
+            return _('You are not allowed to view attachments of this page.')
+        if not target:
+            return _("Empty target given.")
+
         ci = AttachFile.ContainerItem(request, pagename, target)
         if ci.exists():
             drawurl = ci.member_url('drawing.draw')
@@ -210,24 +209,16 @@
 
 
 def execute(pagename, request):
-    _ = request.getText
-    msg = None
-    if not request.user.may.read(pagename):
-        msg = '<p>%s</p>' % _('You are not allowed to view this page.')
-        TwikiDraw(request, pagename, target).render_msg(msg, 'error')
-        return
+    target = request.values.get('target')
+    twd = TwikiDraw(request, pagename, target)
 
-    target = request.values.get('target', '')
-    if not target:
-        msg = '<p>%s</p>' % _("Empty target given.")
-        TwikiDraw(request, pagename, target).render_msg(msg, 'error')
-        return
+    do = request.values.get('do')
+    if do == 'save':
+        msg = twd.save()
+    else:
+        msg = twd.render()
+    if msg:
+        request.theme.add_msg(msg, 'error')
+        do_show(pagename, request)
 
-    do = request.values.get('do', '')
-    if do == 'save' and request.user.may.write(pagename):
-        msg = TwikiDraw(request, pagename, target).save()
-        TwikiDraw(request, pagename, target).render_msg(msg, 'error')
-        return
 
-    TwikiDraw(request, pagename, target).render()
-