terom@1: #!/usr/bin/env python2.4
terom@12: #
terom@12: # DeGAL - A pretty simple web image gallery
terom@12: # Copyright (C) 2007 Tero Marttila
terom@12: # http://marttila.de/~terom/degal/
terom@12: #
terom@12: # This program is free software; you can redistribute it and/or modify
terom@12: # it under the terms of the GNU General Public License as published by
terom@12: # the Free Software Foundation; either version 2 of the License, or
terom@12: # (at your option) any later version.
terom@12: #
terom@12: # This program is distributed in the hope that it will be useful,
terom@12: # but WITHOUT ANY WARRANTY; without even the implied warranty of
terom@12: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
terom@12: # GNU General Public License for more details.
terom@12: #
terom@12: # You should have received a copy of the GNU General Public License
terom@12: # along with this program; if not, write to the
terom@12: # Free Software Foundation, Inc.,
terom@12: # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
terom@12: #
terom@4:
terom@3: import shelve
terom@1: import cgi
terom@1: import Cookie
terom@1: import os, os.path
terom@1:
terom@1: import degal
terom@1:
terom@1: #
terom@1: # load request params
terom@1: #
terom@1: vars = cgi.FieldStorage()
terom@1:
terom@3: # these are interpeted different ways, hence the generic naming
terom@3: arg1 = vars["keys"].value
terom@1: if 'index' in vars :
terom@3: arg2 = vars["index"].value
terom@1: else :
terom@3: arg2 = None
terom@1:
terom@3: # the cookie with the user's current series
terom@3: cookie = Cookie.SimpleCookie(os.environ.get('HTTP_COOKIE', None))
terom@1:
terom@3: # a special action?
terom@3: if arg1 and arg1 in ('add', 'del', 'clear', 'view') or arg2 == 'load' :
terom@3: # load the keys from the cookie
terom@1: if 'series' in cookie :
terom@1: keys = cookie["series"].value.split()
terom@1: else :
terom@1: keys = []
terom@1:
terom@3: if arg2 == 'load' :
terom@3: # set the keys in the user's cookie to those in the URL
terom@3: keys = arg1.split()
terom@3:
terom@3: elif arg1 == 'add' and arg2 not in keys :
terom@3: # add a code to the list of keys
terom@3: keys.append(arg2)
terom@3:
terom@3: elif arg1 == 'del' and arg2 in keys :
terom@3: # remove a key from the list of keys
terom@3: keys.remove(arg2)
terom@3:
terom@3: elif arg1 == 'clear' :
terom@3: # clear out the set of keys
terom@1: keys = []
terom@3:
terom@3: elif arg1 == 'view' :
terom@3: # just view them
terom@1: pass
terom@3:
terom@3: # set the series cookie value
terom@1: cookie['series'] = ' '.join(keys)
terom@1: cookie['series']['path'] = '/'
terom@3:
terom@3: # if we have keys, redirect to them, otherwise, back to index we go
terom@1: if keys :
terom@1: redirect_to = "../%s/" % ('+'.join(keys))
terom@1: else :
terom@1: redirect_to = "../.."
terom@3:
terom@3: # do the redirect
terom@1: print "Status: 302"
terom@1: print "Location: %s" % redirect_to
terom@1: print cookie
terom@1: print
terom@1: print "Redirect..."
terom@1: else :
terom@3: # we're just viewing
terom@3: keys = arg1.split()
terom@3:
terom@3: # is this "My Series"?
terom@3: my_series = 'series' in cookie and cookie['series'].value.split() == keys
terom@1:
terom@3: if arg2 :
terom@3: index = int(arg2)
terom@3: else :
terom@3: index = None
terom@1:
terom@1: # load DB
terom@3: db = shelve.open('shorturls2', 'r')
terom@1:
terom@1: # get the Image objects
terom@1: photos = []
terom@3:
terom@3: #
terom@3: # Start ugly code-misreuse
terom@3: #
terom@1:
terom@1: # monkey-patch the templates
terom@1: rendered_templates = []
terom@1:
terom@1: def _myRenderTo (self, path, **vars) :
terom@1: rendered_templates.append(self.render(**vars))
terom@1:
terom@3: # lalalalala... ooh, look;
terom@3:
terom@1: degal.Template.renderTo = _myRenderTo
terom@3: #
terom@3: # vvvvvvvvvvvvvvvvvvvv
terom@3: # vvvvvvvvvvvvvvvvvvvv
terom@3: #
terom@3: # >>>> SHINY PINK ELEPHANT! <<<<
terom@3: #
terom@3: # ^^^^^^^^^^^^^^^^^^^^
terom@3: # ^^^^^^^^^^^^^^^^^^^^
terom@1:
terom@1: # our own version of Folder
terom@1: class Series (degal.Folder) :
terom@1: def __init__ (self) :
terom@1: super(Series, self).__init__()
terom@1:
terom@1: self.alive = True
terom@1: self.filtered = False
terom@1: self.subdirs = {}
terom@1: self.images = {}
terom@1: self.sorted_images = []
terom@1: self.title = "Series"
terom@1:
terom@1: if my_series :
terom@4: self.descr = 'Clear your series'
terom@1: else :
terom@4: self.descr = 'Load as your series'
terom@1:
terom@1: self.shorturl_code = ''
terom@1:
terom@1: def breadcrumb (self) :
terom@1: return [('../..', 'Gallery'), ('.', 'Series')]
terom@1:
terom@1: def inRoot (self, *fnames) :
terom@1: return os.path.join('../..', *fnames)
terom@1:
terom@1: class Image (degal.Image) :
terom@1: def __init__ (self, series, key, i) :
terom@3: type, self.dir_name, self.image_name = db[key]
terom@1:
terom@1: super(Image, self).__init__(series, self.image_name)
terom@1:
terom@3: self.path = self._path()
terom@1: self.shorturl_code = key
terom@1: self.html_path = self.html_name = str(i + 1)
terom@1: self.title = 'Image %d' % (i + 1, )
terom@1: self.descr = 'Standalone image: %s' % (self._path(), self._path().lstrip('./'))
terom@1: self.img_size = (-1, -1)
terom@1: self.filesize = 0
terom@1: self.timestamp = 0
terom@1:
terom@1: if my_series :
terom@1: self.series_act = "del"
terom@1: self.series_verb = "Remove from"
terom@1: else :
terom@1: self.series_act = self.series_verb = ""
terom@1:
terom@1: def breadcrumb (self) :
terom@1: return [('../..', 'Gallery'), ('.', 'Series'), (self.html_name, self.title)]
terom@1:
terom@1: def _path (self, blaa='') :
terom@1: return os.path.join('../..', self.dir_name, blaa, self.image_name)
terom@1:
terom@1: def thumbImgTag (self) :
terom@3: return degal.link(self.html_name, "" % (self._path(degal.THUMB_DIR), '', self.title))
terom@1:
terom@1: def previewImgTag (self) :
terom@3: return degal.link(self._path(), "" % (self._path(degal.PREVIEW_DIR), '', self.title))
terom@1:
terom@1: series = Series()
terom@1:
terom@1: try :
terom@1: prev = None
terom@1:
terom@1: for i, key in enumerate(keys) :
terom@1: img = Image(series, key, i)
terom@1:
terom@1: if prev :
terom@1: prev.next = img
terom@1:
terom@1: img.prev = prev
terom@1: prev = img
terom@1:
terom@1: series.sorted_images.append(img)
terom@1: finally :
terom@1: db.close()
terom@1:
terom@1: if index :
terom@1: img = series.sorted_images[index - 1]
terom@1:
terom@1: img.render()
terom@1: else :
terom@1: series.render()
terom@1:
terom@1: print "Content-Type: text/html"
terom@1: print
terom@1: print rendered_templates[0]
terom@1: