Mercurial > hg > webob_view
annotate webob_view/template/+package+/+package+.py_tmpl @ 0:72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
author | k0s <k0scist@gmail.com> |
---|---|
date | Tue, 27 Oct 2009 15:04:33 -0400 |
parents | |
children | eb2cb7ebc849 |
rev | line source |
---|---|
0
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
1 """ |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
2 ${repr(project)}: a view with webob |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
3 """ |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
4 |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
5 from webob import Request, Response, exc |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
6 |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
7 class View(object): |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
8 |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
9 ### class level variables |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
10 defaults = {} |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
11 |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
12 def __init__(self, **kw): |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
13 for key in self.defaults: |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
14 setattr(self, key, kw.get(key, self.defaults[key])) |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
15 self.response_functions = { 'GET': self.get, |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
16 'POST': self.post, |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
17 } |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
18 |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
19 ### methods dealing with HTTP |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
20 def __call__(self, environ, start_response): |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
21 request = Request(environ) |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
22 res = self.make_response(request) |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
23 return res(environ, start_response) |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
24 |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
25 def make_response(self, request): |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
26 return self.response_functions.get(request.method, self.error)(request) |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
27 |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
28 def get_response(self, text, content_type='text/html'): |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
29 res = Response(content_type=content_type, body=text) |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
30 return res |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
31 |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
32 def get(self, request): |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
33 """ |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
34 return response to a GET requst |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
35 """ |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
36 return self.get_response('Hello, world') |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
37 |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
38 def post(self, request): |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
39 """ |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
40 return response to a POST request |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
41 """ |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
42 return exc.HTTPOk("Ok") |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
43 |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
44 def error(self, request): |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
45 """deal with non-supported methods""" |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
46 return exc.HTTPMethodNotAllowed("Only %r operations are allowed" % self.response_functions.keys()) |
72cb6dc31e3f
initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
47 |