annotate genshi_view/template/+package+/handlers.py @ 21:807c8eef8098

add a handler for tempita; should rename the package (at some point) template_view
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 17 Nov 2010 11:02:33 -0800
parents d54f85043f77
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
1 """
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
2 request handlers:
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
3 these are instantiated for every request, then called
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
4 """
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
5
21
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
6 import os
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
7 from pkg_resources import resource_filename
1
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
8 from urlparse import urlparse
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
9 from webob import Response, exc
21
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
10 from tempita import HTMLTemplate
1
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
11
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
12 class HandlerMatchException(Exception):
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
13 """the handler doesn't match the request"""
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
14
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
15 class Handler(object):
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
16
4
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
17 methods = set(['GET']) # methods to listen to
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
18 handler_path = [] # path elements to match
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
19
1
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
20 @classmethod
4
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
21 def match(cls, app, request):
14
d54f85043f77 better and more configurable matching logic
k0s <k0scist@gmail.com>
parents: 11
diff changeset
22
d54f85043f77 better and more configurable matching logic
k0s <k0scist@gmail.com>
parents: 11
diff changeset
23 # check the method
d54f85043f77 better and more configurable matching logic
k0s <k0scist@gmail.com>
parents: 11
diff changeset
24 if request.method not in cls.methods:
d54f85043f77 better and more configurable matching logic
k0s <k0scist@gmail.com>
parents: 11
diff changeset
25 return None
d54f85043f77 better and more configurable matching logic
k0s <k0scist@gmail.com>
parents: 11
diff changeset
26
d54f85043f77 better and more configurable matching logic
k0s <k0scist@gmail.com>
parents: 11
diff changeset
27 # check the path
d54f85043f77 better and more configurable matching logic
k0s <k0scist@gmail.com>
parents: 11
diff changeset
28 if request.environ['path'][:len(cls.handler_path)] != cls.handler_path:
d54f85043f77 better and more configurable matching logic
k0s <k0scist@gmail.com>
parents: 11
diff changeset
29 return None
d54f85043f77 better and more configurable matching logic
k0s <k0scist@gmail.com>
parents: 11
diff changeset
30
1
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
31 try:
4
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
32 return cls(app, request)
1
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
33 except HandlerMatchException:
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
34 return None
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
35
4
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
36 def __init__(self, app, request):
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
37 self.app = app
1
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
38 self.request = request
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
39 self.application_path = urlparse(request.application_url)[2]
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
40
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
41 def link(self, path=(), permanant=False):
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
42 if isinstance(path, basestring):
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
43 path = [ path ]
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
44 path = [ i.strip('/') for i in path ]
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
45 if permanant:
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
46 application_url = [ self.request.application_url ]
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
47 else:
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
48 application_url = [ self.application_path ]
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
49 path = application_url + path
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
50 return '/'.join(path)
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
51
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
52 def redirect(self, location):
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
53 raise exc.HTTPSeeOther(location=location)
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
54
21
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
55 class TempitaHandler(Handler):
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
56
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
57 template_dirs = [ resource_filename(__name__, 'templates') ]
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
58
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
59 def __init__(self, app, request):
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
60 Handler.__init__(self, app, request)
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
61 self.data = { 'request': request,
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
62 'link': self.link }
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
63
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
64 def __call__(self):
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
65 return getattr(self, self.request.method.title())()
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
66
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
67 def find_template(self, template):
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
68 for d in self.template_dirs:
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
69 path = os.path.join(d, template)
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
70 if os.path.exists(path):
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
71 return HTMLTemplate.from_filename(path)
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
72
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
73 def Get(self):
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
74 # needs to have self.template set
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
75 template = self.find_template(self.template)
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
76 return Response(content_type='text/html',
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
77 body=template.substitute(**self.data))
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
78
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
79
1
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
80 class GenshiHandler(Handler):
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
81
8
af07c17c7b2f pass app to Index handler
k0s <k0scist@gmail.com>
parents: 5
diff changeset
82 def __init__(self, app, request):
9
cb8ebcb8b4fd more corrections
k0s <k0scist@gmail.com>
parents: 8
diff changeset
83 Handler.__init__(self, app, request)
11
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
84 self.data = { 'request': request,
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
85 'link': self.link }
1
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
86
11
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
87 def __call__(self):
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
88 return getattr(self, self.request.method.title())()
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
89
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
90 def Get(self):
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
91 # needs to have self.template set
4
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
92 template = self.app.loader.load(self.template)
1
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
93 return Response(content_type='text/html',
11
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
94 body=template.generate(**self.data).render('html'))
1
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
95
21
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
96
807c8eef8098 add a handler for tempita; should rename the package (at some point) template_view
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
97
1
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
98 class Index(GenshiHandler):
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
99 template = 'index.html'
11
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
100 methods=set(['GET', 'POST'])
1
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
101
11
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
102 def __init__(self, app, request):
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
103 GenshiHandler.__init__(self, app, request)
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
104
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
105 def Get(self):
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
106 self.data['name'] = self.request.remote_user or self.app.name
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
107 return GenshiHandler.Get(self)
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
108
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
109 def Post(self):
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
110 self.app.name = self.request.POST.get('name', self.app.name)
8a7731b2126a include a post method, a nav bar, and some jquery fun
k0s <k0scist@gmail.com>
parents: 9
diff changeset
111 self.redirect(self.link(self.handler_path))