annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
1 from cgi import escape
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
2 from urlparse import urlparse
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
3 from webob import Response, exc
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
4
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
5 class Handler(object):
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
6 def __init__(self, request):
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
7 self.request = request
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
8 self.application_path = urlparse(request.application_url)[2]
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
9
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
10 def link(self, path=(), permanant=False):
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
11 if isinstance(path, basestring):
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
12 path = [ path ]
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
13 path = [ i.strip('/') for i in path ]
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
14 if permanant:
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
15 application_url = [ self.request.application_url ]
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
16 else:
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
17 application_url = [ self.application_path ]
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
18 path = application_url + path
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
19 return '/'.join(path)
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
20
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
21 def redirect(self, location):
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
22 raise exc.HTTPSeeOther(location=location)
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
23
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
24 class Get(Handler):
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
25
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
26 @classmethod
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
27 def match(cls, request):
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
28 return request.method == 'GET'
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
29
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
30 def __call__(self):
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
31 name = self.request.GET.get('name', 'world')
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
32 retval = """<html><body><form method="post">Hello,
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
33 <input type="text" value="name"/></form></body></html>"""
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
34 return Response(content_type='text/html',
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
35 body=retval % name)
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
36
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
37 class Post(Handler):
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
38
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
39 @classmethod
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
40 def match(cls, request):
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
41 return request.method == 'POST'
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
42
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
43 def __call__(self):
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
44 name = 'foo' # XXX get from self.request.POST
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
45 name = escape(name)
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
46 self.redirect(self.link('/?name=%s' % name))
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents:
diff changeset
47