paulb@22 | 1 | #!/usr/bin/env python |
paulb@22 | 2 | |
paulb@22 | 3 | "A calendar application." |
paulb@22 | 4 | |
paulb@22 | 5 | import WebStack.Generic |
paulb@22 | 6 | |
paulb@22 | 7 | class DirectoryResource: |
paulb@22 | 8 | |
paulb@22 | 9 | """ |
paulb@22 | 10 | A resource which handles incoming calendars and viewing requests. |
paulb@22 | 11 | An arbitrary set of rules can be applied to determine what is to be done |
paulb@22 | 12 | with a request, and in this case, the application appears as a directory of |
paulb@22 | 13 | calendars, yet also accepts incoming calendars. |
paulb@22 | 14 | """ |
paulb@22 | 15 | |
paulb@22 | 16 | def respond(self, trans): |
paulb@22 | 17 | |
paulb@22 | 18 | """ |
paulb@22 | 19 | Examine the incoming request, either saving a calendar or displaying |
paulb@22 | 20 | one. |
paulb@22 | 21 | """ |
paulb@22 | 22 | |
paulb@22 | 23 | # Get the last path component as the name of the calendar. |
paulb@22 | 24 | # NOTE: This could be improved to permit hierarchical naming. |
paulb@22 | 25 | |
paulb@169 | 26 | calendar_name = trans.get_path_without_query().split("/")[-1] |
paulb@22 | 27 | |
paulb@22 | 28 | # Determine the action to be taken. |
paulb@22 | 29 | |
paulb@22 | 30 | method = trans.get_request_method() |
paulb@22 | 31 | print method |
paulb@22 | 32 | |
paulb@22 | 33 | # NOTE: Some frameworks do not pass in the content type. |
paulb@22 | 34 | |
paulb@22 | 35 | content_type = trans.get_content_type() |
paulb@22 | 36 | |
paulb@22 | 37 | if method == "PUT": |
paulb@22 | 38 | input = trans.get_request_stream() |
paulb@22 | 39 | print "Name:", calendar_name |
paulb@22 | 40 | print "Content type:", content_type.content_type |
paulb@22 | 41 | print "--------" |
paulb@22 | 42 | print input.read() |
paulb@22 | 43 | print "--------" |
paulb@22 | 44 | else: |
paulb@22 | 45 | |
paulb@22 | 46 | # NOTE: Send the appropriate kind of response. |
paulb@22 | 47 | |
paulb@22 | 48 | trans.set_content_type(WebStack.Generic.ContentType("text/html")) |
paulb@22 | 49 | out = trans.get_response_stream() |
paulb@22 | 50 | out.write("<html><body><h1>Calendar %s</h1></body></html>" % calendar_name) |
paulb@22 | 51 | |
paulb@22 | 52 | # vim: tabstop=4 expandtab shiftwidth=4 |