series.cgi
changeset 3 9637b8f24005
parent 1 740133ab6353
child 4 d46ab092d2b2
equal deleted inserted replaced
2:8b2b40a51098 3:9637b8f24005
     1 #!/usr/bin/env python2.4
     1 #!/usr/bin/env python2.4
     2 
     2 import shelve
     3 import dbm
       
     4 import cgi
     3 import cgi
     5 import Cookie
     4 import Cookie
     6 import os, os.path
     5 import os, os.path
     7 #import pprint
     6 #import pprint
     8 
     7 
    11 #
    10 #
    12 # load request params
    11 # load request params
    13 #
    12 #
    14 vars = cgi.FieldStorage()
    13 vars = cgi.FieldStorage()
    15 
    14 
    16 # keys = what photos are in the series
    15 # these are interpeted different ways, hence the generic naming
    17 keys = vars["keys"].value.split()
    16 arg1 = vars["keys"].value
       
    17 if 'index' in vars :
       
    18     arg2 = vars["index"].value
       
    19 else :
       
    20     arg2 = None
    18 
    21 
    19 # index = what photo to show, or the index
    22 # the cookie with the user's current series
    20 if 'index' in vars :
    23 cookie = Cookie.SimpleCookie(os.environ.get('HTTP_COOKIE', None))
    21     index = vars["index"].value
       
    22 else :
       
    23     index = None
       
    24 
    24 
    25 if keys and keys[0] in ('add', 'del', 'clear', 'view') or index == 'load' :
    25 # a special action?
    26     cookie = Cookie.SimpleCookie(os.environ.get('HTTP_COOKIE', None))
    26 if arg1 and arg1 in ('add', 'del', 'clear', 'view') or arg2 == 'load' :
    27     act = keys[0]
    27     # load the keys from the cookie
    28     _keys = keys
       
    29 
       
    30     if 'series' in cookie :
    28     if 'series' in cookie :
    31         keys = cookie["series"].value.split()
    29         keys = cookie["series"].value.split()
    32     else :
    30     else :
    33         keys = []
    31         keys = []
    34     
    32     
    35     if index == 'load' :
    33     if arg2 == 'load' :
    36         keys = _keys
    34         # set the keys in the user's cookie to those in the URL
    37     elif act == 'add' and index not in keys :
    35         keys = arg1.split()
    38         keys.append(index)
    36 
    39     elif act == 'del' :
    37     elif arg1 == 'add' and arg2 not in keys :
    40         keys.remove(index)
    38         # add a code to the list of keys
    41     elif act == 'clear' :
    39         keys.append(arg2)
       
    40 
       
    41     elif arg1 == 'del' and arg2 in keys :
       
    42         # remove a key from the list of keys
       
    43         keys.remove(arg2)
       
    44 
       
    45     elif arg1 == 'clear' :
       
    46         # clear out the set of keys
    42         keys = []
    47         keys = []
    43     elif act == 'view' :
    48 
       
    49     elif arg1 == 'view' :
       
    50         # just view them
    44         pass
    51         pass
    45     
    52    
       
    53     # set the series cookie value
    46     cookie['series'] = ' '.join(keys)
    54     cookie['series'] = ' '.join(keys)
    47     cookie['series']['path'] = '/'
    55     cookie['series']['path'] = '/'
    48 
    56     
       
    57     # if we have keys, redirect to them, otherwise, back to index we go
    49     if keys :
    58     if keys :
    50         redirect_to = "../%s/" % ('+'.join(keys))
    59         redirect_to = "../%s/" % ('+'.join(keys))
    51     else :
    60     else :
    52         redirect_to = "../.."
    61         redirect_to = "../.."
    53 
    62     
       
    63     # do the redirect
    54     print "Status: 302"
    64     print "Status: 302"
    55     print "Location: %s" % redirect_to
    65     print "Location: %s" % redirect_to
    56     print cookie
    66     print cookie
    57     print
    67     print
    58     print "Redirect..."
    68     print "Redirect..."
    59 else :
    69 else :
    60     cookie = Cookie.SimpleCookie(os.environ.get('HTTP_COOKIE', None))
    70     # we're just viewing
       
    71     keys = arg1.split()
       
    72     
       
    73     # is this "My Series"?
       
    74     my_series = 'series' in cookie and cookie['series'].value.split() == keys
       
    75         
       
    76     if arg2 :
       
    77         index = int(arg2)
       
    78     else :
       
    79         index = None
    61 
    80 
    62     my_series = 'series' in cookie and cookie['series'].value.split() == keys 
    81     # load DB
    63         
    82     db = shelve.open('shorturls2', 'r')
    64     if index :
       
    65         index = int(index)
       
    66 
    83 
       
    84     # get the Image objects
       
    85     photos = []
       
    86     
    67     #
    87     #
    68     # load DB
    88     # Start ugly code-misreuse
    69     #
    89     #
    70     db = dbm.open('shorturls', 'r')
       
    71 
       
    72     #
       
    73     # get the Image objects
       
    74     #
       
    75     photos = []
       
    76 
       
    77     INDEX_URL = "/series/%s/"
       
    78     IMAGE_URL = "/series/%s/%d"
       
    79 
    90 
    80     # monkey-patch the templates
    91     # monkey-patch the templates
    81     rendered_templates = []
    92     rendered_templates = []
    82 
    93 
    83     def _myRenderTo (self, path, **vars) :
    94     def _myRenderTo (self, path, **vars) :
    84         rendered_templates.append(self.render(**vars))
    95         rendered_templates.append(self.render(**vars))
    85 
    96 
       
    97     # lalalalala... ooh, look;
       
    98 
    86     degal.Template.renderTo = _myRenderTo
    99     degal.Template.renderTo = _myRenderTo
       
   100     #
       
   101     #         vvvvvvvvvvvvvvvvvvvv
       
   102     #         vvvvvvvvvvvvvvvvvvvv
       
   103     #
       
   104     #    >>>> SHINY PINK ELEPHANT! <<<<
       
   105     #
       
   106     #         ^^^^^^^^^^^^^^^^^^^^
       
   107     #         ^^^^^^^^^^^^^^^^^^^^
    87 
   108 
    88     # our own version of Folder
   109     # our own version of Folder
    89     class Series (degal.Folder) :
   110     class Series (degal.Folder) :
    90         def __init__ (self) :
   111         def __init__ (self) :
    91             super(Series, self).__init__()
   112             super(Series, self).__init__()
   108             return [('../..', 'Gallery'), ('.', 'Series')]
   129             return [('../..', 'Gallery'), ('.', 'Series')]
   109 
   130 
   110         def inRoot (self, *fnames) :
   131         def inRoot (self, *fnames) :
   111             return os.path.join('../..', *fnames)
   132             return os.path.join('../..', *fnames)
   112 
   133 
   113     #    def pathFor (self, *fnames) :
       
   114     #        return os.path.join(fnames)
       
   115 
       
   116     class Image (degal.Image) :
   134     class Image (degal.Image) :
   117         def __init__ (self, series, key, i) :
   135         def __init__ (self, series, key, i) :
   118             path = db[key]
   136             type, self.dir_name, self.image_name = db[key]
   119             path = path.rstrip(".html")
       
   120             self.dir_name, self.image_name = os.path.split(path)
       
   121 
   137 
   122             super(Image, self).__init__(series, self.image_name)
   138             super(Image, self).__init__(series, self.image_name)
   123             
   139             
   124             self.path = path
   140             self.path = self._path()
   125             self.shorturl_code = key
   141             self.shorturl_code = key
   126             self.html_path = self.html_name = str(i + 1)
   142             self.html_path = self.html_name = str(i + 1)
   127             self.title = 'Image %d' % (i + 1, )
   143             self.title = 'Image %d' % (i + 1, )
   128             self.descr = '<span style="font-size: x-small">Standalone image: <a href="%s.html">%s</a></span>' % (self._path(), self._path().lstrip('./'))
   144             self.descr = '<span style="font-size: x-small">Standalone image: <a href="%s.html">%s</a></span>' % (self._path(), self._path().lstrip('./'))
   129             self.img_size = (-1, -1)
   145             self.img_size = (-1, -1)
   141 
   157 
   142         def _path (self, blaa='') :
   158         def _path (self, blaa='') :
   143             return os.path.join('../..', self.dir_name, blaa, self.image_name)
   159             return os.path.join('../..', self.dir_name, blaa, self.image_name)
   144 
   160 
   145         def thumbImgTag (self) :
   161         def thumbImgTag (self) :
   146             return degal.link(self.html_name, "<img src='%s' alt='%s' title='%s'>" % (os.path.join('../..', self.dir_name, degal.THUMB_DIR, self.image_name), '', self.title))
   162             return degal.link(self.html_name, "<img src='%s' alt='%s' title='%s'>" % (self._path(degal.THUMB_DIR), '', self.title))
   147 
   163 
   148         def previewImgTag (self) :
   164         def previewImgTag (self) :
   149             return degal.link(os.path.join('../..', self.dir_name, self.image_name), "<img src='%s' alt='%s' title='%s'>" % (os.path.join('../..', self.dir_name, degal.PREVIEW_DIR, self.image_name), '', self.title))
   165             return degal.link(self._path(), "<img src='%s' alt='%s' title='%s'>" % (self._path(degal.PREVIEW_DIR), '', self.title))
   150 
       
   151            
       
   152 
   166 
   153     series = Series()
   167     series = Series()
   154 
   168 
   155     try :
   169     try :
   156         prev = None
   170         prev = None
   162                 prev.next = img
   176                 prev.next = img
   163 
   177 
   164             img.prev = prev
   178             img.prev = prev
   165             prev = img
   179             prev = img
   166 
   180 
   167     #        series.images[str(i + 1)] = img
       
   168             series.sorted_images.append(img)
   181             series.sorted_images.append(img)
   169     finally :
   182     finally :
   170         db.close()
   183         db.close()
   171 
   184 
   172     if index :
   185     if index :