changeset 417:7111c1ea508a

action SvgEditor: refactored for using TextCha
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Sat, 12 Sep 2009 10:57:12 +0200
parents e5eeb143b2ae
children 2efbd05651e1
files data/plugin/action/SvgEditor.py htdocs/svg-edit/editor/svg-editor.js
diffstat 2 files changed, 93 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/data/plugin/action/SvgEditor.py	Sat Sep 12 00:14:08 2009 +0200
+++ b/data/plugin/action/SvgEditor.py	Sat Sep 12 10:57:12 2009 +0200
@@ -11,8 +11,11 @@
     @license: GNU GPL, see COPYING for details.
 """
 from MoinMoin import config, wikiutil
-from MoinMoin.action import AttachFile
+from MoinMoin.action import AttachFile, do_show
 from MoinMoin.Page import Page
+from MoinMoin.security.textcha import TextCha
+
+action_name = __name__.split('.')[-1]
 
 def gedit_drawing(self, url, text, **kw):
     # This is called for displaying a drawing image by gui editor.
@@ -61,31 +64,28 @@
     img = m.execute('EmbedObject', u'target=%s' % drawing)
     return self.url(1, drawing_url) + img + self.url(0)
 
-def execute(pagename, request):
-    #ToDo refactoring to class 
-    _ = request.getText
-    if not request.user.may.read(pagename):
-        request.write('<p>%s</p>' % _('You are not allowed to view this page.'))
-        return
-    target = request.values.get('target', 'example.svg')
-    if not target:
-        request.write('<p>%s</p>' % _("Empty target given."))
-        return
+class SvgEditor(object):
+    """ svg-edit action """
+    def __init__(self, request, pagename, target):
+        self._ = request.getText
+        self.request = request
+        self.pagename = pagename
+        self.target = target
 
-    filecontent = ''
-    if AttachFile.exists(request, pagename, target):
-        fpath = AttachFile.getFilename(request, pagename, target).encode(config.charset)
-        filecontent = file(fpath, 'r').readlines()
-        filecontent = ''.join(filecontent[1:])
-        if filecontent:
-            # svg data is saved as xml. at least firefox changes the data by adding closing tags e.g. </rect></circle></circle></circle></rect></svg>
-            # this renders then only the first element
-            filecontent = "<!-- %s -->" % filecontent
-        
-    do = request.values.get('do', '')
-    if do == 'save':
+    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._
+        pagename = self.pagename
+        request = self.request
+        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.')
+
         filecontent = request.values.get('svg_data', '')
         target = request.values.get('target', 'example.svg')
         if filecontent:
@@ -95,13 +95,25 @@
             # save file
             fpath = AttachFile.getFilename(request, pagename, target).encode(config.charset)
             file(fpath, 'w').write(filecontent.decode('base_64'))
-            url = Page(request, pagename).url(request)
-            request.http_redirect(url)
-            
-    url = request.getQualifiedURL()
-    htdocs = "%s%s" % (request.cfg.url_prefix_static, '/svg-edit/editor')
-    
-    meta = """<http-equiv="Content-type" content="text/html;charset=UTF-8" />
+
+ 
+    def render(self):
+        _ = self._
+        request = self.request
+        pagename = self.pagename
+        target = self.target
+        url = request.getQualifiedURL()
+        htdocs = "%s%s" % (request.cfg.url_prefix_static, '/svg-edit/editor')
+        filecontent = ''
+        if AttachFile.exists(request, pagename, target):
+            fpath = AttachFile.getFilename(request, pagename, target).encode(config.charset)
+            filecontent = file(fpath, 'r').readlines()
+            filecontent = ''.join(filecontent[1:])
+        if filecontent:
+             # svg data is saved as xml. at least firefox changes the data by adding closing tags e.g. </rect></circle></circle></circle></rect></svg>
+             # this renders then only the first element
+             filecontent = "<!-- %s -->" % filecontent
+        meta = """<http-equiv="Content-type" content="text/html;charset=UTF-8" />
 <link rel="icon" type="image/png" href="%(htdocs)s/images/logo.png">
 <link rel="stylesheet" href="%(htdocs)s/jgraduate/css/jPicker-1.0.9.css" type="text/css"/>
 <link rel="stylesheet" href="%(htdocs)s/jgraduate/css/jGraduate-0.2.0.css" type="text/css"/>
@@ -120,10 +132,15 @@
 <!--script type="text/javascript" src="%(htdocs)s/svgcanvas.min.js"></script-->
 <script type="text/javascript" src="%(htdocs)s/svg-editor.js"></script>
 <!--script type="text/javascript" src="%(htdocs)s/svg-editor.min.js"></script-->
-""" % {"htdocs": htdocs} 
+""" % {"htdocs": htdocs}
 
-    html = """<h2> %(editdrawing)s </h2>
-<div id="htdocs"><!-- %(htdocs)s/ --></div>    
+        question = ''
+        if TextCha(self.request).is_enabled():
+            question = TextCha(self.request).question
+            
+        html = """<h2> %(editdrawing)s </h2>
+%(textcha)s
+<div id="htdocs"><!-- %(htdocs)s/ --></div>
 <div id="svg_editor">
 <div id="workarea">
 <style id="styleoverrides" type="text/css"></style>
@@ -132,9 +149,9 @@
 </div>
 
 <div id="logo">
-	<a href="http://svg-edit.googlecode.com/" target="_blank" title="SVG-edit Home Page">
-		<img src="%(htdocs)s/images/logo.png" alt="logo" />
-	</a>
+    <a href="http://svg-edit.googlecode.com/" target="_blank" title="SVG-edit Home Page">
+        <img src="%(htdocs)s/images/logo.png" alt="logo" />
+    </a>
 </div>
 
 <div id="tools_top" class="tools_panel">
@@ -200,12 +217,12 @@
         <option value="smallest">smallest object</option>
         <option value="page">page</option>
         </select>
-        <img class="tool_sep" src="(htdocs)s/images/sep.png" alt="|"/>
-        <img class="tool_button" id="tool_group" src="(htdocs)s/images/shape_group.png" title="Group Elements [G]" alt="Group"/>
+        <img class="tool_sep" src="%(htdocs)s/images/sep.png" alt="|"/>
+        <img class="tool_button" id="tool_group" src="%(htdocs)s/images/shape_group.png" title="Group Elements [G]" alt="Group"/>
         </div>
         <div id="g_panel">
-        <img class="tool_sep" src="images/sep.png" alt="|"/>
-        <img class="tool_button" id="tool_ungroup" src="(htdocs)s/images/shape_ungroup.png" title="Ungroup Elements [G]" alt="Ungroup"/>
+        <img class="tool_sep" src="%(htdocs)s/images/sep.png" alt="|"/>
+        <img class="tool_button" id="tool_ungroup" src="%(htdocs)s/images/shape_ungroup.png" title="Ungroup Elements [G]" alt="Ungroup"/>
         </div>
 
 
@@ -410,18 +427,41 @@
     </div>
 </div>
 
-""" % {"htdocs": htdocs,
+""" % {"textcha": TextCha(self.request).render(),
+       "htdocs": htdocs,
        "filecontent": filecontent,
        "editdrawing": _("Edit drawing"),
        }
 
-    title = "%s:%s" % (pagename, target)
-    request.theme.send_title(title, page=request.page, pagename=pagename)
-    request.write(request.formatter.startContent("content"))
-    request.write(request.formatter.rawHTML(meta))
-    
-    request.write(request.formatter.rawHTML(html))
-    request.write(request.formatter.endContent())
-    request.theme.send_footer(pagename)
-    request.theme.send_closing_html()
+        title = "%s:%s" % (pagename, target)
+        request.theme.send_title(title, page=request.page, pagename=pagename)
+        request.write(request.formatter.startContent("content"))
+        request.write(request.formatter.rawHTML(meta))
+        request.write(request.formatter.rawHTML(html))
+        request.write(request.formatter.endContent())
+        request.theme.send_footer(pagename)
+        request.theme.send_closing_html()         
+        
+def execute(pagename, request):
+    #ToDo refactoring to class 
 
+    _ = request.getText
+    msg = None
+    if not request.user.may.read(pagename):
+        msg = '<p>%s</p>' % _('You are not allowed to view this page.')
+        SvgEditor(request, pagename, target).render_msg(msg, 'error')
+        return
+
+    target = request.values.get('target', '')
+    if not target:
+        msg = '<p>%s</p>' % _("Empty target given.")
+        SvgEditor(request, pagename, target).render_msg(msg, 'error')
+        return
+
+    do = request.values.get('do', '')
+    if do == 'save' and request.user.may.write(pagename):
+        msg = SvgEditor(request, pagename, target).save()
+        SvgEditor(request, pagename, target).render_msg(msg, 'error')
+        return
+
+    SvgEditor(request, pagename, target).render()
--- a/htdocs/svg-edit/editor/svg-editor.js	Sat Sep 12 00:14:08 2009 +0200
+++ b/htdocs/svg-edit/editor/svg-editor.js	Sat Sep 12 10:57:12 2009 +0200
@@ -11,6 +11,7 @@
 	var isMac = false; //(navigator.platform.indexOf("Mac") != -1);
 	var modKey = ""; //(isMac ? "meta+" : "ctrl+");
 	var htdocs = document.getElementById("htdocs").innerHTML.replace('<!-- ', '').replace(' -->', '');
+	var textcha_question = document.getElementById("textcha-question").firstChild.data;
 	var svg = document.getElementById("svgdata");
 	var svgCanvas = new SvgCanvas(document.getElementById("svgcanvas"));
 	var filecontent =  svg.innerHTML;
@@ -55,9 +56,10 @@
 	var svg_data = Utils.encode64(svg);
 	var pagename = titlename.split(':')[0];
 	var svg_target = titlename.split(':')[1];
+	var textcha_answer = document.getElementById("textcha-answer").value;
 	$.post(
 	        pagename,
-	        {'action': "SvgEditor", 'do': "save", 'target': svg_target, 'svg_data': svg_data}
+	        {'action': "SvgEditor", 'do': "save", 'target': svg_target, 'svg_data': svg_data, 'textcha-question': textcha_question, 'textcha-answer': textcha_answer}
 	      );
 
 		//window.open("data:image/svg+xml;base64," + Utils.encode64(svg));