view webob_view/template/+package+/handlers.py @ 4:ee9ed5875df0

add a name to the input
author Robert Marianski <rob@marianski.com>
date Mon, 02 Nov 2009 17:50:39 -0500
parents 4dcb932065e4
children
line wrap: on
line source

from cgi import escape
from urlparse import urlparse
from webob import Response, exc

class Handler(object):
    def __init__(self, request):
        self.request = request
        self.application_path = urlparse(request.application_url)[2]

    def link(self, path=(), permanant=False):
        if isinstance(path, basestring):
            path = [ path ]
        path = [ i.strip('/') for i in path ]
        if permanant:
            application_url = [ self.request.application_url ]
        else:
            application_url = [ self.application_path ]
        path = application_url + path
        return '/'.join(path)

    def redirect(self, location):
        raise exc.HTTPSeeOther(location=location)

class Get(Handler):

    @classmethod
    def match(cls, request):
        return request.method == 'GET'

    def __call__(self):
        name = self.request.GET.get('name', 'world')
        retval = """<html><body><form method="post">Hello,
<input type="text" name="name" value="%s"/></form></body></html>"""
        return Response(content_type='text/html',
                        body=retval % name)

class Post(Handler):

    @classmethod
    def match(cls, request):
        return request.method == 'POST'

    def __call__(self):
        name = self.request.POST.get('name', 'world')
        name = escape(name)
        self.redirect(self.link('/?name=%s' % name))