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

You are not allowed to attach a file to this page.