terom@23: #!/usr/bin/env python2.5 terom@23: # terom@23: # DeGAL - A pretty simple web image gallery terom@23: # Copyright (C) 2007 Tero Marttila terom@23: # http://marttila.de/~terom/degal/ terom@23: # terom@23: # This program is free software; you can redistribute it and/or modify terom@23: # it under the terms of the GNU General Public License as published by terom@23: # the Free Software Foundation; either version 2 of the License, or terom@23: # (at your option) any later version. terom@23: # terom@23: # This program is distributed in the hope that it will be useful, terom@23: # but WITHOUT ANY WARRANTY; without even the implied warranty of terom@23: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terom@23: # GNU General Public License for more details. terom@23: # terom@23: # You should have received a copy of the GNU General Public License terom@23: # along with this program; if not, write to the terom@23: # Free Software Foundation, Inc., terom@23: # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. terom@23: # terom@23: terom@23: import os, os.path terom@23: terom@23: import inc terom@23: from lib import db, shorturl, settings, utils, template, req terom@23: terom@23: terom@23: tpl = template.Template("taggr") terom@23: terom@23: act = req.get_str("act", None) terom@23: path = req.get_str("path", None) terom@23: terom@23: print "Content-Type: text/html" terom@23: print terom@23: terom@23: def dirlist (path) : terom@23: if '.' in path[1:] : terom@23: raise ValueError("invalid path") terom@23: terom@23: dirs = [] terom@23: imgs = [] terom@23: terom@23: for fname in os.listdir(path) : terom@23: fpath = os.path.join(path, fname).lstrip('.').lstrip('/') terom@23: terom@23: if fname.startswith('.') : terom@23: continue terom@23: terom@23: if fname in (settings.THUMB_DIR, settings.PREVIEW_DIR) : terom@23: continue terom@23: terom@23: if os.path.isdir(fpath) : terom@23: dirs.append((fname, fpath)) terom@23: terom@23: elif utils.isImage(fname) : terom@24: id = db.select("""SELECT id FROM images WHERE dirpath=? AND filename=?""", path, fname).fetchone()[0] terom@23: terom@24: tags = [tag for type, tag in db.select("""SELECT type, tag FROM tags WHERE image=?""", id)] terom@24: terom@24: imgs.append((id, os.path.join(path, settings.THUMB_DIR, fname), tags)) terom@23: terom@23: dirs.sort() terom@23: imgs.sort() terom@23: terom@23: return dirs, imgs terom@23: terom@23: if act == "dirlist" : terom@23: dirs, imgs = dirlist(path) terom@23: terom@23: print template.Template("taggr_dir").render( terom@23: subdirs = dirs, terom@23: images = imgs, terom@23: ) terom@23: terom@24: elif act == "tag" : terom@24: img_list = req.get_int_list("img") terom@25: tag_list = req.get_str_list("tag") terom@24: terom@25: db.insert_many("""INSERT INTO tags (image, tag) VALUES (?, ?)""", ((img, tag) for img in img_list for tag in tag_list)) terom@24: terom@24: print "OK" terom@24: terom@24: elif act == "untag" : terom@24: img = req.get_int("img") terom@24: tag = req.get_str("tag") terom@24: terom@24: db.delete("""DELETE FROM tags WHERE image=? AND tag=?""", img, tag) terom@24: terom@24: print "OK" terom@24: terom@23: elif act == None : terom@23: dirs, imgs = dirlist(".") terom@23: terom@23: if path : terom@23: open_dir = path terom@23: open_dir_contents = dirlist(path) terom@23: else : terom@23: open_dir = open_dir_contents = None terom@24: terom@23: print template.Template("taggr").render( terom@23: stylesheet_url = utils.url("style.css", up=1), terom@23: title = "Taggr", terom@23: breadcrumb = [(utils.url(up=1), "Index"), (utils.url("."), "Taggr")], terom@23: terom@23: subdirs = dirs, terom@23: images = imgs, terom@23: terom@23: open_dir = open_dir, terom@23: open_dir_contents = open_dir_contents, terom@23: ) terom@23: