WebStack

Annotated examples/Common/Cookies/__init__.py

407:3d0fda3123e2
2005-07-18 paulb [project @ 2005-07-18 21:11:51 by paulb] Added additional documentation to make configuration of the login redirect resource more obvious.
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@374 86
      <p>
paulb@374 87
        <input name="add" type="submit" value="Add..."/>
paulb@374 88
        <input name="delete" type="submit" value="Delete..."/>
paulb@374 89
        <input name="refresh" type="submit" value="Refresh..."/>
paulb@374 90
      </p>
paulb@260 91
    </form>
paulb@99 92
  </body>
paulb@99 93
</html>
paulb@99 94
""" % (
paulb@99 95
    message,
paulb@118 96
    self._format_cookies(trans.get_cookies()),
paulb@262 97
    self._is_selected(method == "GET"),
paulb@262 98
    self._is_selected(method == "POST"),
paulb@262 99
    method,
paulb@262 100
    path,
paulb@262 101
    method,
paulb@106 102
    cookie_name,
paulb@260 103
    cookie_value,
paulb@99 104
))
paulb@99 105
paulb@118 106
    def _format_cookies(self, d):
paulb@99 107
        return "".join([
paulb@118 108
            "<dt>%s</dt><dd>%s</dd>" % (key, value.value)
paulb@99 109
            for key, value in d.items()
paulb@99 110
        ])
paulb@99 111
paulb@99 112
    def _format_list(self, l):
paulb@99 113
        return "".join([
paulb@99 114
            "<li>%s</li>" % value
paulb@99 115
            for value in l
paulb@99 116
        ])
paulb@99 117
paulb@262 118
    def _is_selected(self, value):
paulb@262 119
        if value:
paulb@262 120
            return 'selected="selected"'
paulb@262 121
        else:
paulb@262 122
            return ""
paulb@262 123
paulb@99 124
# vim: tabstop=4 expandtab shiftwidth=4