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