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