cgi-bin/series.py
changeset 18 46536daf9e04
parent 17 adde6ad8731e
equal deleted inserted replaced
17:adde6ad8731e 18:46536daf9e04
       
     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 import shelve
       
    24 import cgi
       
    25 import Cookie
       
    26 import os, os.path
       
    27 
       
    28 import degal
       
    29 
       
    30 #
       
    31 # load request params
       
    32 #
       
    33 vars = cgi.FieldStorage()
       
    34 
       
    35 # these are interpeted different ways, hence the generic naming
       
    36 arg1 = vars["keys"].value
       
    37 if 'index' in vars :
       
    38     arg2 = vars["index"].value
       
    39 else :
       
    40     arg2 = None
       
    41 
       
    42 # the cookie with the user's current series
       
    43 cookie = Cookie.SimpleCookie(os.environ.get('HTTP_COOKIE', None))
       
    44 
       
    45 # a special action?
       
    46 if arg1 and arg1 in ('add', 'del', 'clear', 'view') or arg2 == 'load' :
       
    47     # load the keys from the cookie
       
    48     if 'series' in cookie :
       
    49         keys = cookie["series"].value.split()
       
    50     else :
       
    51         keys = []
       
    52     
       
    53     if arg2 == 'load' :
       
    54         # set the keys in the user's cookie to those in the URL
       
    55         keys = arg1.split()
       
    56 
       
    57     elif arg1 == 'add' and arg2 not in keys :
       
    58         # add a code to the list of keys
       
    59         keys.append(arg2)
       
    60 
       
    61     elif arg1 == 'del' and arg2 in keys :
       
    62         # remove a key from the list of keys
       
    63         keys.remove(arg2)
       
    64 
       
    65     elif arg1 == 'clear' :
       
    66         # clear out the set of keys
       
    67         keys = []
       
    68 
       
    69     elif arg1 == 'view' :
       
    70         # just view them
       
    71         pass
       
    72    
       
    73     # set the series cookie value
       
    74     cookie['series'] = ' '.join(keys)
       
    75     cookie['series']['path'] = '/'
       
    76     
       
    77     # if we have keys, redirect to them, otherwise, back to index we go
       
    78     if keys :
       
    79         redirect_to = "../%s/" % ('+'.join(keys))
       
    80     else :
       
    81         redirect_to = "../.."
       
    82     
       
    83     # do the redirect
       
    84     print "Status: 302"
       
    85     print "Location: %s" % redirect_to
       
    86     print cookie
       
    87     print
       
    88     print "Redirect..."
       
    89 else :
       
    90     # we're just viewing
       
    91     keys = arg1.split()
       
    92     
       
    93     # is this "My Series"?
       
    94     my_series = 'series' in cookie and cookie['series'].value.split() == keys
       
    95         
       
    96     if arg2 :
       
    97         index = int(arg2)
       
    98     else :
       
    99         index = None
       
   100 
       
   101     # load DB
       
   102     db = shelve.open('shorturls2', 'r')
       
   103 
       
   104     # get the Image objects
       
   105     photos = []
       
   106     
       
   107     #
       
   108     # Start ugly code-misreuse
       
   109     #
       
   110 
       
   111     # monkey-patch the templates
       
   112     rendered_templates = []
       
   113 
       
   114     def _myRenderTo (self, path, **vars) :
       
   115         rendered_templates.append(self.render(**vars))
       
   116 
       
   117     # lalalalala... ooh, look;
       
   118 
       
   119     degal.Template.renderTo = _myRenderTo
       
   120     #
       
   121     #         vvvvvvvvvvvvvvvvvvvv
       
   122     #         vvvvvvvvvvvvvvvvvvvv
       
   123     #
       
   124     #    >>>> SHINY PINK ELEPHANT! <<<<
       
   125     #
       
   126     #         ^^^^^^^^^^^^^^^^^^^^
       
   127     #         ^^^^^^^^^^^^^^^^^^^^
       
   128 
       
   129     # our own version of Folder
       
   130     class Series (degal.Folder) :
       
   131         def __init__ (self) :
       
   132             super(Series, self).__init__()
       
   133             
       
   134             self.alive = True
       
   135             self.filtered = False
       
   136             self.subdirs = {}
       
   137             self.images = {}
       
   138             self.sorted_images = []
       
   139             self.title = "Series"
       
   140 
       
   141             if my_series :
       
   142                 self.descr = '<a href="../clear/" rel="nofollow">Clear your series</a>'
       
   143             else :
       
   144                 self.descr = '<a href="load" rel="nofollow">Load as your series</a>'
       
   145 
       
   146             self.shorturl_code = ''
       
   147         
       
   148         def breadcrumb (self) :
       
   149             return [('../..', 'Gallery'), ('.', 'Series')]
       
   150 
       
   151         def inRoot (self, *fnames) :
       
   152             return os.path.join('../..', *fnames)
       
   153 
       
   154     class Image (degal.Image) :
       
   155         def __init__ (self, series, key, i) :
       
   156             type, self.dir_name, self.image_name = db[key]
       
   157 
       
   158             super(Image, self).__init__(series, self.image_name)
       
   159             
       
   160             self.path = self._path()
       
   161             self.shorturl_code = key
       
   162             self.html_path = self.html_name = str(i + 1)
       
   163             self.title = 'Image %d' % (i + 1, )
       
   164             self.descr = '<span style="font-size: x-small">Standalone image: <a href="%s.html">%s</a></span>' % (self._path(), self._path().lstrip('./'))
       
   165             self.img_size = (-1, -1)
       
   166             self.filesize = 0
       
   167             self.timestamp = 0
       
   168 
       
   169             if my_series :
       
   170                 self.series_act = "del"
       
   171                 self.series_verb = "Remove from"
       
   172             else :
       
   173                 self.series_act = self.series_verb = ""
       
   174 
       
   175         def breadcrumb (self) :
       
   176             return [('../..', 'Gallery'), ('.', 'Series'), (self.html_name, self.title)]
       
   177 
       
   178         def _path (self, blaa='') :
       
   179             return os.path.join('../..', self.dir_name, blaa, self.image_name)
       
   180 
       
   181         def thumbImgTag (self) :
       
   182             return degal.link(self.html_name, "<img src='%s' alt='%s' title='%s'>" % (self._path(degal.THUMB_DIR), '', self.title))
       
   183 
       
   184         def previewImgTag (self) :
       
   185             return degal.link(self._path(), "<img src='%s' alt='%s' title='%s'>" % (self._path(degal.PREVIEW_DIR), '', self.title))
       
   186 
       
   187     series = Series()
       
   188 
       
   189     try :
       
   190         prev = None
       
   191 
       
   192         for i, key in enumerate(keys) :
       
   193             img = Image(series, key, i)
       
   194 
       
   195             if prev :
       
   196                 prev.next = img
       
   197 
       
   198             img.prev = prev
       
   199             prev = img
       
   200 
       
   201             series.sorted_images.append(img)
       
   202     finally :
       
   203         db.close()
       
   204 
       
   205     if index :
       
   206         img = series.sorted_images[index - 1]
       
   207 
       
   208         img.render()
       
   209     else :
       
   210         series.render()
       
   211 
       
   212     print "Content-Type: text/html"
       
   213     print
       
   214     print rendered_templates[0]
       
   215