view 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
line wrap: on
line source

"""
${repr(project)}: a view with webob
"""

from webob import Request, Response, exc

class View(object):

    ### class level variables
    defaults = {}

    def __init__(self, **kw):
        for key in self.defaults:
            setattr(self, key, kw.get(key, self.defaults[key]))
        self.response_functions = { 'GET': self.get,
                                    'POST': self.post,
                                    }

    ### methods dealing with HTTP
    def __call__(self, environ, start_response):
        request = Request(environ)
        res = self.make_response(request)
        return res(environ, start_response)
                                
    def make_response(self, request):
        return self.response_functions.get(request.method, self.error)(request)

    def get_response(self, text, content_type='text/html'):
        res = Response(content_type=content_type, body=text)
        return res

    def get(self, request):
        """
        return response to a GET requst
        """
        return self.get_response('Hello, world')

    def post(self, request):
        """
        return response to a POST request
        """
        return exc.HTTPOk("Ok")

    def error(self, request):
        """deal with non-supported methods"""
        return exc.HTTPMethodNotAllowed("Only %r operations are allowed" % self.response_functions.keys())