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,
|
|
33 <input type="text" value="name"/></form></body></html>"""
|
|
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):
|
|
44 name = 'foo' # XXX get from self.request.POST
|
|
45 name = escape(name)
|
|
46 self.redirect(self.link('/?name=%s' % name))
|
|
47
|