1
# -*- coding: iso-8859-1 -*-
5
This action is used to call twikidraw
7
@copyright: 2001 by Ken Sugino (sugino@mediaone.net),
8
2001-2004 by Juergen Hermann <jh@web.de>,
9
2005 MoinMoin:AlexanderSchremmer,
10
2005 DiegoOngaro at ETSZONE (diego@etszone.com),
11
2007-2008 MoinMoin:ThomasWaldmann,
12
2005-2009 MoinMoin:ReimarBauer,
13
@license: GNU GPL, see COPYING for details.
17
from MoinMoin import log
18
logging = log.getLogger(__name__)
20
from MoinMoin import wikiutil, config
21
from MoinMoin.action import AttachFile, do_show
22
from MoinMoin.action.AttachFile import _write_stream
23
from MoinMoin.security.textcha import TextCha
25
action_name = __name__.split('.')[-1]
28
def gedit_drawing(self, url, text, **kw):
29
# This is called for displaying a drawing image by gui editor.
30
_ = self.request.getText
31
# TODO: this 'text' argument is kind of superfluous, replace by using alt=... kw arg
32
# ToDo: make this clickable for the gui editor
33
if 'alt' not in kw or not kw['alt']:
35
# we force the title here, needed later for html>wiki converter
36
kw['title'] = "drawing:%s" % wikiutil.quoteWikinameURL(url)
37
pagename, drawing = AttachFile.absoluteName(url, self.page.page_name)
38
containername = wikiutil.taintfilename(drawing)
39
drawing_url = AttachFile.getAttachUrl(pagename, containername, self.request)
40
ci = AttachFile.ContainerItem(self.request, pagename, containername)
42
title = _('Create new drawing "%(filename)s (opens in new window)"') % {'filename': self.text(containername)}
43
img = self.icon('attachimg') # TODO: we need a new "drawimg" in similar grey style and size
45
return self.url(1, drawing_url, css=css, title=title) + img + self.url(0)
46
kw['src'] = ci.member_url('drawing.png')
47
return self.image(**kw)
50
def attachment_drawing(self, url, text, **kw):
51
# This is called for displaying a clickable drawing image by text_html formatter.
52
# XXX text arg is unused!
53
_ = self.request.getText
54
pagename, drawing = AttachFile.absoluteName(url, self.page.page_name)
55
containername = wikiutil.taintfilename(drawing)
57
drawing_url = AttachFile.getAttachUrl(pagename, containername, self.request, do='modify')
58
ci = AttachFile.ContainerItem(self.request, pagename, containername)
60
title = _('Create new drawing "%(filename)s (opens in new window)"') % {'filename': self.text(containername)}
61
img = self.icon('attachimg') # TODO: we need a new "drawimg" in similar grey style and size
63
return self.url(1, drawing_url, css=css, title=title) + img + self.url(0)
65
title = _('Edit drawing %(filename)s (opens in new window)') % {'filename': self.text(containername)}
66
kw['src'] = src = ci.member_url('drawing.png')
70
mapfile = ci.get('drawing.map')
73
map = map.decode(config.charset)
74
except (KeyError, IOError, OSError):
77
# we have a image map. inline it and add a map ref to the img tag
78
# we have also to set a unique ID
79
mapid = u'ImageMapOf%s%s' % (self.request.uid_generator(pagename), drawing)
80
map = map.replace(u'%MAPNAME%', mapid)
81
# add alt and title tags to areas
82
map = re.sub(ur'href\s*=\s*"((?!%TWIKIDRAW%).+?)"', ur'href="\1" alt="\1" title="\1"', map)
83
map = map.replace(u'%TWIKIDRAW%"', u'%s" alt="%s" title="%s"' % (
84
wikiutil.escape(drawing_url, 1), title, title))
85
# unxml, because 4.01 concrete will not validate />
86
map = map.replace(u'/>', u'>')
87
title = _('Clickable drawing: %(filename)s') % {'filename': self.text(containername)}
91
kw['alt'] = kw['title']
92
kw['usemap'] = '#'+mapid
93
return self.url(1, drawing_url) + map + self.image(**kw) + self.url(0)
98
kw['alt'] = kw['title']
99
return self.url(1, drawing_url) + self.image(**kw) + self.url(0)
102
class TwikiDraw(object):
103
""" twikidraw action """
104
def __init__(self, request, pagename, target):
105
self.request = request
106
self.pagename = pagename
110
request = self.request
113
if not wikiutil.checkTicket(request, request.args.get('ticket', '')):
114
return _('Please use the interactive user interface to use action %(actionname)s!') % {'actionname': 'twikidraw.save' }
116
pagename = self.pagename
118
if not request.user.may.write(pagename):
119
return _('You are not allowed to save a drawing on this page.')
121
return _("Empty target name given.")
123
file_upload = request.files.get('filepath')
125
# This might happen when trying to upload file names
126
# with non-ascii characters on Safari.
127
return _("No file content. Delete non ASCII characters from the file name and try again.")
129
filename = request.form['filename']
130
basepath, basename = os.path.split(filename)
131
basename, ext = os.path.splitext(basename)
132
ci = AttachFile.ContainerItem(request, pagename, target)
133
filecontent = file_upload.stream
134
content_length = None
135
if ext == '.draw': # TWikiDraw POSTs this first
136
AttachFile._addLogEntry(request, 'ATTDRW', pagename, target)
138
filecontent = filecontent.read() # read file completely into memory
139
filecontent = filecontent.replace("\r", "")
141
# touch attachment directory to invalidate cache if new map is saved
142
attach_dir = AttachFile.getAttachDir(request, pagename)
143
os.utime(attach_dir, None)
144
filecontent = filecontent.read() # read file completely into memory
145
filecontent = filecontent.strip()
147
#content_length = file_upload.content_length
148
# XXX gives -1 for wsgiref :( If this is fixed, we could use the file obj,
149
# without reading it into memory completely:
150
filecontent = filecontent.read()
152
ci.put('drawing' + ext, filecontent, content_length)
156
request = self.request
158
pagename = self.pagename
160
if not request.user.may.read(pagename):
161
return _('You are not allowed to view attachments of this page.')
163
return _("Empty target name given.")
165
ci = AttachFile.ContainerItem(request, pagename, target)
167
drawurl = ci.member_url('drawing.draw')
168
pngurl = ci.member_url('drawing.png')
170
drawurl = 'drawing.draw'
171
pngurl = 'drawing.png'
172
pageurl = request.href(pagename)
173
saveurl = request.href(pagename, action=action_name, do='save', target=target,
174
ticket=wikiutil.createTicket(request))
175
helpurl = request.href("HelpOnActions/AttachFile")
179
<applet code="CH.ifa.draw.twiki.TWikiDraw.class"
180
archive="%(htdocs)s/applets/TWikiDrawPlugin/twikidraw.jar" width="640" height="480">
181
<param name="drawpath" value="%(drawurl)s">
182
<param name="pngpath" value="%(pngurl)s">
183
<param name="savepath" value="%(saveurl)s">
184
<param name="basename" value="%(basename)s">
185
<param name="viewpath" value="%(pageurl)s">
186
<param name="helppath" value="%(helpurl)s">
187
<strong>NOTE:</strong> You need a Java enabled browser to edit the drawing.
191
htdocs=request.cfg.url_prefix_static,
192
basename=wikiutil.escape(target, 1),
193
drawurl=wikiutil.escape(drawurl, 1),
194
pngurl=wikiutil.escape(pngurl, 1),
195
pageurl=wikiutil.escape(pageurl, 1),
196
saveurl=wikiutil.escape(saveurl, 1),
197
helpurl=wikiutil.escape(helpurl, 1),
200
title = "%s %s:%s" % (_("Edit drawing"), pagename, target)
201
request.theme.send_title(title, page=request.page, pagename=pagename)
202
request.write(request.formatter.startContent("content"))
203
request.write(request.formatter.rawHTML(html))
204
request.write(request.formatter.endContent())
205
request.theme.send_footer(pagename)
206
request.theme.send_closing_html()
209
def execute(pagename, request):
210
target = request.values.get('target')
211
twd = TwikiDraw(request, pagename, target)
213
do = request.values.get('do')
219
request.theme.add_msg(msg, 'error')
220
do_show(pagename, request)