annotate makeitso/handlers.py @ 236:f3a0fb722b45

version bump
author Jeff Hammel <k0scist@gmail.com>
date Tue, 15 Dec 2015 10:23:30 -0800
parents 6d1c703c5ffc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 """
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 request handlers:
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 these are instantiated for every request, then called
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 """
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
20
6d1c703c5ffc use tempita instead of genshi since we have it already (and dont need a really complex templating system)
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
6 import os
6d1c703c5ffc use tempita instead of genshi since we have it already (and dont need a really complex templating system)
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
7 from pkg_resources import resource_filename
14
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 from urlparse import urlparse
20
6d1c703c5ffc use tempita instead of genshi since we have it already (and dont need a really complex templating system)
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
9 from tempita import HTMLTemplate
14
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 from webob import Response, exc
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 class HandlerMatchException(Exception):
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 """the handler doesn't match the request"""
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 class Handler(object):
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 methods = set(['GET']) # methods to listen to
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 handler_path = [] # path elements to match
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 @classmethod
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 def match(cls, app, request):
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 # check the method
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 if request.method not in cls.methods:
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 return None
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 # check the path
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 if request.environ['path'][:len(cls.handler_path)] != cls.handler_path:
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 return None
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 try:
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 return cls(app, request)
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 except HandlerMatchException:
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 return None
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 def __init__(self, app, request):
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 self.app = app
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 self.request = request
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 self.application_path = urlparse(request.application_url)[2]
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 def link(self, path=(), permanant=False):
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 if isinstance(path, basestring):
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 path = [ path ]
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 path = [ i.strip('/') for i in path ]
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 if permanant:
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 application_url = [ self.request.application_url ]
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 else:
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 application_url = [ self.application_path ]
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 path = application_url + path
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 return '/'.join(path)
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 def redirect(self, location):
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53 raise exc.HTTPSeeOther(location=location)
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54
20
6d1c703c5ffc use tempita instead of genshi since we have it already (and dont need a really complex templating system)
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
55 class TempitaHandler(Handler):
6d1c703c5ffc use tempita instead of genshi since we have it already (and dont need a really complex templating system)
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
56
6d1c703c5ffc use tempita instead of genshi since we have it already (and dont need a really complex templating system)
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
57 template_dirs = [ resource_filename(__name__, 'templates') ]
14
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 def __init__(self, app, request):
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 Handler.__init__(self, app, request)
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 self.data = { 'request': request,
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62 'link': self.link }
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
63
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
64 def __call__(self):
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65 return getattr(self, self.request.method.title())()
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
66
20
6d1c703c5ffc use tempita instead of genshi since we have it already (and dont need a really complex templating system)
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
67 def find_template(self, template):
6d1c703c5ffc use tempita instead of genshi since we have it already (and dont need a really complex templating system)
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
68 for d in self.template_dirs:
6d1c703c5ffc use tempita instead of genshi since we have it already (and dont need a really complex templating system)
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
69 path = os.path.join(d, template)
6d1c703c5ffc use tempita instead of genshi since we have it already (and dont need a really complex templating system)
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
70 if os.path.exists(path):
6d1c703c5ffc use tempita instead of genshi since we have it already (and dont need a really complex templating system)
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
71 return HTMLTemplate.from_filename(path)
6d1c703c5ffc use tempita instead of genshi since we have it already (and dont need a really complex templating system)
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
72
14
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
73 def Get(self):
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
74 # needs to have self.template set
20
6d1c703c5ffc use tempita instead of genshi since we have it already (and dont need a really complex templating system)
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
75 template = self.find_template(self.template)
14
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
76 return Response(content_type='text/html',
20
6d1c703c5ffc use tempita instead of genshi since we have it already (and dont need a really complex templating system)
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
77 body=template.substitute(**self.data))
14
bf1ce840d0f0 make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
78
20
6d1c703c5ffc use tempita instead of genshi since we have it already (and dont need a really complex templating system)
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
79 class Index(TempitaHandler):
6d1c703c5ffc use tempita instead of genshi since we have it already (and dont need a really complex templating system)
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
80 template = 'index.html'
6d1c703c5ffc use tempita instead of genshi since we have it already (and dont need a really complex templating system)
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
81 methods=set(['GET'])