Mercurial > hg > MakeItSo
annotate makeitso/handlers.py @ 14:bf1ce840d0f0
make this a genshi view (should be: tempita view)
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 15 Nov 2010 08:05:55 -0800 |
parents | |
children | 6d1c703c5ffc |
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 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 from urlparse import urlparse |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 from webob import Response, exc |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 class HandlerMatchException(Exception): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 """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
|
11 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 class Handler(object): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 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
|
15 handler_path = [] # path elements to match |
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 @classmethod |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 def match(cls, app, request): |
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 # check the method |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 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
|
22 return None |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
23 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 # check the path |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
25 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
|
26 return None |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
27 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
28 try: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
29 return cls(app, request) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
30 except HandlerMatchException: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
31 return None |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
32 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
33 def __init__(self, app, request): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
34 self.app = app |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
35 self.request = request |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
36 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
|
37 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
38 def link(self, path=(), permanant=False): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
39 if isinstance(path, basestring): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
40 path = [ path ] |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
41 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
|
42 if permanant: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
43 application_url = [ self.request.application_url ] |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
44 else: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
45 application_url = [ self.application_path ] |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
46 path = application_url + path |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
47 return '/'.join(path) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
48 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
49 def redirect(self, location): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
50 raise exc.HTTPSeeOther(location=location) |
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 class GenshiHandler(Handler): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
53 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
54 def __init__(self, app, request): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
55 Handler.__init__(self, app, request) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
56 self.data = { 'request': request, |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
57 'link': self.link } |
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 __call__(self): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
60 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
|
61 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
62 def Get(self): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
63 # needs to have self.template set |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
64 template = self.app.loader.load(self.template) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
65 return Response(content_type='text/html', |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
66 body=template.generate(**self.data).render('html')) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
67 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
68 class Index(GenshiHandler): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
69 template = 'index.html' |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
70 methods=set(['GET', 'POST']) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
71 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
72 def __init__(self, app, request): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
73 GenshiHandler.__init__(self, app, request) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
74 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
75 def Get(self): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
76 self.data['name'] = self.request.remote_user or self.app.name |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
77 return GenshiHandler.Get(self) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
78 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
79 def Post(self): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
80 self.app.name = self.request.POST.get('name', self.app.name) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
81 self.redirect(self.link(self.handler_path)) |