series.cgi
author terom
Thu, 08 Nov 2007 17:06:03 +0000
changeset 1 740133ab6353
child 3 9637b8f24005
permissions -rwxr-xr-x
initial code, somewhere between 0.2 and 0.5
1
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
     1
#!/usr/bin/env python2.4
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
     2
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
     3
import dbm
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
     4
import cgi
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
     5
import Cookie
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
     6
import os, os.path
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
     7
#import pprint
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
     8
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
     9
import degal
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    10
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    11
#
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    12
# load request params
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    13
#
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    14
vars = cgi.FieldStorage()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    15
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    16
# keys = what photos are in the series
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    17
keys = vars["keys"].value.split()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    18
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    19
# index = what photo to show, or the index
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    20
if 'index' in vars :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    21
    index = vars["index"].value
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    22
else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    23
    index = None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    24
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    25
if keys and keys[0] in ('add', 'del', 'clear', 'view') or index == 'load' :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    26
    cookie = Cookie.SimpleCookie(os.environ.get('HTTP_COOKIE', None))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    27
    act = keys[0]
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    28
    _keys = keys
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    29
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    30
    if 'series' in cookie :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    31
        keys = cookie["series"].value.split()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    32
    else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    33
        keys = []
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    34
    
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    35
    if index == 'load' :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    36
        keys = _keys
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    37
    elif act == 'add' and index not in keys :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    38
        keys.append(index)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    39
    elif act == 'del' :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    40
        keys.remove(index)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    41
    elif act == 'clear' :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    42
        keys = []
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    43
    elif act == 'view' :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    44
        pass
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    45
    
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    46
    cookie['series'] = ' '.join(keys)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    47
    cookie['series']['path'] = '/'
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    48
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    49
    if keys :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    50
        redirect_to = "../%s/" % ('+'.join(keys))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    51
    else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    52
        redirect_to = "../.."
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    53
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    54
    print "Status: 302"
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    55
    print "Location: %s" % redirect_to
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    56
    print cookie
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    57
    print
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    58
    print "Redirect..."
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    59
else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    60
    cookie = Cookie.SimpleCookie(os.environ.get('HTTP_COOKIE', None))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    61
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    62
    my_series = 'series' in cookie and cookie['series'].value.split() == keys 
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    63
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    64
    if index :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    65
        index = int(index)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    66
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    67
    #
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    68
    # load DB
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    69
    #
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    70
    db = dbm.open('shorturls', 'r')
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    71
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    72
    #
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    73
    # get the Image objects
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    74
    #
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    75
    photos = []
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    76
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    77
    INDEX_URL = "/series/%s/"
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    78
    IMAGE_URL = "/series/%s/%d"
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    79
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    80
    # monkey-patch the templates
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    81
    rendered_templates = []
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    82
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    83
    def _myRenderTo (self, path, **vars) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    84
        rendered_templates.append(self.render(**vars))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    85
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    86
    degal.Template.renderTo = _myRenderTo
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    87
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    88
    # our own version of Folder
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    89
    class Series (degal.Folder) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    90
        def __init__ (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    91
            super(Series, self).__init__()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    92
            
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    93
            self.alive = True
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    94
            self.filtered = False
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    95
            self.subdirs = {}
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    96
            self.images = {}
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    97
            self.sorted_images = []
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    98
            self.title = "Series"
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
    99
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   100
            if my_series :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   101
                self.descr = '<a href="../clear/">Clear your series</a>'
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   102
            else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   103
                self.descr = '<a href="load">Load as your series</a>'
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   104
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   105
            self.shorturl_code = ''
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   106
        
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   107
        def breadcrumb (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   108
            return [('../..', 'Gallery'), ('.', 'Series')]
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   109
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   110
        def inRoot (self, *fnames) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   111
            return os.path.join('../..', *fnames)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   112
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   113
    #    def pathFor (self, *fnames) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   114
    #        return os.path.join(fnames)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   115
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   116
    class Image (degal.Image) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   117
        def __init__ (self, series, key, i) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   118
            path = db[key]
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   119
            path = path.rstrip(".html")
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   120
            self.dir_name, self.image_name = os.path.split(path)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   121
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   122
            super(Image, self).__init__(series, self.image_name)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   123
            
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   124
            self.path = path
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   125
            self.shorturl_code = key
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   126
            self.html_path = self.html_name = str(i + 1)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   127
            self.title = 'Image %d' % (i + 1, )
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   128
            self.descr = '<span style="font-size: x-small">Standalone image: <a href="%s.html">%s</a></span>' % (self._path(), self._path().lstrip('./'))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   129
            self.img_size = (-1, -1)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   130
            self.filesize = 0
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   131
            self.timestamp = 0
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   132
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   133
            if my_series :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   134
                self.series_act = "del"
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   135
                self.series_verb = "Remove from"
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   136
            else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   137
                self.series_act = self.series_verb = ""
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   138
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   139
        def breadcrumb (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   140
            return [('../..', 'Gallery'), ('.', 'Series'), (self.html_name, self.title)]
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   141
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   142
        def _path (self, blaa='') :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   143
            return os.path.join('../..', self.dir_name, blaa, self.image_name)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   144
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   145
        def thumbImgTag (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   146
            return degal.link(self.html_name, "<img src='%s' alt='%s' title='%s'>" % (os.path.join('../..', self.dir_name, degal.THUMB_DIR, self.image_name), '', self.title))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   147
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   148
        def previewImgTag (self) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   149
            return degal.link(os.path.join('../..', self.dir_name, self.image_name), "<img src='%s' alt='%s' title='%s'>" % (os.path.join('../..', self.dir_name, degal.PREVIEW_DIR, self.image_name), '', self.title))
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   150
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   151
           
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   152
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   153
    series = Series()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   154
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   155
    try :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   156
        prev = None
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   157
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   158
        for i, key in enumerate(keys) :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   159
            img = Image(series, key, i)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   160
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   161
            if prev :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   162
                prev.next = img
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   163
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   164
            img.prev = prev
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   165
            prev = img
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   166
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   167
    #        series.images[str(i + 1)] = img
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   168
            series.sorted_images.append(img)
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   169
    finally :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   170
        db.close()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   171
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   172
    if index :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   173
        img = series.sorted_images[index - 1]
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   174
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   175
        img.render()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   176
    else :
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   177
        series.render()
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   178
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   179
    print "Content-Type: text/html"
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   180
    print
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   181
    print rendered_templates[0]
740133ab6353 initial code, somewhere between 0.2 and 0.5
terom
parents:
diff changeset
   182