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@19: import os terom@1: import cgi terom@1: import Cookie terom@1: terom@19: import inc terom@19: from lib import shorturl, template, utils, settings 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@19: terom@19: index = fname = None terom@19: terom@3: if arg2 : terom@19: try : terom@19: index = int(arg2) terom@19: except ValueError : terom@19: fname = arg2 terom@1: terom@19: # our custom Series/Image classes, because they do act slightly differently terom@1: terom@19: class Series (object) : terom@19: def __init__ (self, keys) : terom@19: self.images = [] terom@19: prev = None terom@1: terom@19: self.image_dict = dict() terom@1: terom@22: images = shorturl.get_images(keys) terom@1: terom@22: for index, (key, (dir, fname)) in enumerate(zip(keys, images)) : terom@19: img = Image(self, key, dir, fname, index) terom@19: self.images.append(img) terom@19: self.image_dict[fname] = img terom@1: terom@19: img.prev = prev terom@1: terom@19: if prev : terom@19: prev.next = img terom@19: terom@19: prev = img terom@19: terom@19: def render (self) : terom@19: if my_series : terom@19: descr = 'Clear your series' terom@19: else : terom@19: descr = 'Load as your series' terom@19: terom@19: return template.gallery.render( terom@19: stylesheet_url = utils.url("style.css", up=2), terom@19: terom@19: breadcrumb = [(utils.url(up=1), "Index"), (utils.url(), "Series")], terom@19: terom@19: dirs = None, terom@19: title = "Series", terom@19: terom@19: num_pages = 1, terom@19: cur_page = 0, terom@19: terom@19: images = self.images, terom@19: terom@19: description = descr, terom@19: terom@19: shorturl = None, terom@19: shorturl_code = None, terom@19: ) terom@19: terom@19: class Image (object) : terom@19: def __init__ (self, series, key, dir, fname, index) : terom@19: self.fname = fname terom@19: self.name = utils.url_join(dir, fname, abs=True) terom@19: self.html_name = utils.url(fname) terom@19: self.real_html_name = utils.url_join(dir, fname + ".html", abs=True) terom@19: terom@19: self.thumb_name = utils.url_join(dir, settings.THUMB_DIR, fname, abs=True) terom@19: self.preview_name = utils.url_join(dir, settings.PREVIEW_DIR, fname, abs=True) terom@19: terom@19: self.shorturl = key terom@19: terom@19: self.prev = self.next = None terom@19: terom@19: def render (self) : terom@19: descr = 'Standalone image' % self.real_html_name terom@19: terom@19: if my_series : terom@19: series_url = utils.url_join("del", self.shorturl, up=1) terom@19: series_verb = "Remove from" terom@19: else : terom@19: series_url = series_verb = "" terom@19: terom@19: return template.image.render( terom@19: stylesheet_url = utils.url("style.css", up=3), terom@19: terom@19: breadcrumb = [(utils.url(up=2), "Index"), (utils.url("."), "Series"), (self.html_name, self.fname)], terom@19: terom@19: title = self.fname, terom@19: terom@19: prev = self.prev, terom@19: img = self, terom@19: next = self.next, terom@19: terom@19: description = descr, terom@19: terom@19: img_size = None, terom@19: file_size = None, terom@19: timestamp = None, terom@19: terom@19: shorturl = utils.url_join("s", self.shorturl, abs=True), terom@19: shorturl_code = self.shorturl, terom@19: terom@19: series_url = series_url, terom@19: series_verb = series_verb, terom@19: ) terom@19: terom@19: series = Series(keys) terom@19: terom@19: if fname : terom@19: html = series.image_dict[fname].render() terom@19: elif index : terom@19: html = series.images[index - 1].render() terom@1: else : terom@19: html = series.render() terom@1: terom@1: print "Content-Type: text/html" terom@1: print terom@19: print html terom@1: