Mercurial > hg > webob_view
annotate webob_view/template/+package+/handlers.py @ 3:4dcb932065e4
finish dispatching version of webob_view
author | k0s <k0scist@gmail.com> |
---|---|
date | Thu, 29 Oct 2009 15:28:52 -0400 |
parents | eb2cb7ebc849 |
children | ee9ed5875df0 |
rev | line source |
---|---|
1 | 1 from cgi import escape |
2 from urlparse import urlparse | |
3 from webob import Response, exc | |
4 | |
5 class Handler(object): | |
6 def __init__(self, request): | |
7 self.request = request | |
8 self.application_path = urlparse(request.application_url)[2] | |
9 | |
10 def link(self, path=(), permanant=False): | |
11 if isinstance(path, basestring): | |
12 path = [ path ] | |
13 path = [ i.strip('/') for i in path ] | |
14 if permanant: | |
15 application_url = [ self.request.application_url ] | |
16 else: | |
17 application_url = [ self.application_path ] | |
18 path = application_url + path | |
19 return '/'.join(path) | |
20 | |
21 def redirect(self, location): | |
22 raise exc.HTTPSeeOther(location=location) | |
23 | |
24 class Get(Handler): | |
25 | |
26 @classmethod | |
27 def match(cls, request): | |
28 return request.method == 'GET' | |
29 | |
30 def __call__(self): | |
31 name = self.request.GET.get('name', 'world') | |
32 retval = """<html><body><form method="post">Hello, | |
3
4dcb932065e4
finish dispatching version of webob_view
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
33 <input type="text" value="%s"/></form></body></html>""" |
1 | 34 return Response(content_type='text/html', |
35 body=retval % name) | |
36 | |
37 class Post(Handler): | |
38 | |
39 @classmethod | |
40 def match(cls, request): | |
41 return request.method == 'POST' | |
42 | |
43 def __call__(self): | |
3
4dcb932065e4
finish dispatching version of webob_view
k0s <k0scist@gmail.com>
parents:
1
diff
changeset
|
44 name = self.request.POST.get('name', 'world') |
1 | 45 name = escape(name) |
46 self.redirect(self.link('/?name=%s' % name)) | |
47 |