Back to index

moin  1.9.0~rc2
anywikidraw.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - anywikidraw
00004 
00005     This action is used to call anywikidraw (http://sourceforge.net/projects/anywikidraw/)
00006 
00007     @copyright: 2001 by Ken Sugino (sugino@mediaone.net),
00008                 2001-2004 by Juergen Hermann <jh@web.de>,
00009                 2005 MoinMoin:AlexanderSchremmer,
00010                 2005 DiegoOngaro at ETSZONE (diego@etszone.com),
00011                 2007-2008 MoinMoin:ThomasWaldmann,
00012                 2005-2009 MoinMoin:ReimarBauer
00013     @license: GNU GPL, see COPYING for details.
00014 """
00015 import os, re
00016 
00017 from MoinMoin import log
00018 logging = log.getLogger(__name__)
00019 
00020 from MoinMoin import config, wikiutil
00021 from MoinMoin.action import AttachFile, do_show
00022 from MoinMoin.action.AttachFile import _write_stream
00023 
00024 action_name = __name__.split('.')[-1]
00025 
00026 
00027 from MoinMoin.action.twikidraw import gedit_drawing
00028 
00029 
00030 def attachment_drawing(self, url, text, **kw):
00031     # This is called for displaying a clickable drawing image by text_html formatter.
00032     # XXX text arg is unused!
00033     _ = self.request.getText
00034     pagename, drawing = AttachFile.absoluteName(url, self.page.page_name)
00035     containername = wikiutil.taintfilename(drawing)
00036 
00037     drawing_url = AttachFile.getAttachUrl(pagename, containername, self.request, do='modify')
00038     ci = AttachFile.ContainerItem(self.request, pagename, containername)
00039     if not ci.exists():
00040         title = _('Create new drawing "%(filename)s (opens in new window)"') % {'filename': self.text(containername)}
00041         img = self.icon('attachimg')  # TODO: we need a new "drawimg" in similar grey style and size
00042         css = 'nonexistent'
00043         return self.url(1, drawing_url, css=css, title=title) + img + self.url(0)
00044 
00045     title = _('Edit drawing %(filename)s (opens in new window)') % {'filename': self.text(containername)}
00046     kw['src'] = src = ci.member_url('drawing.png')
00047     kw['css'] = 'drawing'
00048 
00049     try:
00050         mapfile = ci.get('drawing.map')
00051         map = mapfile.read()
00052         mapfile.close()
00053         map = map.decode(config.charset)
00054     except (KeyError, IOError, OSError):
00055         map = u''
00056     if map:
00057         # ToDo mapid must become uniq
00058         # we have a image map. inline it and add a map ref to the img tag
00059         # we have also to set a unique ID
00060         mapid = u'ImageMapOf%s%s' % (self.request.uid_generator(pagename), drawing)
00061 
00062         map = map.replace(u'id="%s.svg"' % drawing, '')
00063         map = map.replace(u'name="%s.svg"' % drawing, u'name="%s"' % mapid)
00064         # unxml, because 4.01 concrete will not validate />
00065         map = map.replace(u'/>', u'>')
00066         title = _('Clickable drawing: %(filename)s') % {'filename': self.text(containername)}
00067         if 'title' not in kw:
00068             kw['title'] = title
00069         if 'alt' not in kw:
00070             kw['alt'] = kw['title']
00071         kw['usemap'] = '#'+mapid
00072         return self.url(1, drawing_url) + map + self.image(**kw) + self.url(0)
00073     else:
00074         if 'title' not in kw:
00075             kw['title'] = title
00076         if 'alt' not in kw:
00077             kw['alt'] = kw['title']
00078         return self.url(1, drawing_url) + self.image(**kw) + self.url(0)
00079 
00080 
00081 class AnyWikiDraw(object):
00082     """ anywikidraw action """
00083     def __init__(self, request, pagename, target):
00084         self.request = request
00085         self.pagename = pagename
00086         self.target = target
00087 
00088     def save(self):
00089         request = self.request
00090         _ = request.getText
00091         pagename = self.pagename
00092         target = self.target
00093         if not request.user.may.write(pagename):
00094             return _('You are not allowed to save a drawing on this page.')
00095         if not target:
00096             return _("Empty target name given.")
00097 
00098         file_upload = request.files.get('filepath')
00099         if not file_upload:
00100             # This might happen when trying to upload file names
00101             # with non-ascii characters on Safari.
00102             return _("No file content. Delete non ASCII characters from the file name and try again.")
00103 
00104         filename = request.form['filename']
00105         basepath, basename = os.path.split(filename)
00106         basename, ext = os.path.splitext(basename)
00107 
00108         ci = AttachFile.ContainerItem(request, pagename, target)
00109         filecontent = file_upload.stream
00110         content_length = None
00111         if ext == '.svg': # AnyWikiDraw POSTs this first
00112             AttachFile._addLogEntry(request, 'ATTDRW', pagename, target)
00113             ci.truncate()
00114             filecontent = filecontent.read() # read file completely into memory
00115             filecontent = filecontent.replace("\r", "")
00116         elif ext == '.map':
00117             # touch attachment directory to invalidate cache if new map is saved
00118             attach_dir = AttachFile.getAttachDir(request, pagename)
00119             os.utime(attach_dir, None)
00120             filecontent = filecontent.read() # read file completely into memory
00121             filecontent = filecontent.strip()
00122         else:
00123             #content_length = file_upload.content_length
00124             # XXX gives -1 for wsgiref :( If this is fixed, we could use the file obj,
00125             # without reading it into memory completely:
00126             filecontent = filecontent.read()
00127 
00128         if filecontent:
00129             ci.put('drawing' + ext, filecontent, content_length)
00130 
00131     def render(self):
00132         request = self.request
00133         _ = request.getText
00134         pagename = self.pagename
00135         target = self.target
00136         if not request.user.may.read(pagename):
00137             return _('You are not allowed to view attachments of this page.')
00138         if not target:
00139             return _("Empty target name given.")
00140 
00141         ci = AttachFile.ContainerItem(request, pagename, target)
00142         if ci.exists():
00143             drawurl = ci.member_url('drawing.svg')
00144         else:
00145             drawurl = ''
00146         pageurl = request.href(pagename)
00147         saveurl = request.href(pagename, action=action_name, do='save', target=target)
00148         helpurl = request.href("HelpOnActions/AttachFile")
00149 
00150         html = """
00151 <p>
00152 <applet code="org.anywikidraw.moinmoin.MoinMoinDrawingApplet.class" codebase="."
00153         archive="%(htdocs)s/applets/anywikidraw/lib/AnyWikiDrawForMoinMoin.jar" width="800" height="620">
00154 
00155     <!-- The following parameters are used to tell AnyWikiDraw how to communicate with MoinMoin. -->
00156     <param name="DrawingName" value="%(basename)s.svg">
00157     <param name="DrawingURL" value="%(drawurl)s">
00158     <param name="PageURL" value="%(pageurl)s">
00159     <param name="UploadURL" value="%(saveurl)s">
00160 
00161     <!-- The following parameters are used to configure the drawing applet -->
00162     <param name="Locale" value="en">
00163 
00164     <!-- The following parameters are used to configure Sun's Java Plug-In -->
00165     <param name="codebase_lookup" value="false">
00166     <param name="classloader_cache" value="false">
00167     <!-- The following makes trouble with FF3 on Ubuntu 9.04 as client and
00168          Apache2 / mod_wsgi on Debian Lenny as server, it seems to confuse
00169          .gz files with gzip content-encoding and fails miserably somehow:
00170          param name="java_arguments" value="-Djnlp.packEnabled=true" -->
00171     <param name="boxborder" value="false">
00172     <param name="centerimage" value="true">
00173     <strong>NOTE:</strong> You need a Java enabled browser to edit the drawing.
00174 </applet>
00175 </p>
00176 """ % dict(
00177     htdocs=request.cfg.url_prefix_static,
00178     basename=wikiutil.escape(target, 1),
00179     drawurl=wikiutil.escape(drawurl, 1),
00180     pageurl=wikiutil.escape(pageurl, 1),
00181     saveurl=wikiutil.escape(saveurl, 1),
00182 )
00183 
00184         title = '%s %s:%s' % (_('Edit drawing'), pagename, target)
00185         request.theme.send_title(title, page=request.page, pagename=pagename)
00186         request.write(request.formatter.startContent("content"))
00187         request.write(request.formatter.rawHTML(html))
00188         request.write(request.formatter.endContent())
00189         request.theme.send_footer(pagename)
00190         request.theme.send_closing_html()
00191 
00192 
00193 def execute(pagename, request):
00194     target = request.values.get('target')
00195     awd = AnyWikiDraw(request, pagename, target)
00196 
00197     do = request.values.get('do')
00198     if do == 'save':
00199         msg = awd.save()
00200     else:
00201         msg = awd.render()
00202     if msg:
00203         request.theme.add_msg(msg, 'error')
00204         do_show(pagename, request)
00205