de-cgi-bin/taggr2.py
author terom
Thu, 31 Jan 2008 19:19:05 +0000
changeset 30 b1d5c32ab771
parent 25 4b3cf12848c2
permissions -rwxr-xr-x
moar tweaks/small bugfixes
#!/usr/bin/env python2.5
#
# 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, os.path

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


tpl = template.Template("taggr")

act = req.get_str("act", None)
path = req.get_str("path", None)

print "Content-Type: text/html"
print

def dirlist (path) :
    if '.' in path[1:] :
        raise ValueError("invalid path")

    dirs = []
    imgs = []
    
    for fname in os.listdir(path) :
        fpath = os.path.join(path, fname).lstrip('.').lstrip('/')

        if fname.startswith('.') :
            continue

        if fname in (settings.THUMB_DIR, settings.PREVIEW_DIR) :
            continue

        if os.path.isdir(fpath) :
            dirs.append((fname, fpath))

        elif utils.isImage(fname) :
            id = db.select("""SELECT id FROM images WHERE dirpath=? AND filename=?""", path, fname).fetchone()[0]

            tags = [tag for type, tag in db.select("""SELECT type, tag FROM tags WHERE image=?""", id)]

            imgs.append((id, os.path.join(path, settings.THUMB_DIR, fname), tags))

    dirs.sort()
    imgs.sort()

    return dirs, imgs

if act == "dirlist" :
    dirs, imgs = dirlist(path)

    print template.Template("taggr_dir").render(
        subdirs                 = dirs,
        images                  = imgs,
    )

elif act == "tag" :
    img_list = req.get_int_list("img")
    tag_list = req.get_str_list("tag")

    db.insert_many("""INSERT INTO tags (image, tag) VALUES (?, ?)""", ((img, tag) for img in img_list for tag in tag_list))

    print "OK"

elif act == "untag" :
    img = req.get_int("img")
    tag = req.get_str("tag")

    db.delete("""DELETE FROM tags WHERE image=? AND tag=?""", img, tag)

    print "OK"

elif act == None :
    dirs, imgs = dirlist(".")

    if path :
        open_dir = path
        open_dir_contents = dirlist(path)
    else :
        open_dir = open_dir_contents = None
    
    print template.Template("taggr").render(
        stylesheet_url          = utils.url("style.css", up=1),
        title                   = "Taggr",
        breadcrumb              = [(utils.url(up=1), "Index"), (utils.url("."), "Taggr")],

        subdirs                 = dirs,
        images                  = imgs,

        open_dir                = open_dir,
        open_dir_contents       = open_dir_contents,
    )