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