annotate genshi_view/template/+package+/handlers.py @ 8:af07c17c7b2f

pass app to Index handler
author k0s <k0scist@gmail.com>
date Fri, 06 Nov 2009 16:53:32 -0500
parents 86df7b4dbc9c
children cb8ebcb8b4fd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
1 from urlparse import urlparse
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
2 from webob import Response, exc
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
3
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
4 class HandlerMatchException(Exception):
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
5 """the handler doesn't match the request"""
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
6
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
7 class Handler(object):
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
8
4
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
9 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
10 handler_path = [] # path elements to match
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
11
1
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
12 @classmethod
4
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
13 def match(cls, app, request):
1
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
14 try:
4
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
15 return cls(app, request)
1
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
16 except HandlerMatchException:
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
17 return None
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
18
4
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
19 def __init__(self, app, request):
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
20 if request.method not in self.methods:
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
21 raise HandlerMatchException
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
22 self.path = request.path.info.strip('/').split('/')
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
23 if self.path == ['']:
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
24 self.path = []
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
25 if path[:len(handler_path)] != self.handler_path:
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
26 raise HandlerMatchException
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
27 self.app = app
1
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
28 self.request = request
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
29 self.application_path = urlparse(request.application_url)[2]
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
30
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
31 def link(self, path=(), permanant=False):
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
32 if isinstance(path, basestring):
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
33 path = [ path ]
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
34 path = [ i.strip('/') for i in path ]
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
35 if permanant:
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
36 application_url = [ self.request.application_url ]
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
37 else:
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
38 application_url = [ self.application_path ]
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
39 path = application_url + path
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
40 return '/'.join(path)
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
41
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
42 def redirect(self, location):
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
43 raise exc.HTTPSeeOther(location=location)
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
44
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
45 class GenshiHandler(Handler):
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
46
8
af07c17c7b2f pass app to Index handler
k0s <k0scist@gmail.com>
parents: 5
diff changeset
47 def __init__(self, app, request):
1
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
48 Handler.__init__(self, request)
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
49 self.data = { 'request': request }
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
50
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
51 def __call__(self):
4
0be8714e4d6a use the app and add it to the handler
k0s <k0scist@gmail.com>
parents: 1
diff changeset
52 template = self.app.loader.load(self.template)
1
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
53 return Response(content_type='text/html',
5
86df7b4dbc9c missed a )
k0s <k0scist@gmail.com>
parents: 4
diff changeset
54 body=template.generate(**self.data).render())
1
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
55
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
56 class Index(GenshiHandler):
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
57 template = 'index.html'
d9519f40c177 moving to a dispatcher architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
58