series.cgi
author terom
Tue, 20 Nov 2007 23:47:00 +0000
changeset 10 e8143d784b77
parent 4 d46ab092d2b2
child 12 c2d8e9a754a1
permissions -rwxr-xr-x
SI units/ISO date-time format
#!/usr/bin/env python2.4

import shelve
import cgi
import Cookie
import os, os.path

import degal

#
# load request params
#
vars = cgi.FieldStorage()

# these are interpeted different ways, hence the generic naming
arg1 = vars["keys"].value
if 'index' in vars :
    arg2 = vars["index"].value
else :
    arg2 = None

# 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 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 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 :
    # we're just viewing
    keys = arg1.split()
    
    # is this "My Series"?
    my_series = 'series' in cookie and cookie['series'].value.split() == keys
        
    if arg2 :
        index = int(arg2)
    else :
        index = None

    # load DB
    db = shelve.open('shorturls2', 'r')

    # get the Image objects
    photos = []
    
    #
    # Start ugly code-misreuse
    #

    # monkey-patch the templates
    rendered_templates = []

    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) :
        def __init__ (self) :
            super(Series, self).__init__()
            
            self.alive = True
            self.filtered = False
            self.subdirs = {}
            self.images = {}
            self.sorted_images = []
            self.title = "Series"

            if my_series :
                self.descr = '<a href="../clear/" rel="nofollow">Clear your series</a>'
            else :
                self.descr = '<a href="load" rel="nofollow">Load as your series</a>'

            self.shorturl_code = ''
        
        def breadcrumb (self) :
            return [('../..', 'Gallery'), ('.', 'Series')]

        def inRoot (self, *fnames) :
            return os.path.join('../..', *fnames)

    class Image (degal.Image) :
        def __init__ (self, series, key, i) :
            type, self.dir_name, self.image_name = db[key]

            super(Image, self).__init__(series, self.image_name)
            
            self.path = self._path()
            self.shorturl_code = key
            self.html_path = self.html_name = str(i + 1)
            self.title = 'Image %d' % (i + 1, )
            self.descr = '<span style="font-size: x-small">Standalone image: <a href="%s.html">%s</a></span>' % (self._path(), self._path().lstrip('./'))
            self.img_size = (-1, -1)
            self.filesize = 0
            self.timestamp = 0

            if my_series :
                self.series_act = "del"
                self.series_verb = "Remove from"
            else :
                self.series_act = self.series_verb = ""

        def breadcrumb (self) :
            return [('../..', 'Gallery'), ('.', 'Series'), (self.html_name, self.title)]

        def _path (self, blaa='') :
            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'>" % (self._path(degal.THUMB_DIR), '', self.title))

        def previewImgTag (self) :
            return degal.link(self._path(), "<img src='%s' alt='%s' title='%s'>" % (self._path(degal.PREVIEW_DIR), '', self.title))

    series = Series()

    try :
        prev = None

        for i, key in enumerate(keys) :
            img = Image(series, key, i)

            if prev :
                prev.next = img

            img.prev = prev
            prev = img

            series.sorted_images.append(img)
    finally :
        db.close()

    if index :
        img = series.sorted_images[index - 1]

        img.render()
    else :
        series.render()

    print "Content-Type: text/html"
    print
    print rendered_templates[0]