author | terom |
Sat, 22 Dec 2007 19:19:05 +0000 | |
changeset 17 | adde6ad8731e |
parent 12 | series.cgi@c2d8e9a754a1 |
permissions | -rwxr-xr-x |
1 | 1 |
#!/usr/bin/env python2.4 |
12
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
4
diff
changeset
|
2 |
# |
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
4
diff
changeset
|
3 |
# DeGAL - A pretty simple web image gallery |
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
4
diff
changeset
|
4 |
# Copyright (C) 2007 Tero Marttila |
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
4
diff
changeset
|
5 |
# http://marttila.de/~terom/degal/ |
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
4
diff
changeset
|
6 |
# |
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
4
diff
changeset
|
7 |
# This program is free software; you can redistribute it and/or modify |
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
4
diff
changeset
|
8 |
# it under the terms of the GNU General Public License as published by |
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
4
diff
changeset
|
9 |
# the Free Software Foundation; either version 2 of the License, or |
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
4
diff
changeset
|
10 |
# (at your option) any later version. |
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
4
diff
changeset
|
11 |
# |
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
4
diff
changeset
|
12 |
# This program is distributed in the hope that it will be useful, |
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
4
diff
changeset
|
13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
4
diff
changeset
|
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
4
diff
changeset
|
15 |
# GNU General Public License for more details. |
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
4
diff
changeset
|
16 |
# |
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
4
diff
changeset
|
17 |
# You should have received a copy of the GNU General Public License |
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
4
diff
changeset
|
18 |
# along with this program; if not, write to the |
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
4
diff
changeset
|
19 |
# Free Software Foundation, Inc., |
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
4
diff
changeset
|
20 |
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
c2d8e9a754a1
Major code restructuring. Version is now 0.5, templates use Mako, and the code is split off into several files under lib/
terom
parents:
4
diff
changeset
|
21 |
# |
4
d46ab092d2b2
add rel="nofollow" to ShortURL and series-related links, googlebot doesn't need to follow them
terom
parents:
3
diff
changeset
|
22 |
|
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
23 |
import shelve |
1 | 24 |
import cgi |
25 |
import Cookie |
|
26 |
import os, os.path |
|
27 |
||
28 |
import degal |
|
29 |
||
30 |
# |
|
31 |
# load request params |
|
32 |
# |
|
33 |
vars = cgi.FieldStorage() |
|
34 |
||
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
35 |
# these are interpeted different ways, hence the generic naming |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
36 |
arg1 = vars["keys"].value |
1 | 37 |
if 'index' in vars : |
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
38 |
arg2 = vars["index"].value |
1 | 39 |
else : |
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
40 |
arg2 = None |
1 | 41 |
|
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
42 |
# the cookie with the user's current series |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
43 |
cookie = Cookie.SimpleCookie(os.environ.get('HTTP_COOKIE', None)) |
1 | 44 |
|
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
45 |
# a special action? |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
46 |
if arg1 and arg1 in ('add', 'del', 'clear', 'view') or arg2 == 'load' : |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
47 |
# load the keys from the cookie |
1 | 48 |
if 'series' in cookie : |
49 |
keys = cookie["series"].value.split() |
|
50 |
else : |
|
51 |
keys = [] |
|
52 |
||
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
53 |
if arg2 == 'load' : |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
54 |
# set the keys in the user's cookie to those in the URL |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
55 |
keys = arg1.split() |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
56 |
|
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
57 |
elif arg1 == 'add' and arg2 not in keys : |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
58 |
# add a code to the list of keys |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
59 |
keys.append(arg2) |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
60 |
|
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
61 |
elif arg1 == 'del' and arg2 in keys : |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
62 |
# remove a key from the list of keys |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
63 |
keys.remove(arg2) |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
64 |
|
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
65 |
elif arg1 == 'clear' : |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
66 |
# clear out the set of keys |
1 | 67 |
keys = [] |
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
68 |
|
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
69 |
elif arg1 == 'view' : |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
70 |
# just view them |
1 | 71 |
pass |
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
72 |
|
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
73 |
# set the series cookie value |
1 | 74 |
cookie['series'] = ' '.join(keys) |
75 |
cookie['series']['path'] = '/' |
|
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
76 |
|
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
77 |
# if we have keys, redirect to them, otherwise, back to index we go |
1 | 78 |
if keys : |
79 |
redirect_to = "../%s/" % ('+'.join(keys)) |
|
80 |
else : |
|
81 |
redirect_to = "../.." |
|
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
82 |
|
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
83 |
# do the redirect |
1 | 84 |
print "Status: 302" |
85 |
print "Location: %s" % redirect_to |
|
86 |
print cookie |
|
87 |
||
88 |
print "Redirect..." |
|
89 |
else : |
|
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
90 |
# we're just viewing |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
91 |
keys = arg1.split() |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
92 |
|
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
93 |
# is this "My Series"? |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
94 |
my_series = 'series' in cookie and cookie['series'].value.split() == keys |
1 | 95 |
|
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
96 |
if arg2 : |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
97 |
index = int(arg2) |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
98 |
else : |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
99 |
index = None |
1 | 100 |
|
101 |
# load DB |
|
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
102 |
db = shelve.open('shorturls2', 'r') |
1 | 103 |
|
104 |
# get the Image objects |
|
105 |
photos = [] |
|
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
106 |
|
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
107 |
# |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
108 |
# Start ugly code-misreuse |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
109 |
# |
1 | 110 |
|
111 |
# monkey-patch the templates |
|
112 |
rendered_templates = [] |
|
113 |
||
114 |
def _myRenderTo (self, path, **vars) : |
|
115 |
rendered_templates.append(self.render(**vars)) |
|
116 |
||
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
117 |
# lalalalala... ooh, look; |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
118 |
|
1 | 119 |
degal.Template.renderTo = _myRenderTo |
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
120 |
# |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
121 |
# vvvvvvvvvvvvvvvvvvvv |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
122 |
# vvvvvvvvvvvvvvvvvvvv |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
123 |
# |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
124 |
# >>>> SHINY PINK ELEPHANT! <<<< |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
125 |
# |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
126 |
# ^^^^^^^^^^^^^^^^^^^^ |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
127 |
# ^^^^^^^^^^^^^^^^^^^^ |
1 | 128 |
|
129 |
# our own version of Folder |
|
130 |
class Series (degal.Folder) : |
|
131 |
def __init__ (self) : |
|
132 |
super(Series, self).__init__() |
|
133 |
||
134 |
self.alive = True |
|
135 |
self.filtered = False |
|
136 |
self.subdirs = {} |
|
137 |
self.images = {} |
|
138 |
self.sorted_images = [] |
|
139 |
self.title = "Series" |
|
140 |
||
141 |
if my_series : |
|
4
d46ab092d2b2
add rel="nofollow" to ShortURL and series-related links, googlebot doesn't need to follow them
terom
parents:
3
diff
changeset
|
142 |
self.descr = '<a href="../clear/" rel="nofollow">Clear your series</a>' |
1 | 143 |
else : |
4
d46ab092d2b2
add rel="nofollow" to ShortURL and series-related links, googlebot doesn't need to follow them
terom
parents:
3
diff
changeset
|
144 |
self.descr = '<a href="load" rel="nofollow">Load as your series</a>' |
1 | 145 |
|
146 |
self.shorturl_code = '' |
|
147 |
||
148 |
def breadcrumb (self) : |
|
149 |
return [('../..', 'Gallery'), ('.', 'Series')] |
|
150 |
||
151 |
def inRoot (self, *fnames) : |
|
152 |
return os.path.join('../..', *fnames) |
|
153 |
||
154 |
class Image (degal.Image) : |
|
155 |
def __init__ (self, series, key, i) : |
|
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
156 |
type, self.dir_name, self.image_name = db[key] |
1 | 157 |
|
158 |
super(Image, self).__init__(series, self.image_name) |
|
159 |
||
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
160 |
self.path = self._path() |
1 | 161 |
self.shorturl_code = key |
162 |
self.html_path = self.html_name = str(i + 1) |
|
163 |
self.title = 'Image %d' % (i + 1, ) |
|
164 |
self.descr = '<span style="font-size: x-small">Standalone image: <a href="%s.html">%s</a></span>' % (self._path(), self._path().lstrip('./')) |
|
165 |
self.img_size = (-1, -1) |
|
166 |
self.filesize = 0 |
|
167 |
self.timestamp = 0 |
|
168 |
||
169 |
if my_series : |
|
170 |
self.series_act = "del" |
|
171 |
self.series_verb = "Remove from" |
|
172 |
else : |
|
173 |
self.series_act = self.series_verb = "" |
|
174 |
||
175 |
def breadcrumb (self) : |
|
176 |
return [('../..', 'Gallery'), ('.', 'Series'), (self.html_name, self.title)] |
|
177 |
||
178 |
def _path (self, blaa='') : |
|
179 |
return os.path.join('../..', self.dir_name, blaa, self.image_name) |
|
180 |
||
181 |
def thumbImgTag (self) : |
|
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
182 |
return degal.link(self.html_name, "<img src='%s' alt='%s' title='%s'>" % (self._path(degal.THUMB_DIR), '', self.title)) |
1 | 183 |
|
184 |
def previewImgTag (self) : |
|
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
185 |
return degal.link(self._path(), "<img src='%s' alt='%s' title='%s'>" % (self._path(degal.PREVIEW_DIR), '', self.title)) |
1 | 186 |
|
187 |
series = Series() |
|
188 |
||
189 |
try : |
|
190 |
prev = None |
|
191 |
||
192 |
for i, key in enumerate(keys) : |
|
193 |
img = Image(series, key, i) |
|
194 |
||
195 |
if prev : |
|
196 |
prev.next = img |
|
197 |
||
198 |
img.prev = prev |
|
199 |
prev = img |
|
200 |
||
201 |
series.sorted_images.append(img) |
|
202 |
finally : |
|
203 |
db.close() |
|
204 |
||
205 |
if index : |
|
206 |
img = series.sorted_images[index - 1] |
|
207 |
||
208 |
img.render() |
|
209 |
else : |
|
210 |
series.render() |
|
211 |
||
212 |
print "Content-Type: text/html" |
|
213 |
||
214 |
print rendered_templates[0] |
|
215 |