Mercurial > hg > SimpleWiki
comparison simplewiki/handlers.py @ 1:4c83f7715993
[mq]: renderer
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 07 Sep 2010 18:11:04 -0700 |
parents | d5102c881cb5 |
children | 2464e2051b78 |
comparison
equal
deleted
inserted
replaced
0:d5102c881cb5 | 1:4c83f7715993 |
---|---|
1 """ | 1 """ |
2 request handlers: | 2 request handlers: |
3 these are instantiated for every request, then called | 3 these are instantiated for every request, then called |
4 """ | 4 """ |
5 | 5 |
6 import os | |
6 from urlparse import urlparse | 7 from urlparse import urlparse |
7 from webob import Response, exc | 8 from webob import Response, exc |
8 | 9 |
9 class HandlerMatchException(Exception): | 10 class HandlerMatchException(Exception): |
10 """the handler doesn't match the request""" | 11 """the handler doesn't match the request""" |
47 return '/'.join(path) | 48 return '/'.join(path) |
48 | 49 |
49 def redirect(self, location): | 50 def redirect(self, location): |
50 raise exc.HTTPSeeOther(location=location) | 51 raise exc.HTTPSeeOther(location=location) |
51 | 52 |
52 class GenshiHandler(Handler): | 53 class GenshiRenderer(Handler): |
54 | |
55 @classmethod | |
56 def match(cls, app, request): | |
57 | |
58 # check the method | |
59 if request.method not in cls.methods: | |
60 return None | |
61 | |
62 # check the path | |
63 path = request.environ['path'] | |
64 if not path: | |
65 return None | |
66 if not path[-1].endswith('.html'): | |
67 return None | |
68 | |
69 | |
70 try: | |
71 return cls(app, request) | |
72 except HandlerMatchException: | |
73 return None | |
53 | 74 |
54 def __init__(self, app, request): | 75 def __init__(self, app, request): |
55 Handler.__init__(self, app, request) | 76 Handler.__init__(self, app, request) |
77 self.template = os.path.join(app.directory, *request.environ['path']) | |
78 if not os.path.exists(self.template): | |
79 raise HandlerMatchException | |
56 self.data = { 'request': request, | 80 self.data = { 'request': request, |
57 'link': self.link } | 81 'link': self.link } |
58 | 82 |
59 def __call__(self): | 83 def __call__(self): |
60 return getattr(self, self.request.method.title())() | 84 return getattr(self, self.request.method.title())() |
63 # needs to have self.template set | 87 # needs to have self.template set |
64 template = self.app.loader.load(self.template) | 88 template = self.app.loader.load(self.template) |
65 return Response(content_type='text/html', | 89 return Response(content_type='text/html', |
66 body=template.generate(**self.data).render('html')) | 90 body=template.generate(**self.data).render('html')) |
67 | 91 |
68 class Index(GenshiHandler): | |
69 template = 'index.html' | |
70 methods=set(['GET', 'POST']) | |
71 | |
72 def __init__(self, app, request): | |
73 GenshiHandler.__init__(self, app, request) | |
74 | |
75 def Get(self): | |
76 self.data['name'] = self.request.remote_user or self.app.name | |
77 return GenshiHandler.Get(self) | |
78 | |
79 def Post(self): | |
80 self.app.name = self.request.POST.get('name', self.app.name) | |
81 self.redirect(self.link(self.handler_path)) |