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