Mercurial > hg > MakeItSo
diff 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 |
line wrap: on
line diff
--- a/makeitso/handlers.py Wed Nov 17 10:42:31 2010 -0800 +++ b/makeitso/handlers.py Wed Nov 17 10:57:59 2010 -0800 @@ -3,7 +3,10 @@ these are instantiated for every request, then called """ +import os +from pkg_resources import resource_filename from urlparse import urlparse +from tempita import HTMLTemplate from webob import Response, exc class HandlerMatchException(Exception): @@ -49,7 +52,9 @@ def redirect(self, location): raise exc.HTTPSeeOther(location=location) -class GenshiHandler(Handler): +class TempitaHandler(Handler): + + template_dirs = [ resource_filename(__name__, 'templates') ] def __init__(self, app, request): Handler.__init__(self, app, request) @@ -59,23 +64,18 @@ def __call__(self): return getattr(self, self.request.method.title())() + def find_template(self, template): + for d in self.template_dirs: + path = os.path.join(d, template) + if os.path.exists(path): + return HTMLTemplate.from_filename(path) + def Get(self): # needs to have self.template set - template = self.app.loader.load(self.template) + template = self.find_template(self.template) return Response(content_type='text/html', - body=template.generate(**self.data).render('html')) - -class Index(GenshiHandler): - template = 'index.html' - methods=set(['GET', 'POST']) + body=template.substitute(**self.data)) - def __init__(self, app, request): - GenshiHandler.__init__(self, app, request) - - def Get(self): - self.data['name'] = self.request.remote_user or self.app.name - return GenshiHandler.Get(self) - - def Post(self): - self.app.name = self.request.POST.get('name', self.app.name) - self.redirect(self.link(self.handler_path)) +class Index(TempitaHandler): + template = 'index.html' + methods=set(['GET'])