comparison genshi_view/template/+package+/handlers.py @ 11:8a7731b2126a

include a post method, a nav bar, and some jquery fun
author k0s <k0scist@gmail.com>
date Sat, 07 Nov 2009 22:49:46 -0500
parents cb8ebcb8b4fd
children d54f85043f77
comparison
equal deleted inserted replaced
10:ffdfb79b30bf 11:8a7731b2126a
1 """
2 request handlers:
3 these are instantiated for every request, then called
4 """
5
1 from urlparse import urlparse 6 from urlparse import urlparse
2 from webob import Response, exc 7 from webob import Response, exc
3 8
4 class HandlerMatchException(Exception): 9 class HandlerMatchException(Exception):
5 """the handler doesn't match the request""" 10 """the handler doesn't match the request"""
15 return cls(app, request) 20 return cls(app, request)
16 except HandlerMatchException: 21 except HandlerMatchException:
17 return None 22 return None
18 23
19 def __init__(self, app, request): 24 def __init__(self, app, request):
25
26 # check the method
20 if request.method not in self.methods: 27 if request.method not in self.methods:
21 raise HandlerMatchException 28 raise HandlerMatchException
22 self.path = request.path_info.strip('/').split('/') 29
23 if self.path == ['']: 30 # check the path
24 self.path = [] 31 if request.environ['path'][:len(self.handler_path)] != self.handler_path:
25 if self.path[:len(self.handler_path)] != self.handler_path:
26 raise HandlerMatchException 32 raise HandlerMatchException
33
27 self.app = app 34 self.app = app
28 self.request = request 35 self.request = request
29 self.application_path = urlparse(request.application_url)[2] 36 self.application_path = urlparse(request.application_url)[2]
30 37
31 def link(self, path=(), permanant=False): 38 def link(self, path=(), permanant=False):
44 51
45 class GenshiHandler(Handler): 52 class GenshiHandler(Handler):
46 53
47 def __init__(self, app, request): 54 def __init__(self, app, request):
48 Handler.__init__(self, app, request) 55 Handler.__init__(self, app, request)
49 self.data = { 'request': request } 56 self.data = { 'request': request,
57 'link': self.link }
50 58
51 def __call__(self): 59 def __call__(self):
60 return getattr(self, self.request.method.title())()
61
62 def Get(self):
63 # needs to have self.template set
52 template = self.app.loader.load(self.template) 64 template = self.app.loader.load(self.template)
53 return Response(content_type='text/html', 65 return Response(content_type='text/html',
54 body=template.generate(**self.data).render()) 66 body=template.generate(**self.data).render('html'))
55 67
56 class Index(GenshiHandler): 68 class Index(GenshiHandler):
57 template = 'index.html' 69 template = 'index.html'
70 methods=set(['GET', 'POST'])
58 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))