de-cgi-bin/tags.py
author terom
Thu, 31 Jan 2008 19:19:05 +0000
changeset 30 b1d5c32ab771
parent 20 6c774496bb00
permissions -rwxr-xr-x
moar tweaks/small bugfixes
20
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
     1
#!/usr/bin/env python2.4
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
     2
#
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
     3
# DeGAL - A pretty simple web image gallery
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
     4
# Copyright (C) 2007 Tero Marttila
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
     5
# http://marttila.de/~terom/degal/
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
     6
#
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
     7
# This program is free software; you can redistribute it and/or modify
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
     8
# it under the terms of the GNU General Public License as published by
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
     9
# the Free Software Foundation; either version 2 of the License, or
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    10
# (at your option) any later version.
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    11
#
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    12
# This program is distributed in the hope that it will be useful,
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    15
# GNU General Public License for more details.
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    16
#
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    17
# You should have received a copy of the GNU General Public License
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    18
# along with this program; if not, write to the
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    19
# Free Software Foundation, Inc.,
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    20
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    21
#
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    22
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    23
import cgi
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    24
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    25
import inc
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    26
from lib import tags, template, req, shorturl, utils, settings
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    27
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    28
tag_list = [tag for tag in req.get_str("tags", "").split("/") if tag]
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    29
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    30
tag_db = tags.TagDB()
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    31
shorturl_db = shorturl.DB()
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    32
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    33
root = len(tag_list) + 1
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    34
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    35
class Tag (object) :
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    36
    def __init__ (self, name, count) :
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    37
        self.title = "%s (%d)" % (name, count)
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    38
        self.name = utils.url(name, trailing=True)
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    39
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    40
class TagView (object) :
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    41
    def __init__ (self, tag_list) :
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    42
        img_codes = tag_db.imgs(tag_list)
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    43
        more_tags = tag_db.tags(tag_list)
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    44
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    45
        self.images = []
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    46
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    47
        for index, key in enumerate(img_codes) :
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    48
            dir, fname = shorturl_db.image_info(key)
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    49
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    50
            img = Image(key, dir, fname, index)
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    51
            self.images.append(img)
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    52
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    53
        self.tags = [Tag(name, count) for (name, count) in more_tags]
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    54
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    55
    def render (self) :
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    56
        descr = ""
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    57
        return template.gallery.render(
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    58
            stylesheet_url      = utils.url("style.css", up=root),
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    59
            
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    60
            breadcrumb          = [(utils.url(up=root), "Index"), (utils.url(up=root-1), "Tags")] + [(utils.url(up=root-(n+1), trailing=True), tag) for n, tag in enumerate(tag_list)],
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    61
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    62
            title               = "Tags :: %s" % (" » ".join(tag_list) or "All"),
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    63
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    64
            num_pages           = 1,
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    65
            cur_page            = 0,
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    66
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    67
            dirs                = self.tags,
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    68
            images              = self.images,
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    69
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    70
            description         = descr,
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    71
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    72
            shorturl            = None,
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    73
            shorturl_code       = None,
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    74
        )
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    75
    
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    76
class Image (object) :
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    77
    def __init__ (self, key, dir, fname, index) :
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    78
        self.fname = fname
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    79
        self.name = utils.url_join(dir, fname, abs=True)
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    80
        self.html_name = utils.url_join(dir, fname + ".html", abs=True)
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    81
        self.thumb_name = utils.url_join(dir, settings.THUMB_DIR, fname, abs=True)
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    82
        self.preview_name = utils.url_join(dir, settings.PREVIEW_DIR, fname, abs=True)
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    83
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    84
        self.shorturl = key
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    85
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    86
        self.prev = self.next = None
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    87
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    88
tagview = TagView(tag_list)
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    89
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    90
print "Content-Type: text/html"
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    91
print
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    92
print tagview.render()
6c774496bb00 a rather silly shelve-based tagging thing, commiting before I scrap the code and start over with SQLite
terom
parents:
diff changeset
    93