de-cgi-bin/taggr.py
changeset 19 8d3ffd87cb0b
parent 18 46536daf9e04
equal deleted inserted replaced
18:46536daf9e04 19:8d3ffd87cb0b
       
     1 #!/usr/bin/env python2.4
       
     2 #
       
     3 # DeGAL - A pretty simple web image gallery
       
     4 # Copyright (C) 2007 Tero Marttila
       
     5 # http://marttila.de/~terom/degal/
       
     6 #
       
     7 # This program is free software; you can redistribute it and/or modify
       
     8 # it under the terms of the GNU General Public License as published by
       
     9 # the Free Software Foundation; either version 2 of the License, or
       
    10 # (at your option) any later version.
       
    11 #
       
    12 # This program is distributed in the hope that it will be useful,
       
    13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    15 # GNU General Public License for more details.
       
    16 #
       
    17 # You should have received a copy of the GNU General Public License
       
    18 # along with this program; if not, write to the
       
    19 # Free Software Foundation, Inc.,
       
    20 # 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
       
    21 #
       
    22 
       
    23 
       
    24 import cgi
       
    25 import shelve
       
    26 import os, os.path
       
    27 
       
    28 import degal
       
    29 
       
    30 images = shelve.open('images', 'c')
       
    31 tag_images = shelve.open('tag_images', 'c')
       
    32 tag_tags = shelve.open('tag_tags', 'c')
       
    33 
       
    34 # request params
       
    35 vars = cgi.FieldStorage()
       
    36 
       
    37 if 'path' in vars :
       
    38     path = vars['path'].value
       
    39 else :
       
    40     path = '.'
       
    41 
       
    42 image_list = []
       
    43 
       
    44 if 'bulk_tag' in vars :
       
    45     bulk_tags = vars['bulk_tag'].value.split()
       
    46 else :
       
    47     bulk_tags = None
       
    48 
       
    49 for fname in os.listdir(path) :
       
    50     if degal.isImage(fname) :
       
    51         image_path = os.path.join(path, fname)
       
    52         thumb_path = os.path.join(path, degal.THUMB_DIR, fname)
       
    53         html_path = image_path + '.html'
       
    54 
       
    55         title, descr, tags = images.get(image_path, (None, None, []))
       
    56         
       
    57         if 'img_%s_title' % fname in vars :
       
    58             title = vars['img_%s_title' % fname].value
       
    59         
       
    60         if 'img_%s_descr' % fname in vars :
       
    61             descr = vars['img_%s_descr' % fname].value
       
    62 
       
    63         if 'img_%s_tags' % fname in vars :
       
    64             tags = vars['img_%s_tags' % fname].value.split()
       
    65 
       
    66         if bulk_tags and 'img_%s_chk' % fname in vars :
       
    67             tags.extend(bulk_tags)
       
    68         
       
    69         if title or descr or tags :
       
    70            images[image_path] = (title, descr, tags)
       
    71 
       
    72         html = """
       
    73 <li>
       
    74     <span class="inputs">
       
    75         <label for="img_%(fname)s_title">Title</label> <input type="text" name="img_%(fname)s_title" size="60" value="%(title)s" /> <br/>
       
    76         <label for="img_%(fname)s_descr">Description</label> <input type="text" name="img_%(fname)s_descr" size="60" value="%(descr)s" /> <br/>
       
    77         <label for="img_%(fname)s_tags">Tags</label> <input type="text" name="img_%(fname)s_tags" size="60" value="%(tags)s" /> <br/>
       
    78     </span>
       
    79     
       
    80     <input type="checkbox" name="img_%(fname)s_chk" class="chk" />
       
    81     
       
    82     <span class="thumb"><a href="%(html_path)s"><img src="%(thumb_path)s" /></a></span>
       
    83 </li>
       
    84         """ % dict(
       
    85             html_path       = html_path,
       
    86             thumb_path      = thumb_path,
       
    87             fname           = fname,
       
    88             title           = title and title or '',
       
    89             descr           = descr and descr or '',
       
    90             tags            = tags and ' '.join(tags) or '',
       
    91         )
       
    92 
       
    93         image_list.append((fname, html))
       
    94 
       
    95 image_list.sort()
       
    96 
       
    97 print "Content-Type: text/html"
       
    98 print
       
    99 print degal.Template('taggr').render(
       
   100     TITLE       = "Taggr - %s" % path,
       
   101     BREADCRUMB  = "TODO",
       
   102     CONTENT     = "<ul>" + ''.join([h for fname, h in image_list]) + "</ul>",
       
   103     PATH        = path,
       
   104 )
       
   105