# HG changeset patch # User paulb # Date 1095527040 0 # Node ID 83bed518a778b4cf28131bc3a795f7cbb00ccd98 # Parent ec3bfc09c63619da8627ea245ded11fbf22a6b11 [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. diff -r ec3bfc09c636 -r 83bed518a778 examples/Common/Cookies/__init__.py --- a/examples/Common/Cookies/__init__.py Thu Sep 16 22:20:39 2004 +0000 +++ b/examples/Common/Cookies/__init__.py Sat Sep 18 17:04:00 2004 +0000 @@ -14,32 +14,50 @@ # Get the fields and choose an action. - fields = trans.get_fields_from_path() + fields_from_path = trans.get_fields_from_path() + path = trans.get_path_without_query() + + # If the "set method" form was used, set the new method. + + if fields_from_path.has_key("set"): + method = (fields_from_path.get("method") or ["GET"])[0] + message = "Form method set to %s." % method + cookie_name = cookie_value = None - cookie_name_list = fields.get("name") or ["test"] - cookie_value_list = fields.get("value") or ["test"] - cookie_path_list = fields.get("path") or ["/"] - cookie_expires_list = fields.get("expires") or ["60"] + # Otherwise, discover the incoming fields. - cookie_name = cookie_name_list[0] - cookie_value = cookie_value_list[0] - cookie_path = cookie_path_list[0] - cookie_expires = int(cookie_expires_list[0]) + else: + if fields_from_path.has_key("add") or fields_from_path.has_key("delete"): + fields = fields_from_path + method = (fields.get("method") or ["GET"])[0] + else: + fields = trans.get_fields_from_body() + method = (fields.get("method") or ["GET"])[0] - message = "No action taken - use add or delete to change the cookies." + cookie_name_list = fields.get("name") or ["test"] + cookie_value_list = fields.get("value") or ["test"] + cookie_path_list = fields.get("path") or ["/"] + cookie_expires_list = fields.get("expires") or ["60"] + + cookie_name = cookie_name_list[0] + cookie_value = cookie_value_list[0] + cookie_path = cookie_path_list[0] + cookie_expires = int(cookie_expires_list[0]) + + message = "No action taken - use add or delete to change the cookies." - if fields.has_key("add"): - trans.set_cookie_value( - cookie_name, - cookie_value, - cookie_path, - time.time() + cookie_expires - ) - message = "Cookie %s added!" % cookie_name + if fields.has_key("add"): + trans.set_cookie_value( + cookie_name, + cookie_value, + cookie_path, + time.time() + cookie_expires + ) + message = "Cookie %s added!" % cookie_name - elif fields.has_key("delete"): - trans.delete_cookie(cookie_name) - message = "Cookie %s deleted!" % cookie_name + elif fields.has_key("delete"): + trans.delete_cookie(cookie_name) + message = "Cookie %s deleted!" % cookie_name # Get some information. @@ -55,8 +73,14 @@ +

Method

+
+

Method:

+

+

Cookie

-
+ +

Name specified:

Value found:

@@ -66,6 +90,11 @@ """ % ( message, self._format_cookies(trans.get_cookies()), + self._is_selected(method == "GET"), + self._is_selected(method == "POST"), + method, + path, + method, cookie_name, cookie_value, )) @@ -82,4 +111,10 @@ for value in l ]) + def _is_selected(self, value): + if value: + return 'selected="selected"' + else: + return "" + # vim: tabstop=4 expandtab shiftwidth=4