de-cgi-bin/series.py
changeset 19 8d3ffd87cb0b
parent 18 46536daf9e04
child 22 72696ca68c34
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 import os
       
    24 import cgi
       
    25 import Cookie
       
    26 
       
    27 import inc
       
    28 from lib import shorturl, template, utils, settings
       
    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     index = fname = None
       
    97 
       
    98     if arg2 :
       
    99         try :
       
   100             index = int(arg2)
       
   101         except ValueError :
       
   102             fname = arg2
       
   103 
       
   104     # load DB
       
   105     db = shorturl.DB()
       
   106     
       
   107     # our custom Series/Image classes, because they do act slightly differently
       
   108 
       
   109     class Series (object) :
       
   110         def __init__ (self, keys) :
       
   111             self.images = []
       
   112             prev = None
       
   113 
       
   114             self.image_dict = dict()
       
   115 
       
   116             for index, key in enumerate(keys) :
       
   117                 dir, fname = db.image_info(key)
       
   118 
       
   119                 img = Image(self, key, dir, fname, index)
       
   120                 self.images.append(img)
       
   121                 self.image_dict[fname] = img
       
   122 
       
   123                 img.prev = prev
       
   124 
       
   125                 if prev :
       
   126                     prev.next = img
       
   127 
       
   128                 prev = img
       
   129 
       
   130         def render (self) :
       
   131             if my_series :
       
   132                 descr = '<a href="../clear/" rel="nofollow">Clear your series</a>'
       
   133             else :
       
   134                 descr = '<a href="load" rel="nofollow">Load as your series</a>'
       
   135    
       
   136             return template.gallery.render(
       
   137                 stylesheet_url      = utils.url("style.css", up=2),
       
   138                 
       
   139                 breadcrumb          = [(utils.url(up=1), "Index"), (utils.url(), "Series")],
       
   140 
       
   141                 dirs                = None,
       
   142                 title               = "Series",
       
   143 
       
   144                 num_pages           = 1,
       
   145                 cur_page            = 0,
       
   146 
       
   147                 images              = self.images,
       
   148 
       
   149                 description         = descr,
       
   150 
       
   151                 shorturl            = None,
       
   152                 shorturl_code       = None,
       
   153             )
       
   154     
       
   155     class Image (object) :
       
   156         def __init__ (self, series, key, dir, fname, index) :
       
   157             self.fname = fname
       
   158             self.name = utils.url_join(dir, fname, abs=True)
       
   159             self.html_name = utils.url(fname)
       
   160             self.real_html_name = utils.url_join(dir, fname + ".html", abs=True)
       
   161 
       
   162             self.thumb_name = utils.url_join(dir, settings.THUMB_DIR, fname, abs=True)
       
   163             self.preview_name = utils.url_join(dir, settings.PREVIEW_DIR, fname, abs=True)
       
   164 
       
   165             self.shorturl = key
       
   166 
       
   167             self.prev = self.next = None
       
   168 
       
   169         def render (self) :
       
   170             descr = '<span style="font-size: x-small"><a href="%s.html">Standalone image</a></span>' % self.real_html_name
       
   171             
       
   172             if my_series :
       
   173                 series_url = utils.url_join("del", self.shorturl, up=1)
       
   174                 series_verb = "Remove from"
       
   175             else :
       
   176                 series_url = series_verb = ""
       
   177 
       
   178             return template.image.render(
       
   179                 stylesheet_url      = utils.url("style.css", up=3),
       
   180                 
       
   181                 breadcrumb          = [(utils.url(up=2), "Index"), (utils.url("."), "Series"), (self.html_name, self.fname)],
       
   182 
       
   183                 title               = self.fname,
       
   184 
       
   185                 prev                = self.prev,
       
   186                 img                 = self,
       
   187                 next                = self.next,
       
   188                 
       
   189                 description         = descr,
       
   190     
       
   191                 img_size            = None,
       
   192                 file_size           = None,
       
   193                 timestamp           = None,
       
   194                 
       
   195                 shorturl            = utils.url_join("s", self.shorturl, abs=True),
       
   196                 shorturl_code       = self.shorturl,
       
   197                 
       
   198                 series_url          = series_url,
       
   199                 series_verb         = series_verb,
       
   200             )
       
   201     
       
   202     series = Series(keys)
       
   203 
       
   204     if fname :
       
   205         html = series.image_dict[fname].render()
       
   206     elif index :
       
   207         html = series.images[index - 1].render()
       
   208     else :
       
   209         html = series.render()
       
   210 
       
   211     print "Content-Type: text/html"
       
   212     print
       
   213     print html
       
   214