Mercurial > hg > webob_view
diff webob_view/template/+package+/handlers.py @ 1:eb2cb7ebc849
moving to a handler architecture
author | k0s <k0scist@gmail.com> |
---|---|
date | Thu, 29 Oct 2009 15:08:54 -0400 |
parents | |
children | 4dcb932065e4 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/webob_view/template/+package+/handlers.py Thu Oct 29 15:08:54 2009 -0400 @@ -0,0 +1,47 @@ +from cgi import escape +from urlparse import urlparse +from webob import Response, exc + +class Handler(object): + def __init__(self, request): + self.request = request + self.application_path = urlparse(request.application_url)[2] + + def link(self, path=(), permanant=False): + if isinstance(path, basestring): + path = [ path ] + path = [ i.strip('/') for i in path ] + if permanant: + application_url = [ self.request.application_url ] + else: + application_url = [ self.application_path ] + path = application_url + path + return '/'.join(path) + + def redirect(self, location): + raise exc.HTTPSeeOther(location=location) + +class Get(Handler): + + @classmethod + def match(cls, request): + return request.method == 'GET' + + def __call__(self): + name = self.request.GET.get('name', 'world') + retval = """<html><body><form method="post">Hello, +<input type="text" value="name"/></form></body></html>""" + return Response(content_type='text/html', + body=retval % name) + +class Post(Handler): + + @classmethod + def match(cls, request): + return request.method == 'POST' + + def __call__(self): + name = 'foo' # XXX get from self.request.POST + name = escape(name) + self.redirect(self.link('/?name=%s' % name)) +