series.cgi
changeset 3 9637b8f24005
parent 1 740133ab6353
child 4 d46ab092d2b2
--- a/series.cgi	Thu Nov 08 17:08:04 2007 +0000
+++ b/series.cgi	Thu Nov 08 17:28:01 2007 +0000
@@ -1,6 +1,5 @@
 #!/usr/bin/env python2.4
-
-import dbm
+import shelve
 import cgi
 import Cookie
 import os, os.path
@@ -13,69 +12,81 @@
 #
 vars = cgi.FieldStorage()
 
-# keys = what photos are in the series
-keys = vars["keys"].value.split()
-
-# index = what photo to show, or the index
+# these are interpeted different ways, hence the generic naming
+arg1 = vars["keys"].value
 if 'index' in vars :
-    index = vars["index"].value
+    arg2 = vars["index"].value
 else :
-    index = None
+    arg2 = None
 
-if keys and keys[0] in ('add', 'del', 'clear', 'view') or index == 'load' :
-    cookie = Cookie.SimpleCookie(os.environ.get('HTTP_COOKIE', None))
-    act = keys[0]
-    _keys = keys
+# the cookie with the user's current series
+cookie = Cookie.SimpleCookie(os.environ.get('HTTP_COOKIE', None))
 
+# a special action?
+if arg1 and arg1 in ('add', 'del', 'clear', 'view') or arg2 == 'load' :
+    # load the keys from the cookie
     if 'series' in cookie :
         keys = cookie["series"].value.split()
     else :
         keys = []
     
-    if index == 'load' :
-        keys = _keys
-    elif act == 'add' and index not in keys :
-        keys.append(index)
-    elif act == 'del' :
-        keys.remove(index)
-    elif act == 'clear' :
+    if arg2 == 'load' :
+        # set the keys in the user's cookie to those in the URL
+        keys = arg1.split()
+
+    elif arg1 == 'add' and arg2 not in keys :
+        # add a code to the list of keys
+        keys.append(arg2)
+
+    elif arg1 == 'del' and arg2 in keys :
+        # remove a key from the list of keys
+        keys.remove(arg2)
+
+    elif arg1 == 'clear' :
+        # clear out the set of keys
         keys = []
-    elif act == 'view' :
+
+    elif arg1 == 'view' :
+        # just view them
         pass
-    
+   
+    # set the series cookie value
     cookie['series'] = ' '.join(keys)
     cookie['series']['path'] = '/'
-
+    
+    # if we have keys, redirect to them, otherwise, back to index we go
     if keys :
         redirect_to = "../%s/" % ('+'.join(keys))
     else :
         redirect_to = "../.."
-
+    
+    # do the redirect
     print "Status: 302"
     print "Location: %s" % redirect_to
     print cookie
     print
     print "Redirect..."
 else :
-    cookie = Cookie.SimpleCookie(os.environ.get('HTTP_COOKIE', None))
-
-    my_series = 'series' in cookie and cookie['series'].value.split() == keys 
+    # we're just viewing
+    keys = arg1.split()
+    
+    # is this "My Series"?
+    my_series = 'series' in cookie and cookie['series'].value.split() == keys
         
-    if index :
-        index = int(index)
+    if arg2 :
+        index = int(arg2)
+    else :
+        index = None
 
-    #
     # load DB
-    #
-    db = dbm.open('shorturls', 'r')
+    db = shelve.open('shorturls2', 'r')
 
-    #
     # get the Image objects
-    #
     photos = []
-
-    INDEX_URL = "/series/%s/"
-    IMAGE_URL = "/series/%s/%d"
+    
+    #
+    # Start ugly code-misreuse
+    #
 
     # monkey-patch the templates
     rendered_templates = []
@@ -83,7 +94,17 @@
     def _myRenderTo (self, path, **vars) :
         rendered_templates.append(self.render(**vars))
 
+    # lalalalala... ooh, look;
+
     degal.Template.renderTo = _myRenderTo
+    #
+    #         vvvvvvvvvvvvvvvvvvvv
+    #         vvvvvvvvvvvvvvvvvvvv
+    #
+    #    >>>> SHINY PINK ELEPHANT! <<<<
+    #
+    #         ^^^^^^^^^^^^^^^^^^^^
+    #         ^^^^^^^^^^^^^^^^^^^^
 
     # our own version of Folder
     class Series (degal.Folder) :
@@ -110,18 +131,13 @@
         def inRoot (self, *fnames) :
             return os.path.join('../..', *fnames)
 
-    #    def pathFor (self, *fnames) :
-    #        return os.path.join(fnames)
-
     class Image (degal.Image) :
         def __init__ (self, series, key, i) :
-            path = db[key]
-            path = path.rstrip(".html")
-            self.dir_name, self.image_name = os.path.split(path)
+            type, self.dir_name, self.image_name = db[key]
 
             super(Image, self).__init__(series, self.image_name)
             
-            self.path = path
+            self.path = self._path()
             self.shorturl_code = key
             self.html_path = self.html_name = str(i + 1)
             self.title = 'Image %d' % (i + 1, )
@@ -143,12 +159,10 @@
             return os.path.join('../..', self.dir_name, blaa, self.image_name)
 
         def thumbImgTag (self) :
-            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))
+            return degal.link(self.html_name, "<img src='%s' alt='%s' title='%s'>" % (self._path(degal.THUMB_DIR), '', self.title))
 
         def previewImgTag (self) :
-            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))
-
-           
+            return degal.link(self._path(), "<img src='%s' alt='%s' title='%s'>" % (self._path(degal.PREVIEW_DIR), '', self.title))
 
     series = Series()
 
@@ -164,7 +178,6 @@
             img.prev = prev
             prev = img
 
-    #        series.images[str(i + 1)] = img
             series.sorted_images.append(img)
     finally :
         db.close()