de-cgi-bin/taggr2.py
author terom
Thu, 17 Jan 2008 01:56:04 +0000
changeset 23 10841abbc01f
child 24 001f52cd057e
permissions -rwxr-xr-x
taggr2, which is starting to shape up
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
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    30
tpl = template.Template("taggr")
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    31
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    32
act = req.get_str("act", None)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    33
path = req.get_str("path", None)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    34
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    35
print "Content-Type: text/html"
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    36
print
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    37
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    38
def dirlist (path) :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    39
    if '.' in path[1:] :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    40
        raise ValueError("invalid path")
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    41
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    42
    dirs = []
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    43
    imgs = []
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    44
    
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    45
    for fname in os.listdir(path) :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    46
        fpath = os.path.join(path, fname).lstrip('.').lstrip('/')
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    47
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    48
        if fname.startswith('.') :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    49
            continue
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    50
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    51
        if fname in (settings.THUMB_DIR, settings.PREVIEW_DIR) :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    52
            continue
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    53
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    54
        if os.path.isdir(fpath) :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    55
            dirs.append((fname, fpath))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    56
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    57
        elif utils.isImage(fname) :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    58
            key = shorturl.int2key(db.select("""SELECT id FROM images WHERE dirpath=? AND filename=?""", path, fname).fetchone()[0])
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    59
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    60
            imgs.append((key, os.path.join(path, settings.THUMB_DIR, fname)))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    61
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    62
    dirs.sort()
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    63
    imgs.sort()
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    64
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    65
    return dirs, imgs
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    66
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    67
if act == "dirlist" :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    68
    dirs, imgs = dirlist(path)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    69
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    70
    print template.Template("taggr_dir").render(
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    71
        subdirs                 = dirs,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    72
        images                  = imgs,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    73
    )
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
elif act == None :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    76
    dirs, imgs = dirlist(".")
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    77
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    78
    if path :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    79
        open_dir = path
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    80
        open_dir_contents = dirlist(path)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    81
    else :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    82
        open_dir = open_dir_contents = None
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    83
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    84
    print template.Template("taggr").render(
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    85
        stylesheet_url          = utils.url("style.css", up=1),
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    86
        title                   = "Taggr",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    87
        breadcrumb              = [(utils.url(up=1), "Index"), (utils.url("."), "Taggr")],
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    88
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    89
        subdirs                 = dirs,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    90
        images                  = imgs,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    91
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    92
        open_dir                = open_dir,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    93
        open_dir_contents       = open_dir_contents,
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