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