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