Mercurial > hg > MakeItSo
changeset 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 | c46ec69d6253 |
children | c879b93c3f15 |
files | makeitso/dispatcher.py makeitso/handlers.py |
diffstat | 2 files changed, 18 insertions(+), 34 deletions(-) [+] |
line wrap: on
line diff
--- a/makeitso/dispatcher.py Wed Nov 17 10:42:31 2010 -0800 +++ b/makeitso/dispatcher.py Wed Nov 17 10:57:59 2010 -0800 @@ -3,29 +3,19 @@ data persisting across requests should go here """ -import os - from handlers import Index - -from genshi.template import TemplateLoader -from paste.fileapp import FileApp -from pkg_resources import resource_filename from webob import Request, Response, exc class Dispatcher(object): ### class level variables - defaults = { 'auto_reload': 'False', - 'template_dirs': '', - 'app': None, - 'name': 'anonymous' } + defaults = { 'app': None, } def __init__(self, **kw): # set instance parameters from kw and defaults for key in self.defaults: setattr(self, key, kw.get(key, self.defaults[key])) - self.auto_reload = self.auto_reload.lower() == 'true' # request handlers self.handlers = [ Index ] @@ -34,12 +24,6 @@ if self.app: assert hasattr(self.app, '__call__') - # template loader - self.template_dirs = self.template_dirs.split() - self.template_dirs.append(resource_filename(__name__, 'templates')) - self.loader = TemplateLoader(self.template_dirs, - auto_reload=self.auto_reload) - def __call__(self, environ, start_response): # get a request object
--- 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'])