Attachment 'gallery2image-1.6.0-15.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
59 Version 1.5.4-13 2006-08-02 based on 1.3.5-10
60 FlorianFesti: changed some calls to more readable code
61 ReimarBauer: PEP 8
62 fixed of selecting an index of a scalar value
63 switch of release number to MoinMoin Version 1.5
64 duplicated call of html tag body and head removed
65 optional static path
66 table style changed
67
68 2006-08-11 1.5.4-14 RB bug fixed webf and thumbf changed to tmp. prefix always
69
70 2006-08-11 1.6.0-15 RB ported to 1.6.0 alpha version
71
72 """
73 Dependencies = []
74 import os, Image, StringIO, string, sys
75 from MoinMoin import config, wikiutil, version
76 from MoinMoin.PageEditor import PageEditor
77 from MoinMoin import user, util
78 from MoinMoin.Page import Page
79 from MoinMoin.action import AttachFile
80 from MoinMoin.formatter.text_html import Formatter
81 from MoinMoin.parser import text_moin_wiki
82 from MoinMoin.util import filesys
83
84 def get_path_url(request, pagename, attachment_path):
85 url_prefix = request.cfg.url_prefix
86 url_prefix = url_prefix.replace('/','')
87
88 if hasattr(request.cfg, 'document_root'):
89
90 static_path = "%(dir)s/tmp/Gallery2%(wiki_name)s/%(pagename)s" % {
91 "wiki_name": request.getScriptname(),
92 "pagename": pagename,
93 "dir": request.cfg.document_root,
94 }
95 static_url = "%(prefix)s/tmp/Gallery2%(wiki_name)s/%(pagename)s/" % {
96 "prefix": url_prefix,
97 "wiki_name": request.getScriptname(),
98 "pagename": pagename,
99 }
100 else:
101 static_path = attachment_path
102 static_url = AttachFile.getAttachUrl(pagename, '', request)
103
104 static_url = string.replace(static_url,'//','/')
105
106 return static_path, static_url
107
108 def option_list(this_image, pagename, text, request):
109 txt = ''
110
111 for s in text:
112 name = AttachFile.getAttachUrl(pagename, s, request)
113 if name == this_image:
114 txt += '<option selected value="%(name)s">%(alias)s' % {
115 "name": this_image,
116 "alias": s.replace('tmp.','')}
117 else:
118 txt += '<option value="%(name)s">%(alias)s' % {
119 "name": this_image,
120 "alias": s.replace('tmp.','')}
121
122 return txt
123
124 def to_htmltext(text):
125
126 if text.find ("'"):
127 text = text.split("'")
128 text = '''.join(text)
129
130 return text
131
132 def html_js(this_image, counter):
133 html = '''
134 <SCRIPT LANGUAGE="JavaScript">
135 <!-- Original: Ricocheting (ricocheting@hotmail.com) -->
136 <!-- Web Site: http://www.ricocheting.com -->
137
138 <!-- This script and many more are available free online at -->
139 <!-- The JavaScript Source!! http://javascript.internet.com -->
140
141 <!-- Modifications by Reimar Bauer <R.Bauer@fz-juelich.de> -->
142 <!-- 2005-10-29 -->
143 <!-- Many thanks to Ricocheting, it is much easier as my own one. I like it -->
144 <!-- Some code added and replaced for the MoinMoin:Gallery2 parser-->
145
146
147 <!-- Begin
148
149 var rotate_delay = 1000; // delay in milliseconds (5000 = 5 secs)
150 var current = %(counter)s;
151 var theImages = new Array();
152 var thewebImages = new Array();
153 var thefullImages = new Array();
154
155 function gallery2preload() {
156 var list = document.slideform.webnail_list.value;
157 var value = list.split(",");
158 var n = value.length;
159
160 for (i = 0; i < n-1; i++){
161 theImages[i] = new Image();
162 theImages[i].src = value[i];
163 }
164 thewebImages = theImages;
165
166 var list = document.slideform.full_list.value;
167 var value = list.split(",");
168 var n = value.length;
169
170 for (i = 0; i < n-1; i++){
171 thefullImages[i] = new Image();
172 thefullImages[i].src = value[i];
173 }
174
175 }
176
177 function gallery2getserver() {
178 var value = document.URL;
179 var text = value.split("/");
180 return text[0]+'//'+text[2];
181 }
182
183 function gallery2preload_finished() {
184 var url = gallery2getserver()+'/wiki/img/star_on.png';
185 return url;
186 }
187
188 function gallery2add_comments() {
189 var alias_text = document.slideform.alias.value;
190 var exif_date_text = document.slideform.exif_date.value;
191 var index = document.slideform.slide.selectedIndex;
192 var alias = alias_text.split("!,!");
193 var exif = exif_date_text.split(",");
194 document.getElementById("gallery2_alias_text").innerHTML = alias[index];
195 document.getElementById("gallery2_exif_date_text").innerHTML = exif[index];
196 }
197
198 function gallery2next_slide() {
199 if (document.slideform.slide[current+1]) {
200 document.images.show.src = theImages[current+1].src;
201 document.slideform.slide.selectedIndex = ++current;
202 gallery2add_comments();
203 }
204 else gallery2first_slide();
205 document.getElementById("gallery2_first_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/img/first.png" onclick="gallery2first_slide();" name="fs" title="first slide" >';
206 document.getElementById("gallery2_last_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/img/last.png" onclick="gallery2last_slide();" name="fs" title="last slide" >';
207 }
208 function gallery2previous_slide() {
209 if (current-1 >= 0) {
210 document.images.show.src = theImages[current-1].src;
211 document.slideform.slide.selectedIndex = --current;
212 gallery2add_comments();
213
214 }
215 else gallery2last_slide();
216
217 document.getElementById("gallery2_first_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/img/first.png" onclick="gallery2first_slide();" name="fs" title="first slide" >';
218 document.getElementById("gallery2_last_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/img/last.png" onclick="gallery2last_slide();" name="fs" title="last slide" >';
219 }
220 function gallery2first_slide() {
221 current = 0;
222 document.images.show.src = theImages[0].src;
223 document.slideform.slide.selectedIndex = 0;
224 gallery2add_comments();
225 document.getElementById("gallery2_first_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/img/first_disabled.png" title="first slide" >';
226 document.getElementById("gallery2_last_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/img/last.png" onclick="gallery2last_slide();" name="fs" title="last slide" >';
227 }
228 function gallery2last_slide() {
229 current = document.slideform.slide.length-1;
230 document.images.show.src = theImages[current].src;
231 document.slideform.slide.selectedIndex = current;
232 gallery2add_comments();
233 document.getElementById("gallery2_first_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/img/first.png" onclick="gallery2first_slide();" name="fs" title="first slide" >';
234 document.getElementById("gallery2_last_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/img/last_disabled.png" title="last slide" >';
235 }
236
237 function gallery2switch_images() {
238 if (document.slideform.flag.value == "webnail") {
239 var list = document.slideform.full_list.value;
240 var name = document.slideform.full_name.value;
241 document.slideform.flag.value = "fullscreen";
242 theImages = thefullImages;
243 } else {
244 var list = document.slideform.webnail_list.value;
245 var name = document.slideform.webnail_name.value;
246 document.slideform.flag.value = "webnail";
247 theImages = thewebImages;
248 }
249
250 var value = list.split(",");
251 var alias = name.split(",");
252
253 var n = value.length;
254
255 for (i = 0; i < n-1; i++){
256 var al = alias[i];
257 var al = al.replace("tmp.","");
258 document.slideform.slide[i].value=value[i];
259 document.slideform.slide[i].text=al;
260 }
261 document.images.show.src = theImages[current].src;
262 }
263
264 function gallery2ap(text) {
265 document.slideform.slidebutton.value = (text == "Stop") ? "Start" : "Stop";
266 gallery2rotate();
267 }
268
269 function gallery2change() {
270 current = document.slideform.slide.selectedIndex;
271 document.images.show.src = theImages[current].src;
272 gallery2add_comments();
273 document.getElementById("gallery2_first_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/img/first.png" onclick="gallery2first_slide();" name="fs" title="first slide" >';
274 document.getElementById("gallery2_last_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/img/last.png" onclick="gallery2last_slide();" name="fs" title="last slide" >';
275 }
276
277 function gallery2rotate() {
278 if (document.slideform.slidebutton.value == "Stop") {
279 current = (current == document.slideform.slide.length-1) ? 0 : current+1;
280 document.images.show.src = theImages[current].src;
281 document.slideform.slide.selectedIndex = current;
282 gallery2add_comments();
283 document.getElementById("gallery2_first_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/img/first.png" onclick="gallery2first_slide();" name="fs" title="first slide" >';
284 document.getElementById("gallery2_last_slide").innerHTML = '<img src="'+gallery2getserver()+'/wiki/img/last.png" onclick="gallery2last_slide();" name="fs" title="last slide" >';
285 rotate_delay = document.slideform.duration.value;
286 window.setTimeout("gallery2rotate()", rotate_delay);
287 }
288 }
289 // End -->
290 </script> ''' % {
291 'counter': counter}
292
293 return html
294
295 def html_show_image(request, pagename, url_wiki_page, full, alias, exif_date, target, idx):
296 attachment_path = AttachFile.getAttachDir(request, pagename)
297 static_path, static_url = get_path_url(request, pagename, attachment_path )
298
299 option_webnail = option_list(static_url + target[idx], pagename, target, request)
300 static_path, static_url = get_path_url(request, pagename, attachment_path)
301
302 inner_table_style = ' style="border-style:none; margin:10px;"'
303
304 this_full_list = ''
305 for s in full:
306 this_full_list += AttachFile.getAttachUrl(pagename, s, request) + ','
307
308 this_webnail_list = ''
309 for s in target:
310 this_webnail_list += static_url + s + ','
311
312
313 html = '''
314 <form name=slideform method="POST">
315 <input type="hidden" name="full_list" value='%(this_full_list)s'>
316 <input type="hidden" name="full_name" value='%(this_full_name)s'>
317 <input type="hidden" name="flag" value="webnail">
318 <input type="hidden" name="webnail_list" value='%(this_webnail_list)s'>
319 <input type="hidden" name="webnail_name" value='%(this_webnail_name)s'>
320 <input type="hidden" name="alias" value='%(this_alias_list)s'>
321 <input type="hidden" name="exif_date" value='%(this_exif_date_list)s'>
322 <BR>
323 <table%(tablestyle)s>
324 <tr>
325 <td style="border-style:none" bgcolor="#C0C0C0"><strong>Slide: </strong></td>
326 <td style="border-style:none" bgcolor="#C0C0C0">
327 <select name="slide" onChange="gallery2change();" >
328 %(option_webnails)s
329 </select>
330 </td>
331 <td style="border-style:none" bgcolor="#C0C0C0">
332 <input type="button" name="slidebutton" onClick="gallery2ap(this.value);" value="Start" title="AutoPlay">
333 </td>
334 <td style="border-style:none" bgcolor="#C0C0C0">
335 <strong>Duration: </strong>
336 </td>
337 <td style="border-style:none" bgcolor="#C0C0C0">
338 <input type="input" name="duration" value="1000" size="5" title="Duration">
339 </td>
340 </tr>
341 <tr>
342 <td style="border-style:none" colspan="5" align="center" bgcolor="#C0C0C0">
343 <SPAN id="gallery2_first_slide"><img src="%(server)s/wiki/img/first.png" onclick="gallery2first_slide();" name="fs" title="first slide" ></SPAN>
344 <img src="%(server)s/wiki/img/previous.png" onclick="gallery2previous_slide();" title="previous slide" >
345 <img src="%(server)s/wiki/img/next.png" onClick="gallery2next_slide();" title="next slide" >
346 <SPAN id="gallery2_last_slide"><img src="%(server)s/wiki/img/last.png" onClick="gallery2last_slide();" title="last slide" ></SPAN>
347 <input type="image" value="submit" src="%(server)s/wiki/img/back.png" title="return to %(pagename)s">
348 </td>
349 </tr>
350 <tr>
351 <td style="border-style:none" colspan="5" align="center" bgcolor="#C0C0C0">
352 <img src="%(server)s/%(this_image)s" name="show" onClick="gallery2switch_images();">
353 </td>
354 </tr>
355 <tr valign="center">
356 <td style="border-style:none"colspan="5" bgcolor="#C0C0C0" align="left">
357 <P><SPAN id="gallery2_alias_text">%(this_alias_text)s</SPAN></P>
358 </td></tr>
359 <tr valign="center">
360 <td style="border-style:none" colspan="5" bgcolor="#C0C0C0" align="left">
361 <P><SPAN id="gallery2_exif_date_text">%(this_exif_date_text)s</SPAN></P>
362 </td>
363 </tr>
364 <tr>
365 <td style="border-style:none" colspan="5" bgcolor="#C0C0C0" align="left" >
366 <P><SPAN id="gallery2_status">%(comment)s</SPAN></P>
367 </td>
368
369 </tr>
370 </table>
371 </form>
372 ''' % {
373 "server": request.getQualifiedURL(),
374 "base_url": request.getScriptname(),
375 "this_full_list": this_full_list,
376 "this_full_name": string.join(full, ','),
377 "this_webnail_list": this_webnail_list,
378 "this_webnail_name": string.join(target, ','),
379 "this_target": full[idx],
380
381 "this_alias_text": to_htmltext(alias[idx]),
382 "this_alias_list": to_htmltext(string.join(alias, '!,!')),
383 "this_exif_date_text": exif_date[idx],
384 "this_exif_date_list": string.join(exif_date, ','),
385
386 "this_image": static_url + target[idx], # AttachFile.getAttachUrl(pagename, target[idx], request),
387 "url_wiki_page": url_wiki_page,
388 "comment": "Click on the image for the next higher/lower resolution",
389 "pagename": pagename,
390 "tablestyle": inner_table_style,
391 "option_webnails": option_webnail,
392
393 }
394
395 return html
396
397
398 def to_wikiname(request, text):
399
400 #taken from test_parser_wiki
401 page = Page(request, 'ThisPageDoesNotExistsAndWillNeverBeReally')
402 page.formatter = Formatter(request)
403 request.formatter = page.formatter
404 page.formatter.setPage(page)
405 page.hilite_re = None
406
407 out = StringIO.StringIO()
408 request.redirect(out)
409 wikiizer = wiki.Parser(text.strip(), request)
410 wikiizer.format(page.formatter)
411 result = out.getvalue()
412 request.redirect()
413 del out
414
415 if version.release < '1.5.0':
416 return result.strip()
417 else:
418 result = result.strip()
419 result = result.replace('<a id="line-1"></a><p>', '')
420 result = result.replace('</p>', '')
421 return result
422
423
424 action_name = __name__.split('.')[-1]
425
426 def execute(pagename, request):
427 """ Main dispatcher for the 'Gallery' action.
428 """
429 _ = request.getText
430
431 request.formatter = Formatter(request)
432 attachment_path = AttachFile.getAttachDir(request, pagename)
433 command = request.form.get('do', ['none'])[0]
434
435 if command == 'VS':
436 web = {}
437 images = request.form.get('target', [''])[0]
438 images = images.split(',')
439 target = images[0]
440 images = (images[1:])
441
442 full_image = request.form.get('full', [''])[0]
443 full_image = full_image.split(',')
444 full_target = full_image[0]
445 full_image = (full_image[1:])
446
447 all_description = request.form.get('alias', [''])[0]
448 all_description = all_description.split('!,!')
449 this_description = all_description[0]
450 all_description = (all_description[1:])
451
452 all_exif_date = request.form.get('exif_date', [''])[0]
453 all_exif_date = all_exif_date.split(',')
454 this_exif_date = all_exif_date[0]
455 all_exif_date = (all_exif_date[1:])
456
457 # XXX Check that all lists have same length
458
459 z = 0
460 for img in images:
461 if target == img:
462 idx = z
463
464 z += 1
465 n = len(images)
466
467 ######## url_wiki_page #############################################################
468 text = pagename
469 url = pagename
470 url_wiki_page = wikiutil.link_tag(request, url, text=text,
471 formatter=request.formatter)
472
473 ############################################################################
474
475 attachment_path = AttachFile.getAttachDir(request, pagename)
476
477 static_path, static_url = get_path_url(request, pagename, attachment_path )
478
479 web['src'] = static_url+target #AttachFile.getAttachUrl(pagename, target, request)
480 web['title'] = target
481 #web['width']="1024"
482
483 image_link = request.formatter.image(**web)
484
485 request.http_headers()
486
487 request.setContentLanguage(request.lang)
488
489 request.theme.send_title(pagename,
490 pagename=pagename,
491 body_onload="gallery2preload();",
492 html_head=html_js(AttachFile.getAttachUrl(pagename, target, request), idx))
493
494 request.write(request.formatter.startContent("content"))
495 html = html_show_image(request, pagename, url_wiki_page, full_image, all_description, all_exif_date, images, idx)
496 request.write(html)
497 request.write(request.formatter.endContent())
498 request.theme.send_footer(pagename)
499 msg = None
500
501 elif command == 'PS':
502 msg = None
503
504 elif command == 'BS':
505 msg = "gone back" #None
506
507 elif request.user.may.delete(pagename):
508 # only users which are allowed to delete should use this tool
509
510 target = request.form.get('target', [''])[0]
511 file, ext = os.path.splitext(target)
512
513 if ext == '.gif' or ext == '.png':
514 img_type = 'PNG'
515 thumbfile = "tmp.thumbnail_%(file)s.png" % {"file": file}
516 webnail = "tmp.webnail_%(file)s.png" % {"file": file}
517 else:
518 img_type = "JPEG"
519 thumbfile = "tmp.thumbnail_%(file)s.jpg" % {"file": file}
520 webnail = "tmp.webnail_%(file)s.jpg" % {"file": file}
521
522 static_path, static_url = get_path_url(request, pagename, attachment_path )
523
524 thumb = os.path.join(static_path, thumbfile)
525 webf = os.path.join(static_path, webnail)
526 infile = os.path.join(attachment_path, target)
527
528 msg = None
529 if version.release < '1.5.0':
530 if action_name in request.cfg.excluded_actions:
531 msg = _('File attachments are not allowed in this wiki!')
532
533 elif command == 'RM':
534 if os.path.exists(infile + '.bak'):
535 os.unlink("%(file)s.bak" % {"file": infile})
536 filesys.rename(infile, "%(file)s.bak" % {"file": infile})
537 msg = _('%(target)s deleted, backup in place' % {
538 'target':target})
539
540 elif command == 'RL':
541 im = Image.open(infile)
542 os.remove(infile)
543 im.rotate(90).save(infile, img_type)
544 nim = Image.open(infile)
545 nim.thumbnail((640, 640), Image.ANTIALIAS)
546 os.remove(webf)
547 nim.save(webf, img_type)
548 nim.thumbnail((128, 128), Image.ANTIALIAS)
549 os.remove(thumb)
550 nim.save(thumb, img_type)
551 msg = _('%(target)s rotated to left 90 degrees' % {
552 'target':target})
553
554 elif command == 'RR':
555 im = Image.open(infile)
556 os.remove(infile)
557 im.rotate(270).save(infile, img_type)
558
559 nim = Image.open(infile)
560 nim.thumbnail((640, 640), Image.ANTIALIAS)
561 os.remove(webf)
562 nim.save(webf, img_type)
563 nim.thumbnail((128, 128), Image.ANTIALIAS)
564 os.remove(thumb)
565 nim.save(thumb, img_type)
566 msg = _('%(target)s rotated to right 90 degrees' % {
567 'target':target})
568 else:
569 msg = _('action not implemented: %s') % (command, )
570 else:
571 msg = _('Your are not allowed to change images on this page: %s') % (pagename,)
572
573 if msg:
574 AttachFile.error_msg(pagename, request, msg)
575
576 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.