|
1 #!/usr/bin/env python2.4 |
|
2 # |
|
3 # DeGAL - A pretty simple web image gallery |
|
4 # Copyright (C) 2007 Tero Marttila |
|
5 # http://marttila.de/~terom/degal/ |
|
6 # |
|
7 # This program is free software; you can redistribute it and/or modify |
|
8 # it under the terms of the GNU General Public License as published by |
|
9 # the Free Software Foundation; either version 2 of the License, or |
|
10 # (at your option) any later version. |
|
11 # |
|
12 # This program is distributed in the hope that it will be useful, |
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15 # GNU General Public License for more details. |
|
16 # |
|
17 # You should have received a copy of the GNU General Public License |
|
18 # along with this program; if not, write to the |
|
19 # Free Software Foundation, Inc., |
|
20 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 # |
|
22 |
|
23 import os |
|
24 import cgi |
|
25 import Cookie |
|
26 |
|
27 import inc |
|
28 from lib import shorturl, template, utils, settings |
|
29 |
|
30 # |
|
31 # load request params |
|
32 # |
|
33 vars = cgi.FieldStorage() |
|
34 |
|
35 # these are interpeted different ways, hence the generic naming |
|
36 arg1 = vars["keys"].value |
|
37 if 'index' in vars : |
|
38 arg2 = vars["index"].value |
|
39 else : |
|
40 arg2 = None |
|
41 |
|
42 # the cookie with the user's current series |
|
43 cookie = Cookie.SimpleCookie(os.environ.get('HTTP_COOKIE', None)) |
|
44 |
|
45 # a special action? |
|
46 if arg1 and arg1 in ('add', 'del', 'clear', 'view') or arg2 == 'load' : |
|
47 # load the keys from the cookie |
|
48 if 'series' in cookie : |
|
49 keys = cookie["series"].value.split() |
|
50 else : |
|
51 keys = [] |
|
52 |
|
53 if arg2 == 'load' : |
|
54 # set the keys in the user's cookie to those in the URL |
|
55 keys = arg1.split() |
|
56 |
|
57 elif arg1 == 'add' and arg2 not in keys : |
|
58 # add a code to the list of keys |
|
59 keys.append(arg2) |
|
60 |
|
61 elif arg1 == 'del' and arg2 in keys : |
|
62 # remove a key from the list of keys |
|
63 keys.remove(arg2) |
|
64 |
|
65 elif arg1 == 'clear' : |
|
66 # clear out the set of keys |
|
67 keys = [] |
|
68 |
|
69 elif arg1 == 'view' : |
|
70 # just view them |
|
71 pass |
|
72 |
|
73 # set the series cookie value |
|
74 cookie['series'] = ' '.join(keys) |
|
75 cookie['series']['path'] = '/' |
|
76 |
|
77 # if we have keys, redirect to them, otherwise, back to index we go |
|
78 if keys : |
|
79 redirect_to = "../%s/" % ('+'.join(keys)) |
|
80 else : |
|
81 redirect_to = "../.." |
|
82 |
|
83 # do the redirect |
|
84 print "Status: 302" |
|
85 print "Location: %s" % redirect_to |
|
86 print cookie |
|
87 print |
|
88 print "Redirect..." |
|
89 else : |
|
90 # we're just viewing |
|
91 keys = arg1.split() |
|
92 |
|
93 # is this "My Series"? |
|
94 my_series = 'series' in cookie and cookie['series'].value.split() == keys |
|
95 |
|
96 index = fname = None |
|
97 |
|
98 if arg2 : |
|
99 try : |
|
100 index = int(arg2) |
|
101 except ValueError : |
|
102 fname = arg2 |
|
103 |
|
104 # our custom Series/Image classes, because they do act slightly differently |
|
105 |
|
106 class Series (object) : |
|
107 def __init__ (self, keys) : |
|
108 self.images = [] |
|
109 prev = None |
|
110 |
|
111 self.image_dict = dict() |
|
112 |
|
113 images = shorturl.get_images(keys) |
|
114 |
|
115 for index, (key, (dir, fname)) in enumerate(zip(keys, images)) : |
|
116 img = Image(self, key, dir, fname, index) |
|
117 self.images.append(img) |
|
118 self.image_dict[fname] = img |
|
119 |
|
120 img.prev = prev |
|
121 |
|
122 if prev : |
|
123 prev.next = img |
|
124 |
|
125 prev = img |
|
126 |
|
127 def render (self) : |
|
128 if my_series : |
|
129 descr = '<a href="../clear/" rel="nofollow">Clear your series</a>' |
|
130 else : |
|
131 descr = '<a href="load" rel="nofollow">Load as your series</a>' |
|
132 |
|
133 return template.gallery.render( |
|
134 stylesheet_url = utils.url("style.css", up=2), |
|
135 |
|
136 breadcrumb = [(utils.url(up=1), "Index"), (utils.url(), "Series")], |
|
137 |
|
138 dirs = None, |
|
139 title = "Series", |
|
140 |
|
141 num_pages = 1, |
|
142 cur_page = 0, |
|
143 |
|
144 images = self.images, |
|
145 |
|
146 description = descr, |
|
147 |
|
148 shorturl = None, |
|
149 shorturl_code = None, |
|
150 ) |
|
151 |
|
152 class Image (object) : |
|
153 def __init__ (self, series, key, dir, fname, index) : |
|
154 self.fname = fname |
|
155 self.name = utils.url_join(dir, fname, abs=True) |
|
156 self.html_name = utils.url(fname) |
|
157 self.real_html_name = utils.url_join(dir, fname + ".html", abs=True) |
|
158 |
|
159 self.thumb_name = utils.url_join(dir, settings.THUMB_DIR, fname, abs=True) |
|
160 self.preview_name = utils.url_join(dir, settings.PREVIEW_DIR, fname, abs=True) |
|
161 |
|
162 self.shorturl = key |
|
163 |
|
164 self.prev = self.next = None |
|
165 |
|
166 def render (self) : |
|
167 descr = '<span style="font-size: x-small"><a href="%s.html">Standalone image</a></span>' % self.real_html_name |
|
168 |
|
169 if my_series : |
|
170 series_url = utils.url_join("del", self.shorturl, up=1) |
|
171 series_verb = "Remove from" |
|
172 else : |
|
173 series_url = series_verb = "" |
|
174 |
|
175 return template.image.render( |
|
176 stylesheet_url = utils.url("style.css", up=3), |
|
177 |
|
178 breadcrumb = [(utils.url(up=2), "Index"), (utils.url("."), "Series"), (self.html_name, self.fname)], |
|
179 |
|
180 title = self.fname, |
|
181 |
|
182 prev = self.prev, |
|
183 img = self, |
|
184 next = self.next, |
|
185 |
|
186 description = descr, |
|
187 |
|
188 img_size = None, |
|
189 file_size = None, |
|
190 timestamp = None, |
|
191 |
|
192 shorturl = utils.url_join("s", self.shorturl, abs=True), |
|
193 shorturl_code = self.shorturl, |
|
194 |
|
195 series_url = series_url, |
|
196 series_verb = series_verb, |
|
197 ) |
|
198 |
|
199 series = Series(keys) |
|
200 |
|
201 if fname : |
|
202 html = series.image_dict[fname].render() |
|
203 elif index : |
|
204 html = series.images[index - 1].render() |
|
205 else : |
|
206 html = series.render() |
|
207 |
|
208 print "Content-Type: text/html" |
|
209 print |
|
210 print html |
|
211 |