comparison makeitso/handlers.py @ 20:6d1c703c5ffc

use tempita instead of genshi since we have it already (and dont need a really complex templating system)
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 17 Nov 2010 10:57:59 -0800
parents bf1ce840d0f0
children
comparison
equal deleted inserted replaced
19:c46ec69d6253 20:6d1c703c5ffc
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
7 from pkg_resources import resource_filename
6 from urlparse import urlparse 8 from urlparse import urlparse
9 from tempita import HTMLTemplate
7 from webob import Response, exc 10 from webob import Response, exc
8 11
9 class HandlerMatchException(Exception): 12 class HandlerMatchException(Exception):
10 """the handler doesn't match the request""" 13 """the handler doesn't match the request"""
11 14
47 return '/'.join(path) 50 return '/'.join(path)
48 51
49 def redirect(self, location): 52 def redirect(self, location):
50 raise exc.HTTPSeeOther(location=location) 53 raise exc.HTTPSeeOther(location=location)
51 54
52 class GenshiHandler(Handler): 55 class TempitaHandler(Handler):
56
57 template_dirs = [ resource_filename(__name__, 'templates') ]
53 58
54 def __init__(self, app, request): 59 def __init__(self, app, request):
55 Handler.__init__(self, app, request) 60 Handler.__init__(self, app, request)
56 self.data = { 'request': request, 61 self.data = { 'request': request,
57 'link': self.link } 62 'link': self.link }
58 63
59 def __call__(self): 64 def __call__(self):
60 return getattr(self, self.request.method.title())() 65 return getattr(self, self.request.method.title())()
61 66
67 def find_template(self, template):
68 for d in self.template_dirs:
69 path = os.path.join(d, template)
70 if os.path.exists(path):
71 return HTMLTemplate.from_filename(path)
72
62 def Get(self): 73 def Get(self):
63 # needs to have self.template set 74 # needs to have self.template set
64 template = self.app.loader.load(self.template) 75 template = self.find_template(self.template)
65 return Response(content_type='text/html', 76 return Response(content_type='text/html',
66 body=template.generate(**self.data).render('html')) 77 body=template.substitute(**self.data))
67 78
68 class Index(GenshiHandler): 79 class Index(TempitaHandler):
69 template = 'index.html' 80 template = 'index.html'
70 methods=set(['GET', 'POST']) 81 methods=set(['GET'])
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))