Mercurial > hg > genshi_view
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 |
rev | line source |
---|---|
1 | 1 from urlparse import urlparse |
2 from webob import Response, exc | |
3 | |
4 class HandlerMatchException(Exception): | |
5 """the handler doesn't match the request""" | |
6 | |
7 class Handler(object): | |
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 | 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 | 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 | 16 except HandlerMatchException: |
17 return None | |
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 | 28 self.request = request |
29 self.application_path = urlparse(request.application_url)[2] | |
30 | |
31 def link(self, path=(), permanant=False): | |
32 if isinstance(path, basestring): | |
33 path = [ path ] | |
34 path = [ i.strip('/') for i in path ] | |
35 if permanant: | |
36 application_url = [ self.request.application_url ] | |
37 else: | |
38 application_url = [ self.application_path ] | |
39 path = application_url + path | |
40 return '/'.join(path) | |
41 | |
42 def redirect(self, location): | |
43 raise exc.HTTPSeeOther(location=location) | |
44 | |
45 class GenshiHandler(Handler): | |
46 | |
8 | 47 def __init__(self, app, request): |
1 | 48 Handler.__init__(self, request) |
49 self.data = { 'request': request } | |
50 | |
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 | 53 return Response(content_type='text/html', |
5 | 54 body=template.generate(**self.data).render()) |
1 | 55 |
56 class Index(GenshiHandler): | |
57 template = 'index.html' | |
58 |