author | terom |
Thu, 20 Dec 2007 17:42:04 +0000 | |
changeset 11 | 27dac27d1a58 |
parent 4 | d46ab092d2b2 |
child 12 | c2d8e9a754a1 |
permissions | -rwxr-xr-x |
1 | 1 |
#!/usr/bin/env python2.4 |
4
d46ab092d2b2
add rel="nofollow" to ShortURL and series-related links, googlebot doesn't need to follow them
terom
parents:
3
diff
changeset
|
2 |
|
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
3 |
import shelve |
1 | 4 |
import cgi |
5 |
import Cookie |
|
6 |
import os, os.path |
|
7 |
||
8 |
import degal |
|
9 |
||
10 |
# |
|
11 |
# load request params |
|
12 |
# |
|
13 |
vars = cgi.FieldStorage() |
|
14 |
||
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
15 |
# 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
|
16 |
arg1 = vars["keys"].value |
1 | 17 |
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
|
18 |
arg2 = vars["index"].value |
1 | 19 |
else : |
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
20 |
arg2 = None |
1 | 21 |
|
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
22 |
# 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
|
23 |
cookie = Cookie.SimpleCookie(os.environ.get('HTTP_COOKIE', None)) |
1 | 24 |
|
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
25 |
# 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
|
26 |
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
|
27 |
# load the keys from the cookie |
1 | 28 |
if 'series' in cookie : |
29 |
keys = cookie["series"].value.split() |
|
30 |
else : |
|
31 |
keys = [] |
|
32 |
||
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
33 |
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
|
34 |
# 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
|
35 |
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
|
36 |
|
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
37 |
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
|
38 |
# 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
|
39 |
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
|
40 |
|
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
41 |
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
|
42 |
# 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
|
43 |
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
|
44 |
|
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
45 |
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
|
46 |
# clear out the set of keys |
1 | 47 |
keys = [] |
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
48 |
|
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
49 |
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
|
50 |
# just view them |
1 | 51 |
pass |
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
52 |
|
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
53 |
# set the series cookie value |
1 | 54 |
cookie['series'] = ' '.join(keys) |
55 |
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
|
56 |
|
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
57 |
# if we have keys, redirect to them, otherwise, back to index we go |
1 | 58 |
if keys : |
59 |
redirect_to = "../%s/" % ('+'.join(keys)) |
|
60 |
else : |
|
61 |
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
|
62 |
|
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
63 |
# do the redirect |
1 | 64 |
print "Status: 302" |
65 |
print "Location: %s" % redirect_to |
|
66 |
print cookie |
|
67 |
||
68 |
print "Redirect..." |
|
69 |
else : |
|
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
70 |
# 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
|
71 |
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
|
72 |
|
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
73 |
# 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
|
74 |
my_series = 'series' in cookie and cookie['series'].value.split() == keys |
1 | 75 |
|
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
76 |
if arg2 : |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
77 |
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
|
78 |
else : |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
79 |
index = None |
1 | 80 |
|
81 |
# 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
|
82 |
db = shelve.open('shorturls2', 'r') |
1 | 83 |
|
84 |
# get the Image objects |
|
85 |
photos = [] |
|
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
86 |
|
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
87 |
# |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
88 |
# 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
|
89 |
# |
1 | 90 |
|
91 |
# monkey-patch the templates |
|
92 |
rendered_templates = [] |
|
93 |
||
94 |
def _myRenderTo (self, path, **vars) : |
|
95 |
rendered_templates.append(self.render(**vars)) |
|
96 |
||
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
97 |
# 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
|
98 |
|
1 | 99 |
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
|
100 |
# |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
101 |
# vvvvvvvvvvvvvvvvvvvv |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
102 |
# vvvvvvvvvvvvvvvvvvvv |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
103 |
# |
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
104 |
# >>>> 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
|
105 |
# |
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 |
# ^^^^^^^^^^^^^^^^^^^^ |
1 | 108 |
|
109 |
# our own version of Folder |
|
110 |
class Series (degal.Folder) : |
|
111 |
def __init__ (self) : |
|
112 |
super(Series, self).__init__() |
|
113 |
||
114 |
self.alive = True |
|
115 |
self.filtered = False |
|
116 |
self.subdirs = {} |
|
117 |
self.images = {} |
|
118 |
self.sorted_images = [] |
|
119 |
self.title = "Series" |
|
120 |
||
121 |
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
|
122 |
self.descr = '<a href="../clear/" rel="nofollow">Clear your series</a>' |
1 | 123 |
else : |
4
d46ab092d2b2
add rel="nofollow" to ShortURL and series-related links, googlebot doesn't need to follow them
terom
parents:
3
diff
changeset
|
124 |
self.descr = '<a href="load" rel="nofollow">Load as your series</a>' |
1 | 125 |
|
126 |
self.shorturl_code = '' |
|
127 |
||
128 |
def breadcrumb (self) : |
|
129 |
return [('../..', 'Gallery'), ('.', 'Series')] |
|
130 |
||
131 |
def inRoot (self, *fnames) : |
|
132 |
return os.path.join('../..', *fnames) |
|
133 |
||
134 |
class Image (degal.Image) : |
|
135 |
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
|
136 |
type, self.dir_name, self.image_name = db[key] |
1 | 137 |
|
138 |
super(Image, self).__init__(series, self.image_name) |
|
139 |
||
3
9637b8f24005
...and fix it to redirect to the .html. Series.cgi is also refactored+commented+shorturls2'd
terom
parents:
1
diff
changeset
|
140 |
self.path = self._path() |
1 | 141 |
self.shorturl_code = key |
142 |
self.html_path = self.html_name = str(i + 1) |
|
143 |
self.title = 'Image %d' % (i + 1, ) |
|
144 |
self.descr = '<span style="font-size: x-small">Standalone image: <a href="%s.html">%s</a></span>' % (self._path(), self._path().lstrip('./')) |
|
145 |
self.img_size = (-1, -1) |
|
146 |
self.filesize = 0 |
|
147 |
self.timestamp = 0 |
|
148 |
||
149 |
if my_series : |
|
150 |
self.series_act = "del" |
|
151 |
self.series_verb = "Remove from" |
|
152 |
else : |
|
153 |
self.series_act = self.series_verb = "" |
|
154 |
||
155 |
def breadcrumb (self) : |
|
156 |
return [('../..', 'Gallery'), ('.', 'Series'), (self.html_name, self.title)] |
|
157 |
||
158 |
def _path (self, blaa='') : |
|
159 |
return os.path.join('../..', self.dir_name, blaa, self.image_name) |
|
160 |
||
161 |
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
|
162 |
return degal.link(self.html_name, "<img src='%s' alt='%s' title='%s'>" % (self._path(degal.THUMB_DIR), '', self.title)) |
1 | 163 |
|
164 |
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
|
165 |
return degal.link(self._path(), "<img src='%s' alt='%s' title='%s'>" % (self._path(degal.PREVIEW_DIR), '', self.title)) |
1 | 166 |
|
167 |
series = Series() |
|
168 |
||
169 |
try : |
|
170 |
prev = None |
|
171 |
||
172 |
for i, key in enumerate(keys) : |
|
173 |
img = Image(series, key, i) |
|
174 |
||
175 |
if prev : |
|
176 |
prev.next = img |
|
177 |
||
178 |
img.prev = prev |
|
179 |
prev = img |
|
180 |
||
181 |
series.sorted_images.append(img) |
|
182 |
finally : |
|
183 |
db.close() |
|
184 |
||
185 |
if index : |
|
186 |
img = series.sorted_images[index - 1] |
|
187 |
||
188 |
img.render() |
|
189 |
else : |
|
190 |
series.render() |
|
191 |
||
192 |
print "Content-Type: text/html" |
|
193 |
||
194 |
print rendered_templates[0] |
|
195 |