source: TI07-MOLES/trunk/PythonCode/wsgi/cookieEG.py @ 1903

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/PythonCode/wsgi/cookieEG.py@2522
Revision 1903, 1.6 KB checked in by lawrence, 13 years ago (diff)

This is the shell of a new (and complete) rewrite of discovery
and browse as wsgi applications. (Discovery is already partially
rewritten in this manner, but handling browse will be more
difficult).

Line 
1#!/usr/bin/env python
2"""
3This trivial WSGI app saves your name as a cookie.
4
5@author: Stephen Pascoe
6@requires: paste
7"""
8
9from paste.request import get_cookie_dict
10from paste.wsgiwrappers import WSGIResponse, WSGIRequest
11
12def cookieFactory(global_config,**local_conf):
13    return cookieEG()
14
15class cookieEG(object):
16    """
17    We have 3 cases for which this request might be invoked:
18    1. GET for the first time (no cookie set)
19    2. POST in response to filling in the form
20    3. GET after the cookie is set
21    """
22    def __init__(self):
23        pass
24   
25    def __call__(self, environ, start_response):
26
27        self.request = WSGIRequest(environ)
28
29        if self.request.POST:
30            self.name = self.request.POST['hello_name']
31        else:
32            try:
33                self.name = self.request.cookies['hello_name']
34            except KeyError:
35                self.name = None
36
37        if self.name:
38            iter = self.__say()
39        else:
40            iter = self.__ask()
41
42        response = WSGIResponse(mimetype='text/html')
43        if self.name: response.set_cookie('hello_name', self.name)
44
45        response(self.request.environ, start_response)
46
47        return iter
48
49    def __say(self):
50        yield """
51        <html>
52          <body>
53            <h1>Hello %s</h1>
54          </body>
55        </html>
56        """ % self.name
57
58    def __ask(self):
59        yield """
60        <html>
61        <body>
62        <h1>Hello</h1>
63        <form method='post' action=''>
64        <p>
65          Who are you? <input type='text' name='hello_name'/>
66          <input type='submit'/>
67        </p>
68        </form>
69        </body>
70        </html>
71        """
72
Note: See TracBrowser for help on using the repository browser.