Mercurial > hg > toolbox
comparison toolbox/handlers.py @ 8:201857e15b50
py3
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 24 Nov 2020 13:04:00 -0800 |
parents | b0942f44413f |
children | abf99a03a6c6 |
comparison
equal
deleted
inserted
replaced
7:394648c67478 | 8:201857e15b50 |
---|---|
5 | 5 |
6 import cgi | 6 import cgi |
7 import os | 7 import os |
8 from datetime import datetime | 8 from datetime import datetime |
9 from pkg_resources import resource_filename | 9 from pkg_resources import resource_filename |
10 from urllib import quote as _quote | 10 from urllib.parse import quote as _quote |
11 from urlparse import urlparse | 11 from urlparse import urlparse |
12 from util import strsplit | 12 from .util import strsplit |
13 from util import JSONEncoder | 13 from .util import JSONEncoder |
14 from webob import Response, exc | 14 from webob import Response, exc |
15 from tempita import HTMLTemplate | 15 from tempita import HTMLTemplate |
16 from time import time | 16 from time import time |
17 | 17 |
18 # this is necessary because WSGI stupidly follows the CGI convention wrt encoding slashes | 18 # this is necessary because WSGI stupidly follows the CGI convention wrt encoding slashes |
41 @classmethod | 41 @classmethod |
42 def match(cls, app, request): | 42 def match(cls, app, request): |
43 | 43 |
44 # check the method | 44 # check the method |
45 if request.method not in cls.methods: | 45 if request.method not in cls.methods: |
46 return None | 46 return |
47 | 47 |
48 # check the path | 48 # check the path |
49 if request.environ['path'] != cls.handler_path: | 49 if request.environ['path'] != cls.handler_path: |
50 return None | 50 return |
51 | 51 |
52 # check the constructor | 52 # check the constructor |
53 try: | 53 try: |
54 return cls(app, request) | 54 return cls(app, request) |
55 except HandlerMatchException: | 55 except HandlerMatchException: |
56 return None | 56 return |
57 | 57 |
58 def __init__(self, app, request): | 58 def __init__(self, app, request): |
59 self.app = app | 59 self.app = app |
60 self.request = request | 60 self.request = request |
61 self.check_json() # is this a JSON request? | 61 self.check_json() # is this a JSON request? |
62 | 62 |
135 | 135 |
136 js = ['js/jquery-1.6.min.js', | 136 js = ['js/jquery-1.6.min.js', |
137 'js/less-1.0.41.min.js', | 137 'js/less-1.0.41.min.js', |
138 'js/jquery.timeago.js', | 138 'js/jquery.timeago.js', |
139 'js/main.js'] | 139 'js/main.js'] |
140 | 140 |
141 def __init__(self, app, request): | 141 def __init__(self, app, request): |
142 Handler.__init__(self, app, request) | 142 Handler.__init__(self, app, request) |
143 | 143 |
144 # add application template_dir if specified | 144 # add application template_dir if specified |
145 if app.template_dir: | 145 if app.template_dir: |
146 self.template_dirs = self.template_dirs[:] + [app.template_dir] | 146 self.template_dirs = self.template_dirs[:] + [app.template_dir] |
147 | 147 |
148 self.data = { 'request': request, | 148 self.data = { 'request': request, |
149 'css': self.css, | 149 'css': self.css, |
150 'item_name': self.app.item_name, | 150 'item_name': self.app.item_name, |
151 'item_plural': self.app.item_plural, | 151 'item_plural': self.app.item_plural, |
152 'less': self.less, | 152 'less': self.less, |
160 def find_template(self, name): | 160 def find_template(self, name): |
161 """find a template of a given name""" | 161 """find a template of a given name""" |
162 # the application caches a dict of the templates if app.reload is False | 162 # the application caches a dict of the templates if app.reload is False |
163 if name in self.template_cache: | 163 if name in self.template_cache: |
164 return self.template_cache[name] | 164 return self.template_cache[name] |
165 | 165 |
166 for d in self.template_dirs: | 166 for d in self.template_dirs: |
167 path = os.path.join(d, name) | 167 path = os.path.join(d, name) |
168 if os.path.exists(path): | 168 if os.path.exists(path): |
169 template = HTMLTemplate.from_filename(path) | 169 template = HTMLTemplate.from_filename(path) |
170 if not self.app.reload: | 170 if not self.app.reload: |
192 js = TempitaHandler.js[:] | 192 js = TempitaHandler.js[:] |
193 js.extend(['js/jquery.tokeninput.js', | 193 js.extend(['js/jquery.tokeninput.js', |
194 'js/jquery.jeditable.js', | 194 'js/jquery.jeditable.js', |
195 'js/jquery.autolink.js', | 195 'js/jquery.autolink.js', |
196 'js/project.js']) | 196 'js/project.js']) |
197 | 197 |
198 less = TempitaHandler.less[:] | 198 less = TempitaHandler.less[:] |
199 less.extend(['css/project.less']) | 199 less.extend(['css/project.less']) |
200 | 200 |
201 css = TempitaHandler.css[:] | 201 css = TempitaHandler.css[:] |
202 css.extend(['css/token-input.css', | 202 css.extend(['css/token-input.css', |
230 return datetime.utcfromtimestamp(timestamp).strftime(format_string) | 230 return datetime.utcfromtimestamp(timestamp).strftime(format_string) |
231 | 231 |
232 | 232 |
233 class QueryView(ProjectsView): | 233 class QueryView(ProjectsView): |
234 """general index view to query projects""" | 234 """general index view to query projects""" |
235 | 235 |
236 template = 'index.html' | 236 template = 'index.html' |
237 methods = set(['GET']) | 237 methods = set(['GET']) |
238 | 238 |
239 def __init__(self, app, request): | 239 def __init__(self, app, request): |
240 ProjectsView.__init__(self, app, request) | 240 ProjectsView.__init__(self, app, request) |
260 else: | 260 else: |
261 # default | 261 # default |
262 sort_type = '-modified' | 262 sort_type = '-modified' |
263 self.data['sort_type'] = sort_type | 263 self.data['sort_type'] = sort_type |
264 if sort_type != 'search': | 264 if sort_type != 'search': |
265 # preserve search order results | 265 # preserve search order results |
266 self.sort(sort_type) | 266 self.sort(sort_type) |
267 | 267 |
268 self.data['fields'] = self.app.model.fields() | 268 self.data['fields'] = self.app.model.fields() |
269 self.data['title'] = self.app.site_name | 269 self.data['title'] = self.app.site_name |
270 | 270 |
271 | 271 |
272 class ProjectView(ProjectsView): | 272 class ProjectView(ProjectsView): |
434 def Post(self): | 434 def Post(self): |
435 field = self.data['field'] | 435 field = self.data['field'] |
436 for key in self.request.POST.iterkeys(): | 436 for key in self.request.POST.iterkeys(): |
437 value = self.request.POST[key] | 437 value = self.request.POST[key] |
438 self.app.model.rename_field_value(field, key, value) | 438 self.app.model.rename_field_value(field, key, value) |
439 | |
440 return self.redirect(field, anchor=value) | 439 return self.redirect(field, anchor=value) |
441 | 440 |
442 def get_json(self): | 441 def get_json(self): |
443 return self.data['values'] | 442 return self.data['values'] |
444 | 443 |
445 | 444 |
446 class CreateProjectView(TempitaHandler): | 445 class CreateProjectView(TempitaHandler): |
447 """view to create a new project""" | 446 """view to create a new project""" |
448 | 447 |
449 template = 'new.html' | 448 template = 'new.html' |
450 methods = set(['GET', 'POST']) | 449 methods = set(['GET', 'POST']) |
451 handler_path = ['new'] | 450 handler_path = ['new'] |
452 js = TempitaHandler.js[:] | 451 js = TempitaHandler.js[:] |
453 js.extend(['js/jquery.tokeninput.js', | 452 js.extend(['js/jquery.tokeninput.js', |
454 'js/queryString.js', | 453 'js/queryString.js', |
455 'js/new.js']) | 454 'js/new.js']) |
456 | 455 |
457 less = TempitaHandler.less[:] | 456 less = TempitaHandler.less[:] |
458 less.extend(['css/new.less']) | 457 less.extend(['css/new.less']) |
459 | 458 |
460 css = TempitaHandler.css[:] | 459 css = TempitaHandler.css[:] |
461 css.extend(['css/token-input.css', | 460 css.extend(['css/token-input.css', |
462 'css/token-input-facebook.css']) | 461 'css/token-input-facebook.css']) |
463 | 462 |
464 def __init__(self, app, request): | 463 def __init__(self, app, request): |
532 class DeleteProjectHandler(Handler): | 531 class DeleteProjectHandler(Handler): |
533 | 532 |
534 methods = set(['POST']) | 533 methods = set(['POST']) |
535 handler_path = ['delete'] | 534 handler_path = ['delete'] |
536 | 535 |
537 def Post(self): | 536 def Post(self): |
538 post_data = self.post_data() | 537 post_data = self.post_data() |
539 project = post_data.get('project') | 538 project = post_data.get('project') |
540 if project: | 539 if project: |
541 try: | 540 try: |
542 self.app.model.delete(project) | 541 self.app.model.delete(project) |
566 project = self.app.model.project(name) | 565 project = self.app.model.project(name) |
567 if not project: | 566 if not project: |
568 continue | 567 continue |
569 for field in fields: | 568 for field in fields: |
570 ommitted[field].update(project.get(field, [])) | 569 ommitted[field].update(project.get(field, [])) |
571 | 570 |
572 for project in self.app.model.get(): | 571 for project in self.app.model.get(): |
573 if project in omit: | 572 if project in omit: |
574 continue | 573 continue |
575 # TODO: cache this for speed somehow | 574 # TODO: cache this for speed somehow |
576 # possibly at the model level | 575 # possibly at the model level |