WebStack

Annotated examples/Common/Cookies/__init__.py

262:83bed518a778
2004-09-18 paulb [project @ 2004-09-18 17:04:00 by paulb] Enhanced the cookies example to use values originating from both GET and POST requests. This should test the usage of Unicode values with cookies still further.
paulb@99 1
#!/usr/bin/env python
paulb@99 2
paulb@99 3
"A test of cookies."
paulb@99 4
paulb@99 5
import WebStack.Generic
paulb@99 6
import time
paulb@99 7
paulb@99 8
class CookiesResource:
paulb@99 9
paulb@99 10
    "A resource adding and removing cookies."
paulb@99 11
paulb@99 12
    def respond(self, trans):
paulb@99 13
        trans.set_content_type(WebStack.Generic.ContentType("text/html"))
paulb@99 14
paulb@99 15
        # Get the fields and choose an action.
paulb@99 16
paulb@262 17
        fields_from_path = trans.get_fields_from_path()
paulb@262 18
        path = trans.get_path_without_query()
paulb@262 19
paulb@262 20
        # If the "set method" form was used, set the new method.
paulb@262 21
paulb@262 22
        if fields_from_path.has_key("set"):
paulb@262 23
            method = (fields_from_path.get("method") or ["GET"])[0]
paulb@262 24
            message = "Form method set to %s." % method
paulb@262 25
            cookie_name = cookie_value = None
paulb@99 26
paulb@262 27
        # Otherwise, discover the incoming fields.
paulb@99 28
paulb@262 29
        else:
paulb@262 30
            if fields_from_path.has_key("add") or fields_from_path.has_key("delete"):
paulb@262 31
                fields = fields_from_path
paulb@262 32
                method = (fields.get("method") or ["GET"])[0]
paulb@262 33
            else:
paulb@262 34
                fields = trans.get_fields_from_body()
paulb@262 35
                method = (fields.get("method") or ["GET"])[0]
paulb@99 36
paulb@262 37
            cookie_name_list = fields.get("name") or ["test"]
paulb@262 38
            cookie_value_list = fields.get("value") or ["test"]
paulb@262 39
            cookie_path_list = fields.get("path") or ["/"]
paulb@262 40
            cookie_expires_list = fields.get("expires") or ["60"]
paulb@262 41
paulb@262 42
            cookie_name = cookie_name_list[0]
paulb@262 43
            cookie_value = cookie_value_list[0]
paulb@262 44
            cookie_path = cookie_path_list[0]
paulb@262 45
            cookie_expires = int(cookie_expires_list[0])
paulb@262 46
paulb@262 47
            message = "No action taken - use add or delete to change the cookies."
paulb@99 48
paulb@262 49
            if fields.has_key("add"):
paulb@262 50
                trans.set_cookie_value(
paulb@262 51
                    cookie_name,
paulb@262 52
                    cookie_value,
paulb@262 53
                    cookie_path,
paulb@262 54
                    time.time() + cookie_expires
paulb@262 55
                )
paulb@262 56
                message = "Cookie %s added!" % cookie_name
paulb@99 57
paulb@262 58
            elif fields.has_key("delete"):
paulb@262 59
                trans.delete_cookie(cookie_name)
paulb@262 60
                message = "Cookie %s deleted!" % cookie_name
paulb@99 61
paulb@99 62
        # Get some information.
paulb@99 63
paulb@99 64
        out = trans.get_response_stream()
paulb@99 65
        out.write("""
paulb@99 66
<html>
paulb@99 67
  <head>
paulb@99 68
    <title>Cookies Example</title>
paulb@99 69
  </head>
paulb@99 70
  <body>
paulb@99 71
    <h1>Cookies</h1>
paulb@99 72
    <p>%s</p>
paulb@99 73
    <ul>
paulb@99 74
      %s
paulb@99 75
    </ul>
paulb@262 76
    <h2>Method</h2>
paulb@262 77
    <form method="GET">
paulb@262 78
      <p>Method: <select name="method"><option value="GET" %s>GET</option><option value="POST" %s>POST</option></select></p>
paulb@262 79
      <p><input name="set" type="submit" value="Set method..."/></p>
paulb@262 80
    </form>
paulb@106 81
    <h2>Cookie</h2>
paulb@262 82
    <form method="%s" action="%s">
paulb@262 83
      <input name="method" type="hidden" value="%s"/>
paulb@260 84
      <p>Name specified: <input name="name" value="%s"/></p>
paulb@260 85
      <p>Value found: <input name="value" value="%s"/></p>
paulb@260 86
      <p><input name="add" type="submit" value="Add..."/><input name="delete" type="submit" value="Delete..."/></p>
paulb@260 87
    </form>
paulb@99 88
  </body>
paulb@99 89
</html>
paulb@99 90
""" % (
paulb@99 91
    message,
paulb@118 92
    self._format_cookies(trans.get_cookies()),
paulb@262 93
    self._is_selected(method == "GET"),
paulb@262 94
    self._is_selected(method == "POST"),
paulb@262 95
    method,
paulb@262 96
    path,
paulb@262 97
    method,
paulb@106 98
    cookie_name,
paulb@260 99
    cookie_value,
paulb@99 100
))
paulb@99 101
paulb@118 102
    def _format_cookies(self, d):
paulb@99 103
        return "".join([
paulb@118 104
            "<dt>%s</dt><dd>%s</dd>" % (key, value.value)
paulb@99 105
            for key, value in d.items()
paulb@99 106
        ])
paulb@99 107
paulb@99 108
    def _format_list(self, l):
paulb@99 109
        return "".join([
paulb@99 110
            "<li>%s</li>" % value
paulb@99 111
            for value in l
paulb@99 112
        ])
paulb@99 113
paulb@262 114
    def _is_selected(self, value):
paulb@262 115
        if value:
paulb@262 116
            return 'selected="selected"'
paulb@262 117
        else:
paulb@262 118
            return ""
paulb@262 119
paulb@99 120
# vim: tabstop=4 expandtab shiftwidth=4