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
23
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
     1
#!/usr/bin/env python2.5
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
     2
#
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
     3
# DeGAL - A pretty simple web image gallery
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
     4
# Copyright (C) 2007 Tero Marttila
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
     5
# http://marttila.de/~terom/degal/
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
     6
#
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
     7
# This program is free software; you can redistribute it and/or modify
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
     8
# it under the terms of the GNU General Public License as published by
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
     9
# the Free Software Foundation; either version 2 of the License, or
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    10
# (at your option) any later version.
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    11
#
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    12
# This program is distributed in the hope that it will be useful,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    15
# GNU General Public License for more details.
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    16
#
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    17
# You should have received a copy of the GNU General Public License
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    18
# along with this program; if not, write to the
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    19
# Free Software Foundation, Inc.,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    20
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    21
#
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    22
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    23
import os, os.path
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    24
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    25
import inc
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    26
from lib import db, shorturl, settings, utils, template, req
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    27
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    28
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    29
tpl = template.Template("taggr")
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    30
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    31
act = req.get_str("act", None)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    32
path = req.get_str("path", None)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    33
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    34
print "Content-Type: text/html"
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    35
print
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    36
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    37
def dirlist (path) :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    38
    if '.' in path[1:] :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    39
        raise ValueError("invalid path")
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    40
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    41
    dirs = []
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    42
    imgs = []
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    43
    
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    44
    for fname in os.listdir(path) :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    45
        fpath = os.path.join(path, fname).lstrip('.').lstrip('/')
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    46
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    47
        if fname.startswith('.') :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    48
            continue
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    49
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    50
        if fname in (settings.THUMB_DIR, settings.PREVIEW_DIR) :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    51
            continue
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    52
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    53
        if os.path.isdir(fpath) :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    54
            dirs.append((fname, fpath))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    55
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    56
        elif utils.isImage(fname) :
24
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 23
diff changeset
    57
            id = db.select("""SELECT id FROM images WHERE dirpath=? AND filename=?""", path, fname).fetchone()[0]
23
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    58
24
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 23
diff changeset
    59
            tags = [tag for type, tag in db.select("""SELECT type, tag FROM tags WHERE image=?""", id)]
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 23
diff changeset
    60
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 23
diff changeset
    61
            imgs.append((id, os.path.join(path, settings.THUMB_DIR, fname), tags))
23
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    62
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    63
    dirs.sort()
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    64
    imgs.sort()
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    65
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    66
    return dirs, imgs
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    67
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    68
if act == "dirlist" :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    69
    dirs, imgs = dirlist(path)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    70
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    71
    print template.Template("taggr_dir").render(
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    72
        subdirs                 = dirs,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    73
        images                  = imgs,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    74
    )
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    75
24
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 23
diff changeset
    76
elif act == "tag" :
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 23
diff changeset
    77
    img_list = req.get_int_list("img")
25
4b3cf12848c2 tweaks/bugfixes (e.g. support adding multiple tags to an image at once)
terom
parents: 24
diff changeset
    78
    tag_list = req.get_str_list("tag")
24
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 23
diff changeset
    79
25
4b3cf12848c2 tweaks/bugfixes (e.g. support adding multiple tags to an image at once)
terom
parents: 24
diff changeset
    80
    db.insert_many("""INSERT INTO tags (image, tag) VALUES (?, ?)""", ((img, tag) for img in img_list for tag in tag_list))
24
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 23
diff changeset
    81
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 23
diff changeset
    82
    print "OK"
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 23
diff changeset
    83
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 23
diff changeset
    84
elif act == "untag" :
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 23
diff changeset
    85
    img = req.get_int("img")
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 23
diff changeset
    86
    tag = req.get_str("tag")
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 23
diff changeset
    87
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 23
diff changeset
    88
    db.delete("""DELETE FROM tags WHERE image=? AND tag=?""", img, tag)
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 23
diff changeset
    89
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 23
diff changeset
    90
    print "OK"
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 23
diff changeset
    91
23
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    92
elif act == None :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    93
    dirs, imgs = dirlist(".")
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    94
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    95
    if path :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    96
        open_dir = path
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    97
        open_dir_contents = dirlist(path)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    98
    else :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    99
        open_dir = open_dir_contents = None
24
001f52cd057e tagging/untagging should now work fully in taggr
terom
parents: 23
diff changeset
   100
    
23
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   101
    print template.Template("taggr").render(
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   102
        stylesheet_url          = utils.url("style.css", up=1),
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   103
        title                   = "Taggr",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   104
        breadcrumb              = [(utils.url(up=1), "Index"), (utils.url("."), "Taggr")],
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   105
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   106
        subdirs                 = dirs,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   107
        images                  = imgs,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   108
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   109
        open_dir                = open_dir,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   110
        open_dir_contents       = open_dir_contents,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   111
    )
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   112