WebStack

examples/Common/Cookies/__init__.py

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