paulb@184 | 1 | #!/usr/bin/env python |
paulb@184 | 2 | |
paulb@184 | 3 | "A test of sessions." |
paulb@184 | 4 | |
paulb@184 | 5 | import WebStack.Generic |
paulb@184 | 6 | |
paulb@184 | 7 | class SessionsResource: |
paulb@184 | 8 | |
paulb@184 | 9 | "A resource adding and expiring sessions." |
paulb@184 | 10 | |
paulb@184 | 11 | def respond(self, trans): |
paulb@184 | 12 | trans.set_content_type(WebStack.Generic.ContentType("text/html")) |
paulb@184 | 13 | |
paulb@184 | 14 | # Get the fields and choose an action. |
paulb@184 | 15 | |
paulb@264 | 16 | fields = trans.get_fields_from_body() |
paulb@184 | 17 | |
paulb@184 | 18 | # Get the session, creating one if appropriate. |
paulb@184 | 19 | |
paulb@184 | 20 | create = fields.has_key("create") |
paulb@184 | 21 | session = trans.get_session(create) |
paulb@184 | 22 | message = "No action taken - use expire, add and delete to edit sessions." |
paulb@184 | 23 | |
paulb@184 | 24 | # If a session exists, perform editing operations. |
paulb@184 | 25 | |
paulb@241 | 26 | if session is not None: |
paulb@184 | 27 | names = fields.get("name") or ["test"] |
paulb@184 | 28 | values = fields.get("value") or ["test"] |
paulb@184 | 29 | name = names[0] |
paulb@184 | 30 | value = values[0] |
paulb@184 | 31 | |
paulb@184 | 32 | if fields.has_key("add"): |
paulb@184 | 33 | session[name] = value |
paulb@184 | 34 | message = "Attribute %s added!" % name |
paulb@184 | 35 | |
paulb@184 | 36 | elif fields.has_key("delete"): |
paulb@264 | 37 | try: |
paulb@264 | 38 | del session[name] |
paulb@264 | 39 | except KeyError: |
paulb@264 | 40 | pass |
paulb@184 | 41 | message = "Attribute %s deleted!" % name |
paulb@184 | 42 | |
paulb@184 | 43 | elif fields.has_key("expire"): |
paulb@184 | 44 | trans.expire_session() |
paulb@184 | 45 | message = "Session expired!" |
paulb@287 | 46 | session = None |
paulb@184 | 47 | else: |
paulb@184 | 48 | message = "No session present - use create to add one." |
paulb@184 | 49 | |
paulb@184 | 50 | # If a session exists, get its contents. |
paulb@184 | 51 | |
paulb@241 | 52 | if session is not None: |
paulb@184 | 53 | session_items = session.items() |
paulb@184 | 54 | else: |
paulb@184 | 55 | session_items = [] |
paulb@184 | 56 | |
paulb@184 | 57 | # Get some information. |
paulb@184 | 58 | |
paulb@184 | 59 | out = trans.get_response_stream() |
paulb@184 | 60 | out.write(""" |
paulb@184 | 61 | <html> |
paulb@184 | 62 | <head> |
paulb@184 | 63 | <title>Session Example</title> |
paulb@184 | 64 | </head> |
paulb@184 | 65 | <body> |
paulb@184 | 66 | <h1>Session Details</h1> |
paulb@184 | 67 | <p>%s</p> |
paulb@381 | 68 | <dl> |
paulb@184 | 69 | %s |
paulb@381 | 70 | </dl> |
paulb@264 | 71 | <h2>Session</h2> |
paulb@264 | 72 | <form method="POST"> |
paulb@264 | 73 | <p> |
paulb@264 | 74 | <input name="create" type="submit" value="Create..."/> |
paulb@264 | 75 | <input name="expire" type="submit" value="Expire..."/> |
paulb@264 | 76 | </p> |
paulb@264 | 77 | <p>Name: <input name="name"/></p> |
paulb@264 | 78 | <p>Value: <input name="value"/></p> |
paulb@264 | 79 | <p> |
paulb@264 | 80 | <input name="add" type="submit" value="Add..."/> |
paulb@264 | 81 | <input name="delete" type="submit" value="Delete..."/> |
paulb@374 | 82 | <input name="refresh" type="submit" value="Refresh..."/> |
paulb@264 | 83 | </p> |
paulb@264 | 84 | </form> |
paulb@184 | 85 | </body> |
paulb@184 | 86 | </html> |
paulb@184 | 87 | """ % ( |
paulb@184 | 88 | message, |
paulb@184 | 89 | self._format_attributes(session_items), |
paulb@184 | 90 | )) |
paulb@184 | 91 | |
paulb@184 | 92 | def _format_attributes(self, items): |
paulb@184 | 93 | return "".join([ |
paulb@184 | 94 | "<dt>%s</dt><dd>%s</dd>" % (key, value) |
paulb@184 | 95 | for key, value in items |
paulb@184 | 96 | ]) |
paulb@184 | 97 | |
paulb@184 | 98 | # vim: tabstop=4 expandtab shiftwidth=4 |