Mercurial > hg > index.fcgi > www > www-1
comparison pics2/pics_app.py @ 71:6318de36e334
pics2: limit access to index
author | paulo |
---|---|
date | Wed, 28 Oct 2015 00:57:23 -0700 |
parents | b7966ae653f2 |
children | ec48011dca1e |
comparison
equal
deleted
inserted
replaced
4:05b0ea037863 | 5:bbf826fadacc |
---|---|
8 | 8 |
9 import html | 9 import html |
10 | 10 |
11 import logging | 11 import logging |
12 logging.basicConfig( | 12 logging.basicConfig( |
13 level=logging.DEBUG, | 13 level=logging.INFO, |
14 filename="_LOG", | 14 filename="_LOG", |
15 format="%(asctime)s %(levelname)-8s %(message)s", | 15 format="%(asctime)s %(levelname)-8s %(message)s", |
16 ) | 16 ) |
17 | 17 |
18 | |
19 def _lahat(): | |
20 ret = None | |
21 | |
22 try: | |
23 with open("_lahat") as lahat_f: | |
24 ret = lahat_f.read().strip() | |
25 except IOError: | |
26 pass | |
27 | |
28 return ret | |
18 | 29 |
19 | 30 |
20 def _is_pics_dir(dirpath): | 31 def _is_pics_dir(dirpath): |
21 return os.path.exists(os.path.join(dirpath, "_picsroot")) | 32 return os.path.exists(os.path.join(dirpath, "_picsroot")) |
22 | 33 |
118 | 129 |
119 | 130 |
120 def __init__(self, environ): | 131 def __init__(self, environ): |
121 self._environ = environ | 132 self._environ = environ |
122 self._page_func = None | 133 self._page_func = None |
134 self._show_index = False | |
123 | 135 |
124 #logging.debug("environ = %s" % (sorted(self._environ.items(), key=lambda x: x[0]),)) | 136 #logging.debug("environ = %s" % (sorted(self._environ.items(), key=lambda x: x[0]),)) |
125 logging.debug("environ['PATH_INFO'] = %s" % self._environ["PATH_INFO"]) | 137 logging.debug("environ['PATH_INFO'] = %s" % self._environ["PATH_INFO"]) |
126 logging.debug("environ['SCRIPT_NAME'] = %s" % self._environ["SCRIPT_NAME"]) | 138 logging.debug("environ['SCRIPT_NAME'] = %s" % self._environ["SCRIPT_NAME"]) |
127 logging.debug("environ['QUERY_STRING'] = %s" % self._environ["QUERY_STRING"]) | 139 logging.debug("environ['QUERY_STRING'] = %s" % self._environ["QUERY_STRING"]) |
131 logging.debug("ppi = %s" % ppi) | 143 logging.debug("ppi = %s" % ppi) |
132 | 144 |
133 if len(ppi) < 1 or ppi[0] != '': | 145 if len(ppi) < 1 or ppi[0] != '': |
134 raise AssertionError("Parsed path length must start empty: " + pi) | 146 raise AssertionError("Parsed path length must start empty: " + pi) |
135 | 147 |
148 self._qs = urlparse.parse_qs(self._environ["QUERY_STRING"]) | |
149 logging.debug("self._qs = %s" % self._qs) | |
150 if self._qs.get("lahat", [''])[0] == _lahat(): | |
151 self._show_index = True | |
152 | |
136 if len(ppi) >= 2 and _is_pics_dir(ppi[1]): | 153 if len(ppi) >= 2 and _is_pics_dir(ppi[1]): |
137 if len(ppi) == 2: | 154 if len(ppi) == 2: |
138 self._page_func = self.page_thumbs | 155 self._page_func = self.page_thumbs |
139 elif len(ppi) >= 4 and ppi[2] == "browse" and os.path.exists(os.path.join(*ppi)): | 156 elif len(ppi) >= 4 and ppi[2] == "browse" and os.path.exists(os.path.join(*ppi)): |
140 self._page_func = self.page_browse | 157 self._page_func = self.page_browse |
141 elif len(ppi) == 1: | 158 elif len(ppi) == 1 and self._show_index: |
142 self._page_func = self.page_index | 159 self._page_func = self.page_index |
143 | 160 |
144 if self._page_func is None: | 161 if self._page_func is None: |
145 raise RuntimeError("Cannot find path: " + pi) | 162 raise RuntimeError("Cannot find path: " + pi) |
146 | 163 |
181 def page_thumbs(self): | 198 def page_thumbs(self): |
182 ppi = _parse_path_info(self._environ["PATH_INFO"]) | 199 ppi = _parse_path_info(self._environ["PATH_INFO"]) |
183 d = os.path.join(*ppi) | 200 d = os.path.join(*ppi) |
184 (html_root, html_header, html_body) = self._get_standard_html_doc(d) | 201 (html_root, html_header, html_body) = self._get_standard_html_doc(d) |
185 | 202 |
186 qs = urlparse.parse_qs(self._environ["QUERY_STRING"]) | |
187 from_img = None | 203 from_img = None |
188 if "from" in qs: | 204 if "from" in self._qs: |
189 from_img = qs["from"][0] | 205 from_img = self._qs["from"][0] |
190 | 206 |
191 html_p = html_body.p | 207 html_p = html_body.p |
192 for (t, b) in _get_images(d): | 208 for (t, b) in _get_images(d): |
193 if from_img is not None and b == from_img: | 209 if from_img is not None and b == from_img: |
194 self._go_thumbnail_links_to_browse_imgs_html_body(html_p, t, b, img_args={"klass":"sel2", "id":"selected"}) | 210 self._go_thumbnail_links_to_browse_imgs_html_body(html_p, t, b, img_args={"klass":"sel2", "id":"selected"}) |
195 else: | 211 else: |
196 self._go_thumbnail_links_to_browse_imgs_html_body(html_p, t, b) | 212 self._go_thumbnail_links_to_browse_imgs_html_body(html_p, t, b) |
197 | 213 |
198 html_body.a("(Other pictures)", href=self._get_app_url('')) | 214 # TODO: fix me |
215 #if self._show_index: | |
216 # html_body.a("(Other pictures)", href=self._get_app_url('')) | |
217 | |
199 return html_root | 218 return html_root |
200 | 219 |
201 | 220 |
202 def page_browse(self): | 221 def page_browse(self): |
203 ppi = _parse_path_info(self._environ["PATH_INFO"]) | 222 ppi = _parse_path_info(self._environ["PATH_INFO"]) |