annotate MoinMoin/action/twikidraw.py @ 6025:05a6897ee496

"userdir" caching scope: use it for userid lookup caches, reduce code duplication For same reasons (see previous changesets), we need to use the "userdir" scope for the name2id / openid2id lookup caches, too.
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Wed, 12 Feb 2014 12:07:22 +0100
parents 7e7e1cbb9d3f
children
rev   line source
5105
3feeab009683 anywikidraw/twikidraw action: fix src encoding decl
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5103
diff changeset
1 # -*- coding: iso-8859-1 -*-
5058
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
2 """
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
3 MoinMoin - twikidraw
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
4
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
5 This action is used to call twikidraw
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
6
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
7 @copyright: 2001 by Ken Sugino (sugino@mediaone.net),
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
8 2001-2004 by Juergen Hermann <jh@web.de>,
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
9 2005 MoinMoin:AlexanderSchremmer,
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
10 2005 DiegoOngaro at ETSZONE (diego@etszone.com),
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
11 2007-2008 MoinMoin:ThomasWaldmann,
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
12 2005-2009 MoinMoin:ReimarBauer,
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
13 @license: GNU GPL, see COPYING for details.
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
14 """
5088
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
15 import os, re
5058
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
16
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
17 from MoinMoin import log
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
18 logging = log.getLogger(__name__)
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
19
5100
3920769287c4 twikidraw/anywikidraw action: fix nonascii drawing name handling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5098
diff changeset
20 from MoinMoin import wikiutil, config
5063
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
21 from MoinMoin.action import AttachFile, do_show
5103
6c620a7e2def anywikidraw/twikidraw action: reduce code duplication, cosmetic changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5102
diff changeset
22 from MoinMoin.action.AttachFile import _write_stream
5083
d963e742fdc7 TwikiDraw applet added textcha feature
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5066
diff changeset
23 from MoinMoin.security.textcha import TextCha
5058
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
24
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
25 action_name = __name__.split('.')[-1]
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
26
5063
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
27
5066
1008a0fca6a5 formatter.text_gedit: calls gedit_drawing instead of attachment_drawing.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5063
diff changeset
28 def gedit_drawing(self, url, text, **kw):
1008a0fca6a5 formatter.text_gedit: calls gedit_drawing instead of attachment_drawing.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5063
diff changeset
29 # This is called for displaying a drawing image by gui editor.
1008a0fca6a5 formatter.text_gedit: calls gedit_drawing instead of attachment_drawing.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5063
diff changeset
30 _ = self.request.getText
1008a0fca6a5 formatter.text_gedit: calls gedit_drawing instead of attachment_drawing.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5063
diff changeset
31 # TODO: this 'text' argument is kind of superfluous, replace by using alt=... kw arg
1008a0fca6a5 formatter.text_gedit: calls gedit_drawing instead of attachment_drawing.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5063
diff changeset
32 # ToDo: make this clickable for the gui editor
1008a0fca6a5 formatter.text_gedit: calls gedit_drawing instead of attachment_drawing.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5063
diff changeset
33 if 'alt' not in kw or not kw['alt']:
1008a0fca6a5 formatter.text_gedit: calls gedit_drawing instead of attachment_drawing.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5063
diff changeset
34 kw['alt'] = text
1008a0fca6a5 formatter.text_gedit: calls gedit_drawing instead of attachment_drawing.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5063
diff changeset
35 # we force the title here, needed later for html>wiki converter
1008a0fca6a5 formatter.text_gedit: calls gedit_drawing instead of attachment_drawing.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5063
diff changeset
36 kw['title'] = "drawing:%s" % wikiutil.quoteWikinameURL(url)
1008a0fca6a5 formatter.text_gedit: calls gedit_drawing instead of attachment_drawing.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5063
diff changeset
37 pagename, drawing = AttachFile.absoluteName(url, self.page.page_name)
5102
9ae242080889 drawings/attachments: made editing support more generic (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5101
diff changeset
38 containername = wikiutil.taintfilename(drawing)
9ae242080889 drawings/attachments: made editing support more generic (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5101
diff changeset
39 drawing_url = AttachFile.getAttachUrl(pagename, containername, self.request)
5066
1008a0fca6a5 formatter.text_gedit: calls gedit_drawing instead of attachment_drawing.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5063
diff changeset
40 ci = AttachFile.ContainerItem(self.request, pagename, containername)
1008a0fca6a5 formatter.text_gedit: calls gedit_drawing instead of attachment_drawing.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5063
diff changeset
41 if not ci.exists():
5106
fecbfc7f341d twikidraw/anywikidraw: reduce code duplication, make code more similar
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5105
diff changeset
42 title = _('Create new drawing "%(filename)s (opens in new window)"') % {'filename': self.text(containername)}
5066
1008a0fca6a5 formatter.text_gedit: calls gedit_drawing instead of attachment_drawing.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5063
diff changeset
43 img = self.icon('attachimg') # TODO: we need a new "drawimg" in similar grey style and size
1008a0fca6a5 formatter.text_gedit: calls gedit_drawing instead of attachment_drawing.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5063
diff changeset
44 css = 'nonexistent'
1008a0fca6a5 formatter.text_gedit: calls gedit_drawing instead of attachment_drawing.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5063
diff changeset
45 return self.url(1, drawing_url, css=css, title=title) + img + self.url(0)
5086
6910e50f3c6b drawings: always use the same member file names within .tdraw / .adraw
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5083
diff changeset
46 kw['src'] = ci.member_url('drawing.png')
5066
1008a0fca6a5 formatter.text_gedit: calls gedit_drawing instead of attachment_drawing.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5063
diff changeset
47 return self.image(**kw)
1008a0fca6a5 formatter.text_gedit: calls gedit_drawing instead of attachment_drawing.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5063
diff changeset
48
5103
6c620a7e2def anywikidraw/twikidraw action: reduce code duplication, cosmetic changes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5102
diff changeset
49
5061
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
50 def attachment_drawing(self, url, text, **kw):
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
51 # This is called for displaying a clickable drawing image by text_html formatter.
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
52 # XXX text arg is unused!
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
53 _ = self.request.getText
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
54 pagename, drawing = AttachFile.absoluteName(url, self.page.page_name)
5102
9ae242080889 drawings/attachments: made editing support more generic (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5101
diff changeset
55 containername = wikiutil.taintfilename(drawing)
5061
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
56
5102
9ae242080889 drawings/attachments: made editing support more generic (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5101
diff changeset
57 drawing_url = AttachFile.getAttachUrl(pagename, containername, self.request, do='modify')
5061
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
58 ci = AttachFile.ContainerItem(self.request, pagename, containername)
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
59 if not ci.exists():
5106
fecbfc7f341d twikidraw/anywikidraw: reduce code duplication, make code more similar
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5105
diff changeset
60 title = _('Create new drawing "%(filename)s (opens in new window)"') % {'filename': self.text(containername)}
5061
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
61 img = self.icon('attachimg') # TODO: we need a new "drawimg" in similar grey style and size
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
62 css = 'nonexistent'
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
63 return self.url(1, drawing_url, css=css, title=title) + img + self.url(0)
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
64
5106
fecbfc7f341d twikidraw/anywikidraw: reduce code duplication, make code more similar
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5105
diff changeset
65 title = _('Edit drawing %(filename)s (opens in new window)') % {'filename': self.text(containername)}
5086
6910e50f3c6b drawings: always use the same member file names within .tdraw / .adraw
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5083
diff changeset
66 kw['src'] = src = ci.member_url('drawing.png')
5061
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
67 kw['css'] = 'drawing'
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
68
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
69 try:
5086
6910e50f3c6b drawings: always use the same member file names within .tdraw / .adraw
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5083
diff changeset
70 mapfile = ci.get('drawing.map')
5061
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
71 map = mapfile.read()
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
72 mapfile.close()
5100
3920769287c4 twikidraw/anywikidraw action: fix nonascii drawing name handling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5098
diff changeset
73 map = map.decode(config.charset)
5061
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
74 except (KeyError, IOError, OSError):
5100
3920769287c4 twikidraw/anywikidraw action: fix nonascii drawing name handling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5098
diff changeset
75 map = u''
5061
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
76 if map:
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
77 # we have a image map. inline it and add a map ref to the img tag
5245
55de22afaff3 drawings: A unique ID for image maps is needed if drawings with the same name are included from different pages (bug fix for 1.9ImageMapsInDrawingsDontWork).
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5155
diff changeset
78 # we have also to set a unique ID
55de22afaff3 drawings: A unique ID for image maps is needed if drawings with the same name are included from different pages (bug fix for 1.9ImageMapsInDrawingsDontWork).
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5155
diff changeset
79 mapid = u'ImageMapOf%s%s' % (self.request.uid_generator(pagename), drawing)
5100
3920769287c4 twikidraw/anywikidraw action: fix nonascii drawing name handling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5098
diff changeset
80 map = map.replace(u'%MAPNAME%', mapid)
5061
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
81 # add alt and title tags to areas
5100
3920769287c4 twikidraw/anywikidraw action: fix nonascii drawing name handling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5098
diff changeset
82 map = re.sub(ur'href\s*=\s*"((?!%TWIKIDRAW%).+?)"', ur'href="\1" alt="\1" title="\1"', map)
5101
d8ccac2f24c5 getAttachUrl: removed escaped=... param (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5100
diff changeset
83 map = map.replace(u'%TWIKIDRAW%"', u'%s" alt="%s" title="%s"' % (
d8ccac2f24c5 getAttachUrl: removed escaped=... param (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5100
diff changeset
84 wikiutil.escape(drawing_url, 1), title, title))
5061
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
85 # unxml, because 4.01 concrete will not validate />
5100
3920769287c4 twikidraw/anywikidraw action: fix nonascii drawing name handling
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5098
diff changeset
86 map = map.replace(u'/>', u'>')
5106
fecbfc7f341d twikidraw/anywikidraw: reduce code duplication, make code more similar
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5105
diff changeset
87 title = _('Clickable drawing: %(filename)s') % {'filename': self.text(containername)}
5061
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
88 if 'title' not in kw:
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
89 kw['title'] = title
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
90 if 'alt' not in kw:
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
91 kw['alt'] = kw['title']
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
92 kw['usemap'] = '#'+mapid
5106
fecbfc7f341d twikidraw/anywikidraw: reduce code duplication, make code more similar
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5105
diff changeset
93 return self.url(1, drawing_url) + map + self.image(**kw) + self.url(0)
5061
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
94 else:
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
95 if 'title' not in kw:
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
96 kw['title'] = title
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
97 if 'alt' not in kw:
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
98 kw['alt'] = kw['title']
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
99 return self.url(1, drawing_url) + self.image(**kw) + self.url(0)
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
100
4d90b17cb7b1 formatter.text_html: moved attachment_drawing to the twikidraw action.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5058
diff changeset
101
5063
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
102 class TwikiDraw(object):
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
103 """ twikidraw action """
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
104 def __init__(self, request, pagename, target):
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
105 self.request = request
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
106 self.pagename = pagename
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
107 self.target = target
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
108
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
109 def save(self):
5107
26379ee2aa74 anywikidraw/twikidraw action: more cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5106
diff changeset
110 request = self.request
26379ee2aa74 anywikidraw/twikidraw action: more cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5106
diff changeset
111 _ = request.getText
5524
069f75c3d59c merged moin/1.8 + changes needed for ticket support of 1.9 drawings code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5268
diff changeset
112
069f75c3d59c merged moin/1.8 + changes needed for ticket support of 1.9 drawings code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5268
diff changeset
113 if not wikiutil.checkTicket(request, request.args.get('ticket', '')):
069f75c3d59c merged moin/1.8 + changes needed for ticket support of 1.9 drawings code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5268
diff changeset
114 return _('Please use the interactive user interface to use action %(actionname)s!') % {'actionname': 'twikidraw.save' }
069f75c3d59c merged moin/1.8 + changes needed for ticket support of 1.9 drawings code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5268
diff changeset
115
5063
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
116 pagename = self.pagename
5086
6910e50f3c6b drawings: always use the same member file names within .tdraw / .adraw
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5083
diff changeset
117 target = self.target
5058
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
118 if not request.user.may.write(pagename):
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
119 return _('You are not allowed to save a drawing on this page.')
5107
26379ee2aa74 anywikidraw/twikidraw action: more cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5106
diff changeset
120 if not target:
5268
02fc82fc062d twikidraw/anywikidraw action: improve error msgs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5245
diff changeset
121 return _("Empty target name given.")
5063
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
122
5058
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
123 file_upload = request.files.get('filepath')
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
124 if not file_upload:
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
125 # This might happen when trying to upload file names
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
126 # with non-ascii characters on Safari.
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
127 return _("No file content. Delete non ASCII characters from the file name and try again.")
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
128
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
129 filename = request.form['filename']
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
130 basepath, basename = os.path.split(filename)
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
131 basename, ext = os.path.splitext(basename)
5102
9ae242080889 drawings/attachments: made editing support more generic (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5101
diff changeset
132 ci = AttachFile.ContainerItem(request, pagename, target)
5058
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
133 filecontent = file_upload.stream
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
134 content_length = None
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
135 if ext == '.draw': # TWikiDraw POSTs this first
5102
9ae242080889 drawings/attachments: made editing support more generic (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5101
diff changeset
136 AttachFile._addLogEntry(request, 'ATTDRW', pagename, target)
5058
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
137 ci.truncate()
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
138 filecontent = filecontent.read() # read file completely into memory
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
139 filecontent = filecontent.replace("\r", "")
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
140 elif ext == '.map':
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
141 # touch attachment directory to invalidate cache if new map is saved
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
142 attach_dir = AttachFile.getAttachDir(request, pagename)
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
143 os.utime(attach_dir, None)
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
144 filecontent = filecontent.read() # read file completely into memory
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
145 filecontent = filecontent.strip()
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
146 else:
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
147 #content_length = file_upload.content_length
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
148 # XXX gives -1 for wsgiref :( If this is fixed, we could use the file obj,
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
149 # without reading it into memory completely:
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
150 filecontent = filecontent.read()
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
151
5086
6910e50f3c6b drawings: always use the same member file names within .tdraw / .adraw
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5083
diff changeset
152 ci.put('drawing' + ext, filecontent, content_length)
5058
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
153
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
154
5063
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
155 def render(self):
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
156 request = self.request
5107
26379ee2aa74 anywikidraw/twikidraw action: more cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5106
diff changeset
157 _ = request.getText
5063
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
158 pagename = self.pagename
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
159 target = self.target
5107
26379ee2aa74 anywikidraw/twikidraw action: more cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5106
diff changeset
160 if not request.user.may.read(pagename):
26379ee2aa74 anywikidraw/twikidraw action: more cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5106
diff changeset
161 return _('You are not allowed to view attachments of this page.')
26379ee2aa74 anywikidraw/twikidraw action: more cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5106
diff changeset
162 if not target:
5268
02fc82fc062d twikidraw/anywikidraw action: improve error msgs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5245
diff changeset
163 return _("Empty target name given.")
5107
26379ee2aa74 anywikidraw/twikidraw action: more cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5106
diff changeset
164
5102
9ae242080889 drawings/attachments: made editing support more generic (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5101
diff changeset
165 ci = AttachFile.ContainerItem(request, pagename, target)
5088
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
166 if ci.exists():
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
167 drawurl = ci.member_url('drawing.draw')
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
168 pngurl = ci.member_url('drawing.png')
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
169 else:
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
170 drawurl = 'drawing.draw'
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
171 pngurl = 'drawing.png'
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
172 pageurl = request.href(pagename)
5524
069f75c3d59c merged moin/1.8 + changes needed for ticket support of 1.9 drawings code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5268
diff changeset
173 saveurl = request.href(pagename, action=action_name, do='save', target=target,
069f75c3d59c merged moin/1.8 + changes needed for ticket support of 1.9 drawings code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5268
diff changeset
174 ticket=wikiutil.createTicket(request))
5088
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
175 helpurl = request.href("HelpOnActions/AttachFile")
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
176
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
177 html = """
5058
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
178 <p>
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
179 <applet code="CH.ifa.draw.twiki.TWikiDraw.class"
5088
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
180 archive="%(htdocs)s/applets/TWikiDrawPlugin/twikidraw.jar" width="640" height="480">
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
181 <param name="drawpath" value="%(drawurl)s">
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
182 <param name="pngpath" value="%(pngurl)s">
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
183 <param name="savepath" value="%(saveurl)s">
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
184 <param name="basename" value="%(basename)s">
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
185 <param name="viewpath" value="%(pageurl)s">
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
186 <param name="helppath" value="%(helpurl)s">
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
187 <strong>NOTE:</strong> You need a Java enabled browser to edit the drawing.
5058
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
188 </applet>
5088
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
189 </p>
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
190 """ % dict(
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
191 htdocs=request.cfg.url_prefix_static,
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
192 basename=wikiutil.escape(target, 1),
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
193 drawurl=wikiutil.escape(drawurl, 1),
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
194 pngurl=wikiutil.escape(pngurl, 1),
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
195 pageurl=wikiutil.escape(pageurl, 1),
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
196 saveurl=wikiutil.escape(saveurl, 1),
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
197 helpurl=wikiutil.escape(helpurl, 1),
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
198 )
5058
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
199
5088
a2a51a2ebd6e twikidraw/anywikidraw action: make code more similar, clean up
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5086
diff changeset
200 title = "%s %s:%s" % (_("Edit drawing"), pagename, target)
5063
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
201 request.theme.send_title(title, page=request.page, pagename=pagename)
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
202 request.write(request.formatter.startContent("content"))
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
203 request.write(request.formatter.rawHTML(html))
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
204 request.write(request.formatter.endContent())
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
205 request.theme.send_footer(pagename)
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
206 request.theme.send_closing_html()
5058
b986f014dc71 refactored drawing TwikiDraw to a configurable action
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
207
5063
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
208
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
209 def execute(pagename, request):
5107
26379ee2aa74 anywikidraw/twikidraw action: more cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5106
diff changeset
210 target = request.values.get('target')
5910
7e7e1cbb9d3f security: fix remote code execution vulnerability in twikidraw/anywikidraw actions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5524
diff changeset
211 target = wikiutil.taintfilename(target)
7e7e1cbb9d3f security: fix remote code execution vulnerability in twikidraw/anywikidraw actions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5524
diff changeset
212
5107
26379ee2aa74 anywikidraw/twikidraw action: more cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5106
diff changeset
213 twd = TwikiDraw(request, pagename, target)
5063
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
214
5107
26379ee2aa74 anywikidraw/twikidraw action: more cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5106
diff changeset
215 do = request.values.get('do')
26379ee2aa74 anywikidraw/twikidraw action: more cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5106
diff changeset
216 if do == 'save':
26379ee2aa74 anywikidraw/twikidraw action: more cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5106
diff changeset
217 msg = twd.save()
26379ee2aa74 anywikidraw/twikidraw action: more cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5106
diff changeset
218 else:
26379ee2aa74 anywikidraw/twikidraw action: more cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5106
diff changeset
219 msg = twd.render()
26379ee2aa74 anywikidraw/twikidraw action: more cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5106
diff changeset
220 if msg:
26379ee2aa74 anywikidraw/twikidraw action: more cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5106
diff changeset
221 request.theme.add_msg(msg, 'error')
26379ee2aa74 anywikidraw/twikidraw action: more cleanup
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5106
diff changeset
222 do_show(pagename, request)
5063
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
223
bcb42686c991 action.twikidraw: refactored to an object
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5062
diff changeset
224