de-cgi-bin/series.py
author terom
Wed, 16 Jan 2008 18:44:03 +0000
changeset 22 72696ca68c34
parent 19 8d3ffd87cb0b
permissions -rwxr-xr-x
use sqlite3 instead of bdb, series and shorturl should still work with this (adding new images won't yet)
#!/usr/bin/env python2.4
#
# DeGAL - A pretty simple web image gallery
# Copyright (C) 2007 Tero Marttila
# http://marttila.de/~terom/degal/
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

import os
import cgi
import Cookie

import inc
from lib import shorturl, template, utils, settings

#
# 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
    
    index = fname = None

    if arg2 :
        try :
            index = int(arg2)
        except ValueError :
            fname = arg2

    # our custom Series/Image classes, because they do act slightly differently

    class Series (object) :
        def __init__ (self, keys) :
            self.images = []
            prev = None

            self.image_dict = dict()

            images = shorturl.get_images(keys)

            for index, (key, (dir, fname)) in enumerate(zip(keys, images)) :
                img = Image(self, key, dir, fname, index)
                self.images.append(img)
                self.image_dict[fname] = img

                img.prev = prev

                if prev :
                    prev.next = img

                prev = img

        def render (self) :
            if my_series :
                descr = '<a href="../clear/" rel="nofollow">Clear your series</a>'
            else :
                descr = '<a href="load" rel="nofollow">Load as your series</a>'
   
            return template.gallery.render(
                stylesheet_url      = utils.url("style.css", up=2),
                
                breadcrumb          = [(utils.url(up=1), "Index"), (utils.url(), "Series")],

                dirs                = None,
                title               = "Series",

                num_pages           = 1,
                cur_page            = 0,

                images              = self.images,

                description         = descr,

                shorturl            = None,
                shorturl_code       = None,
            )
    
    class Image (object) :
        def __init__ (self, series, key, dir, fname, index) :
            self.fname = fname
            self.name = utils.url_join(dir, fname, abs=True)
            self.html_name = utils.url(fname)
            self.real_html_name = utils.url_join(dir, fname + ".html", abs=True)

            self.thumb_name = utils.url_join(dir, settings.THUMB_DIR, fname, abs=True)
            self.preview_name = utils.url_join(dir, settings.PREVIEW_DIR, fname, abs=True)

            self.shorturl = key

            self.prev = self.next = None

        def render (self) :
            descr = '<span style="font-size: x-small"><a href="%s.html">Standalone image</a></span>' % self.real_html_name
            
            if my_series :
                series_url = utils.url_join("del", self.shorturl, up=1)
                series_verb = "Remove from"
            else :
                series_url = series_verb = ""

            return template.image.render(
                stylesheet_url      = utils.url("style.css", up=3),
                
                breadcrumb          = [(utils.url(up=2), "Index"), (utils.url("."), "Series"), (self.html_name, self.fname)],

                title               = self.fname,

                prev                = self.prev,
                img                 = self,
                next                = self.next,
                
                description         = descr,
    
                img_size            = None,
                file_size           = None,
                timestamp           = None,
                
                shorturl            = utils.url_join("s", self.shorturl, abs=True),
                shorturl_code       = self.shorturl,
                
                series_url          = series_url,
                series_verb         = series_verb,
            )
    
    series = Series(keys)

    if fname :
        html = series.image_dict[fname].render()
    elif index :
        html = series.images[index - 1].render()
    else :
        html = series.render()

    print "Content-Type: text/html"
    print
    print html