changeset 405:43ee86f48825

action.SvgEditor: gedit_drawing and attachment_drawing added
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Mon, 31 Aug 2009 00:42:43 +0200
parents 01cb43413b59
children d17d73264ec5
files data/plugin/action/SvgEditor.py
diffstat 1 files changed, 64 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/data/plugin/action/SvgEditor.py	Fri Aug 28 09:39:07 2009 +0200
+++ b/data/plugin/action/SvgEditor.py	Mon Aug 31 00:42:43 2009 +0200
@@ -3,7 +3,6 @@
     MoinMoin - SvgEditor
 
     This action is used to call svg-edit, (http://code.google.com/p/svg-edit/) 
-    it is just a thin wrapper around it.
     
     svg-edit pre alpha is called 
     this action needs moin-1.9 and is currently a mockup for an implementation in moin-2.0
@@ -13,10 +12,66 @@
 """
 from MoinMoin import config, wikiutil
 from MoinMoin.action import AttachFile
+from MoinMoin.Page import Page
 
+def gedit_drawing(self, url, text, **kw):
+    # This is called for displaying a drawing image by gui editor.
+    _ = self.request.getText
+    # TODO: this 'text' argument is kind of superfluous, replace by using alt=... kw arg
+    # ToDo: make this clickable for the gui editor
+    if 'alt' not in kw or not kw['alt']:
+        kw['alt'] = text
+    # we force the title here, needed later for html>wiki converter
+    kw['title'] = "drawing:%s" % wikiutil.quoteWikinameURL(url)
+    pagename, drawing = AttachFile.absoluteName(url, self.page.page_name)
+    drawing_url = AttachFile.getAttachUrl(pagename, drawing+'.svg', self.request, drawing=drawing, upload=True)
+    if not AttachFile.exists(self.request, pagename, drawing+'.svg'):
+        title = _('Create new drawing "%(filename)s (opens in new window)"') % {'filename': drawing}
+        img = self.icon('attachimg')  # TODO: we need a new "drawimg" in similar grey style and size
+        css = 'nonexistent'
+
+    # ToDo use EmbedObject here 
+    
+    kw['src'] = drawing + '.svg'
+    return self.image(**kw)
+
+def attachment_drawing(self, url, text, **kw):
+    # This is called for displaying a clickable drawing image by text_html formatter.
+    # XXX text arg is unused!
+    _ = self.request.getText
+    pagename, drawing = AttachFile.absoluteName(url, self.page.page_name)
+    if not drawing.endswith('.svg'):
+        drawing = "%s%s" % (drawing, '.svg')
+
+    drawing_url = AttachFile.getAttachUrl(pagename, drawing, self.request, drawing=drawing, upload=True)
+    if not AttachFile.exists(self.request, pagename, drawing):
+        title = _('Create new drawing "%(filename)s (opens in new window)"') % {'filename': drawing}
+        img = self.icon('attachimg')  # TODO: we need a new "drawimg" in similar grey style and size
+        css = 'nonexistent'
+        return self.url(1, drawing_url, css=css, title=title) + img + self.url(0)
+
+    title = _('Edit drawing %(filename)s (opens in new window)') % {'filename': self.text(drawing)}
+        
+    from MoinMoin import macro
+    from MoinMoin.parser.text import Parser
+    macro.request = self.request
+    macro.formatter = self.request.html_formatter
+    p = Parser("##\n", self.request)
+    m = macro.Macro(p)
+    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
+
     filecontent = ''
     if AttachFile.exists(request, pagename, target):
         fpath = AttachFile.getFilename(request, pagename, target).encode(config.charset)
@@ -26,20 +81,22 @@
             # 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':
+        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:
+            AttachFile._addLogEntry(request, 'ATTDRW', pagename, target + '.svg')
             # get directory, and possibly create it
             attach_dir = AttachFile.getAttachDir(request, pagename, create=1)
             # save file
             fpath = AttachFile.getFilename(request, pagename, target).encode(config.charset)
             file(fpath, 'w').write(filecontent.decode('base_64'))
-            return None, None
-
+            url = Page(request, pagename).url(request)
+            request.http_redirect(url)
             
     url = request.getQualifiedURL()
     htdocs = "%s%s" % (request.cfg.url_prefix_static, '/svg-edit/editor')
@@ -65,7 +122,7 @@
 <!--script type="text/javascript" src="%(htdocs)s/svg-editor.min.js"></script-->
 """ % {"htdocs": htdocs} 
 
-    html = """
+    html = """<h2> %(editdrawing)s </h2>
 <div id="htdocs"><!-- %(htdocs)s/ --></div>    
 <div id="svg_editor">
 <div id="workarea">
@@ -331,6 +388,7 @@
 
 """ % {"htdocs": htdocs,
        "filecontent": filecontent,
+       "editdrawing": _("Edit drawing"),
        }
 
     title = "%s:%s" % (pagename, target)
@@ -342,4 +400,4 @@
     request.write(request.formatter.endContent())
     request.theme.send_footer(pagename)
     request.theme.send_closing_html()
-    return None,None
+