Attachment 'gallery2image-1.5.1-12.py'
Download 1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - gallery2Image Action macro
4
5 PURPOSE::
6 This action macro is used to rotate, move to bak or to slide through the images from Gallery2
7
8 CALLING SEQUENCE::
9 called by Gallery2 POST Method
10
11 PROCEDURE::
12 see Gallery2
13
14 Please remove the version number from this file
15
16 RESTRICTIONS::
17 spaces in file names are not supported. I don't know how to escape them right. Probaly this does work in 1.3.5
18
19 slide show is best used with fastcgi if you have many images
20
21 MODIFICATION HISTORY::
22 Version 1.3.3.-1
23 @copyright: 2005 by Reimar Bauer (R.Bauer@fz-juelich.de)
24 @license: GNU GPL, see COPYING for details.
25 2005-06-24: 1.3.3.-2 feature reqeust of CraigJohnson added
26 os.path.join used to join platform independent pathes
27 os.unlink removed to get it more platform independend
28 2005-08-06: 1.3.5-3 RB VS mode added
29 by one step back or forward could be toggled through the selected slides
30 and the first and last one could be selected too
31 2005-08-07 1.3.5-4 RB bug fixed for cgi-bin call. formatting of tables adjusted
32 2005-08-13 1.3.5-5 RB code change from GET to POST
33 forms instead of link
34 toggle between webnail and image by click on image
35 alias (description) and exif_date added
36 this version needs Gallery2-1.3.5-7.py
37 2005-08-31 1.3.5-6 RB html code changed into a function :-)
38 some html bugs fixed too
39 instead of button text now images used (disabled buttons are grey color coded)
40 back link to callers page
41 whole page inserted into the common wiki page
42 code clean up.
43 2005-09-01 1.3.5-7 RB image paths changed to absoulte url
44 2005-10-29 1.3.5-8 RB the code of VS rewritten
45 for the slideshow changed to a javascript player learned from Ricocheting
46 image preloading for webnails and fullimages because webspeed could be slow
47 slide show is really fast by this
48 default duration is 1000msec
49 (runs under 1.5.0 too)
50 2005-11-01 1.3.5-9 RB output buttons changed to <SPAN id> text
51 to all javascript functions added the prefix gallery2
52 some fixes on javascript code
53 controls separated.
54 2005-11-12 1.3.5-10 RB alias text is displayed by innerHTML
55 bug fixed ' is allowed to use in alias text
56 wish of grey out buttons for first or last slide button added
57 span id's changed to a prefix of gallery2_
58 2005-11-25 1.3.5-11 RB bug fixed of selecting an index of a scalar value
59 (It's a python wonder that it has worked this way)
60 2005-01-27 1.5.1-12 RB switch of release number to MoinMoin Version 1.5
61 duplicated call of html tag body and head removed
62
63
64
65 """
66 Dependencies = []
67 import os,string,Image,StringIO
68 from MoinMoin import config, wikiutil, version
69 from MoinMoin.PageEditor import PageEditor
70 from MoinMoin import user, util
71 from MoinMoin.Page import Page
72 from MoinMoin.action import AttachFile
73 from MoinMoin.formatter.text_html import Formatter
74 from MoinMoin.parser import wiki
75
76
77 def option_list(this_image,pagename,text,request):
78 txt = ''
79
80 for s in text:
81 name = AttachFile.getAttachUrl(pagename,s,request)
82 if name == this_image:
83 txt += '<option selected value="%(name)s">%(alias)s' % {
84 "name":name,
85 "alias":s}
86
87 else:
88 txt += '<option value="%(name)s">%(alias)s' % {
89 "name":name,
90 "alias":s}
91
92 return txt
93
94 def to_htmltext(text):
95
96 if text.find ("'"):
97 text = text.split("'")
98 text = '''.join(text)
99
100 return text
101
102 def html_js(this_image,counter):
103 html = '''
104 <SCRIPT LANGUAGE="JavaScript">
105 <!-- Original: Ricocheting (ricocheting@hotmail.com) -->
106 <!-- Web Site: http://www.ricocheting.com -->
107
108 <!-- This script and many more are available free online at -->
109 <!-- The JavaScript Source!! http://javascript.internet.com -->
110
111 <!-- Modifications by Reimar Bauer <R.Bauer@fz-juelich.de> -->
112 <!-- 2005-10-29 -->
113 <!-- Many thanks to Ricocheting, it is much easier as my own one. I like it -->
114 <!-- Some code added and replaced for the MoinMoin:Gallery2 parser-->
115
116
117 <!-- Begin
118
119 var rotate_delay = 1000; // delay in milliseconds (5000 = 5 secs)
120 var current = %(counter)s;
121 var theImages = new Array();
122 var thewebImages = new Array();
123 var thefullImages = new Array();
124
125 function gallery2preload() {
126 var list = document.slideform.webnail_list.value;
127 var value = list.split(",");
128 var n = value.length;
129
130 for (i = 0; i < n-1; i++){
131 theImages[i] = new Image();
132 theImages[i].src = value[i];
133 }
134 thewebImages = theImages;
135
136 var list = document.slideform.full_list.value;
137 var value = list.split(",");
138 var n = value.length;
139
140 for (i = 0; i < n-1; i++){
141 thefullImages[i] = new Image();
142 thefullImages[i].src = value[i];
143 }
144
145 }
146
147 function gallery2getserver() {
148 var value = document.URL;
149 var text = value.split("/");
150 return text[0]+'//'+text[2];
151 }
152
153 function gallery2preload_finished() {
154 var url = gallery2getserver()+'/wiki/modern/img/star_on.png';
155 return url;
156 }
157
158 function gallery2add_comments() {
159 var alias_text = document.slideform.alias.value;
160 var exif_date_text = document.slideform.exif_date.value;
161 var index = document.slideform.slide.selectedIndex;
162 var alias = alias_text.split("!,!");
163 var exif = exif_date_text.split(",");
164 document.getElementById("gallery2_alias_text").innerHTML = alias[index];
165 document.getElementById("gallery2_exif_date_text").innerHTML = exif[index];
166 }
167
168 function gallery2next_slide() {
169 if (document.slideform.slide[current+1]) {
170 document.images.show.src = theImages[current+1].src;
171 document.slideform.slide.selectedIndex = ++current;
172 gallery2add_comments();
173 }
174 else gallery2first_slide();
175 document.getElementById("gallery2_first_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/modern/img/first.png" onclick="gallery2first_slide();" name="fs" title="first slide" >';
176 document.getElementById("gallery2_last_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/modern/img/last.png" onclick="gallery2last_slide();" name="fs" title="last slide" >';
177 }
178 function gallery2previous_slide() {
179 if (current-1 >= 0) {
180 document.images.show.src = theImages[current-1].src;
181 document.slideform.slide.selectedIndex = --current;
182 gallery2add_comments();
183
184 }
185 else gallery2last_slide();
186
187 document.getElementById("gallery2_first_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/modern/img/first.png" onclick="gallery2first_slide();" name="fs" title="first slide" >';
188 document.getElementById("gallery2_last_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/modern/img/last.png" onclick="gallery2last_slide();" name="fs" title="last slide" >';
189 }
190 function gallery2first_slide() {
191 current = 0;
192 document.images.show.src = theImages[0].src;
193 document.slideform.slide.selectedIndex = 0;
194 gallery2add_comments();
195 document.getElementById("gallery2_first_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/modern/img/first_disabled.png" title="first slide" >';
196 document.getElementById("gallery2_last_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/modern/img/last.png" onclick="gallery2last_slide();" name="fs" title="last slide" >';
197 }
198 function gallery2last_slide() {
199 current = document.slideform.slide.length-1;
200 document.images.show.src = theImages[current].src;
201 document.slideform.slide.selectedIndex = current;
202 gallery2add_comments();
203 document.getElementById("gallery2_first_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/modern/img/first.png" onclick="gallery2first_slide();" name="fs" title="first slide" >';
204 document.getElementById("gallery2_last_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/modern/img/last_disabled.png" title="last slide" >';
205 }
206
207 function gallery2switch_images() {
208 if (document.slideform.flag.value == "webnail") {
209 var list = document.slideform.full_list.value;
210 var name = document.slideform.full_name.value;
211 document.slideform.flag.value = "fullscreen";
212 theImages = thefullImages;
213 } else {
214 var list = document.slideform.webnail_list.value;
215 var name = document.slideform.webnail_name.value;
216 document.slideform.flag.value = "webnail";
217 theImages = thewebImages;
218 }
219
220 var value = list.split(",");
221 var alias = name.split(",");
222
223 var n = value.length;
224
225 for (i = 0; i < n-1; i++){
226 document.slideform.slide[i].value=value[i];
227 document.slideform.slide[i].text=alias[i];
228 }
229 document.images.show.src = theImages[current].src;
230 }
231
232 function gallery2ap(text) {
233 document.slideform.slidebutton.value = (text == "Stop") ? "Start" : "Stop";
234 gallery2rotate();
235 }
236
237 function gallery2change() {
238 current = document.slideform.slide.selectedIndex;
239 document.images.show.src = theImages[current].src;
240 gallery2add_comments();
241 document.getElementById("gallery2_first_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/modern/img/first.png" onclick="gallery2first_slide();" name="fs" title="first slide" >';
242 document.getElementById("gallery2_last_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/modern/img/last.png" onclick="gallery2last_slide();" name="fs" title="last slide" >';
243 }
244
245 function gallery2rotate() {
246 if (document.slideform.slidebutton.value == "Stop") {
247 current = (current == document.slideform.slide.length-1) ? 0 : current+1;
248 document.images.show.src = theImages[current].src;
249 document.slideform.slide.selectedIndex = current;
250 gallery2add_comments();
251 document.getElementById("gallery2_first_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/modern/img/first.png" onclick="gallery2first_slide();" name="fs" title="first slide" >';
252 document.getElementById("gallery2_last_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/modern/img/last.png" onclick="gallery2last_slide();" name="fs" title="last slide" >';
253 rotate_delay = document.slideform.duration.value;
254 window.setTimeout("gallery2rotate()", rotate_delay);
255 }
256 }
257 // End -->
258 </script>
259 '''% {
260 'counter':counter}
261
262 return html
263
264 def html_show_image(request,pagename,url_wiki_page,full,alias,exif_date,target,idx):
265
266 option_webnail = option_list(AttachFile.getAttachUrl(pagename,target[idx],request),pagename,target,request)
267
268 this_full_list = ''
269 for s in full:
270 this_full_list += AttachFile.getAttachUrl(pagename,s,request)+','
271
272 this_webnail_list = ''
273 for s in target:
274 this_webnail_list += AttachFile.getAttachUrl(pagename,s,request)+','
275
276 html = '''
277 <left>
278 <form name=slideform method="POST">
279 <input type="hidden" name="full_list" value='%(this_full_list)s'>
280 <input type="hidden" name="full_name" value='%(this_full_name)s'>
281 <input type="hidden" name="flag" value="webnail">
282 <input type="hidden" name="webnail_list" value='%(this_webnail_list)s'>
283 <input type="hidden" name="webnail_name" value='%(this_webnail_name)s'>
284 <input type="hidden" name="alias" value='%(this_alias_list)s'>
285 <input type="hidden" name="exif_date" value='%(this_exif_date_list)s'>
286 <BR>
287 <table><tr><td bgcolor="#C0C0C0"><strong>Slide: </strong></td>
288 <td bgcolor="#C0C0C0">
289 <select name="slide" onChange="gallery2change();" >
290 %(option_webnails)s
291 </select>
292 </td>
293 <td bgcolor="#C0C0C0">
294 <input type="button" name="slidebutton" onClick="gallery2ap(this.value);" value="Start" title="AutoPlay">
295 </td>
296 <td bgcolor="#C0C0C0">
297 <strong>Duration: </strong>
298 </td>
299 <td bgcolor="#C0C0C0">
300 <input type="input" name="duration" value="1000" size="5" title="Duration">
301 </td>
302 <td bgcolor="#C0C0C0" align="left" >
303 <P><SPAN id="gallery2_preloadstatus"></SPAN></P>
304 </td>
305
306 </tr>
307 </table>
308 <BR>
309 <table align="center" cellspacing=1 cellpadding=4 bgcolor="#000000">
310 <tr>
311 <td align="center" bgcolor="white">
312 </td>
313 </tr>
314 <tr>
315
316 <td align="center" bgcolor="#C0C0C0">
317 <SPAN id="gallery2_first_slide"><img src="%(server)s/wiki/modern/img/first.png" onclick="gallery2first_slide();" name="fs" title="first slide" ></SPAN>
318 <img src="%(server)s/wiki/modern/img/previous.png" onclick="gallery2previous_slide();" title="previous slide" >
319 <img src="%(server)s/wiki/modern/img/next.png" onClick="gallery2next_slide();" title="next slide" >
320 <SPAN id="gallery2_last_slide"><img src="%(server)s/wiki/modern/img/last.png" onClick="gallery2last_slide();" title="last slide" ></SPAN>
321 <input type="image" value="submit" src="%(server)s/wiki/modern/img/back.png" title="return to %(pagename)s">
322 </td>
323 </tr>
324 <tr>
325 <td align="center" bgcolor="white"">
326 <img src="%(this_image)s" name="show" onClick="gallery2switch_images();">
327 </td>
328 </tr>
329 <tr valign="center">
330 <td bgcolor="#C0C0C0" align="left">
331 <P><SPAN id="gallery2_alias_text">%(this_alias_text)s</SPAN></P>
332 </td></tr>
333 <tr valign="center">
334 <td bgcolor="#C0C0C0" align="left">
335 <P><SPAN id="gallery2_exif_date_text">%(this_exif_date_text)s</SPAN></P>
336 </td>
337 </tr>
338 <tr>
339 <td align="center" bgcolor="white">
340 </td>
341 </tr>
342 </table>
343 <BR>
344 <table>
345 <tr>
346 <td bgcolor="#C0C0C0" align="left" >
347 <P><SPAN id="gallery2_status">%(comment)s</SPAN></P>
348 </td>
349 </tr>
350 </table>
351 </form>
352 </center>
353 ''' % {
354 "server" : 'http://'+request.server_name+':' + str(request.server_port),
355 "base_url" : request.getScriptname(),
356
357 "this_full_list" : this_full_list,
358 "this_full_name" : string.join(full,','),
359 "this_webnail_list" : this_webnail_list,
360 "this_webnail_name" : string.join(target,','),
361
362 "this_alias_text": to_htmltext(alias[idx]),
363 "this_alias_list" : to_htmltext(string.join(alias,'!,!')),
364 "this_exif_date_text": exif_date[idx],
365 "this_exif_date_list" :string.join(exif_date,','),
366
367 "this_image" : AttachFile.getAttachUrl(pagename,target[idx],request),
368 "url_wiki_page" : url_wiki_page,
369 "comment" : "Click on the image for the next higher/lower resolution",
370 "pagename" : pagename,
371 "option_webnails" : option_webnail
372 }
373
374 return html
375
376 def to_wikiname(request,text):
377
378 #taken from test_parser_wiki
379 page = Page(request, 'ThisPageDoesNotExistsAndWillNeverBeReally')
380 page.formatter = Formatter(request)
381 request.formatter = page.formatter
382 page.formatter.setPage(page)
383 page.hilite_re = None
384
385 out=StringIO.StringIO()
386 request.redirect(out)
387 wikiizer = wiki.Parser(text.strip(),request)
388 wikiizer.format(page.formatter)
389 result = out.getvalue()
390 request.redirect()
391 del out
392
393 if version.release < '1.5.0' :
394 return result.strip()
395 else:
396 result = result.strip()
397 result = result.replace('<a id="line-1"></a><p>','')
398 result = result.replace('</p>','')
399 return result
400
401
402 action_name = __name__.split('.')[-1]
403
404 def execute(pagename, request):
405 """ Main dispatcher for the 'Gallery' action.
406 """
407 _ = request.getText
408
409 request.formatter = Formatter(request)
410 attachment_path = AttachFile.getAttachDir(request,pagename)
411 if request.form['do'][0] == 'VS' :
412 web = {}
413 images = string.split(request.form['target'][0],',')
414 target = images[0]
415 images = (images[1:])
416
417 full_image = string.split(request.form['full'][0],',')
418 full_target = full_image[0]
419 full_image = (full_image[1:])
420
421 all_description = string.split(request.form['alias'][0],'!,!')
422 this_description = all_description[0]
423 all_description = (all_description[1:])
424
425
426
427 all_exif_date = string.split(request.form['exif_date'][0],',')
428 this_exif_date = all_exif_date[0]
429 all_exif_date = (all_exif_date[1:])
430
431 z = 0
432 for img in images :
433 if target == img :
434 idx = z
435
436 z += 1
437 n = len(images)
438
439 ######## url_wiki_page #############################################################
440 text = pagename
441 url = pagename
442 url_wiki_page = wikiutil.link_tag(request, url, text = text,
443 formatter = request.formatter)
444
445 ############################################################################
446
447 attachment_path = AttachFile.getAttachDir(request,pagename)
448
449 web['src'] = AttachFile.getAttachUrl(pagename,target,request)
450 web['title'] = target
451 #web['width']="1024"
452
453 image_link=request.formatter.image(**web)
454
455 request.http_headers()
456
457 request.setContentLanguage(request.lang)
458
459 wikiutil.send_title(request, pagename,
460 pagename = pagename,
461 body_onload = "gallery2preload();",
462 html_head = html_js(AttachFile.getAttachUrl(pagename,target,request),idx))
463
464 request.write(request.formatter.startContent("content"))
465
466 html = html_show_image(request,pagename,url_wiki_page,full_image,all_description,all_exif_date,images,idx)
467
468 request.write(html)
469 request.write(request.formatter.endContent())
470 wikiutil.send_footer(request, pagename)
471 msg = None
472
473
474 elif request.form['do'][0] == 'PS' :
475 msg = None
476
477 elif request.form['do'][0] == 'BS' :
478 msg = "gone back" #None
479
480 elif request.user.may.delete(pagename):
481 # only users which are allowed to delete should use this tool
482
483 target=request.form['target'][0]
484 file, ext = os.path.splitext(target)
485
486 if ext == '.gif' or ext == '.png' :
487 img_type = 'PNG'
488 thumbfile = "thumbnail_%(file)s.png" % {"file" : file}
489 webnail = "webnail_%(file)s.png" % {"file" : file}
490 else:
491 img_type="JPEG"
492 thumbfile="thumbnail_%(file)s.jpg" % {"file" : file}
493 webnail="webnail_%(file)s.jpg" % {"file" : file}
494
495 thumb = os.path.join(attachment_path,thumbfile)
496 webf = os.path.join(attachment_path,webnail)
497 infile = os.path.join(attachment_path,target)
498
499 msg = None
500 if version.release < '1.5.0' :
501 if action_name in request.cfg.excluded_actions:
502 msg = _('File attachments are not allowed in this wiki!')
503
504 elif request.form['do'][0] == 'RM' :
505 if os.path.exists(infile + '.bak') :
506 os.unlink("%(file)s.bak" % {"file" : infile})
507 os.link(infile,"%(file)s.bak" % {"file" : infile})
508 os.unlink(infile)
509 os.unlink(webf)
510 os.unlink(thumb)
511 msg = _('%(target)s deleted, backup in place' % {
512 'target':target})
513
514 elif request.form['do'][0] == 'RL' :
515 im = Image.open(infile)
516 #os.unlink(infile)
517 im.rotate(90).save(infile,img_type)
518 nim = Image.open(infile)
519 nim.thumbnail((640,640), Image.ANTIALIAS)
520 #os.unlink(webf)
521 nim.save(webf, img_type)
522 nim.thumbnail((128, 128), Image.ANTIALIAS)
523 #os.unlink(thumb)
524 nim.save(thumb, img_type)
525 msg= _('%(target)s rotated to left 90 degrees' % {
526 'target':target})
527
528 elif request.form['do'][0] == 'RR' :
529 im = Image.open(infile)
530 #os.unlink(infile)
531 im.rotate(270).save(infile,img_type)
532
533 nim = Image.open(infile)
534 nim.thumbnail((640,640), Image.ANTIALIAS)
535 #os.unlink(webf)
536 nim.save(webf, img_type)
537 nim.thumbnail((128, 128), Image.ANTIALIAS)
538 #os.unlink(thumb)
539 nim.save(thumb, img_type)
540 msg= _('%(target)s rotated to right 90 degrees' % {
541 'target':target})
542
543 else:
544 msg = _('action not implemented: %s') % (request.form['do'][0],)
545 else:
546 msg = _('Your are not allowed to change images on this page: %s') % (pagename,)
547
548 if msg:
549 AttachFile.error_msg(pagename, request, msg)
550
551 return()
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.