1.1 --- a/examples/Common/Calendar/__init__.py Sun May 01 17:04:13 2005 +0000
1.2 +++ b/examples/Common/Calendar/__init__.py Sun May 01 17:04:38 2005 +0000
1.3 @@ -3,6 +3,7 @@
1.4 "A calendar application."
1.5
1.6 import WebStack.Generic
1.7 +import time
1.8
1.9 class DirectoryResource:
1.10
1.11 @@ -20,33 +21,108 @@
1.12 one.
1.13 """
1.14
1.15 - # Get the last path component as the name of the calendar.
1.16 - # NOTE: This could be improved to permit hierarchical naming.
1.17 + # Remember uploaded calendars using a session.
1.18
1.19 - calendar_name = trans.get_path_without_query().split("/")[-1]
1.20 + session = trans.get_session(create=1)
1.21
1.22 # Determine the action to be taken.
1.23
1.24 method = trans.get_request_method()
1.25 - print method
1.26
1.27 # NOTE: Some frameworks do not pass in the content type.
1.28
1.29 content_type = trans.get_content_type()
1.30
1.31 + # Handle uploads.
1.32 +
1.33 if method == "PUT":
1.34 +
1.35 + # Get the last path component as the name of the calendar.
1.36 + # NOTE: This could be improved to permit hierarchical naming.
1.37 +
1.38 + calendar_name = trans.get_path_without_query().split("/")[-1]
1.39 input = trans.get_request_stream()
1.40 - print "Name:", calendar_name
1.41 - print "Content type:", content_type.media_type
1.42 - print "--------"
1.43 - print input.read()
1.44 - print "--------"
1.45 - else:
1.46 + data = input.read()
1.47 +
1.48 + # Store the calendar in the session.
1.49
1.50 - # NOTE: Send the appropriate kind of response.
1.51 + session["calendar name"] = calendar_name
1.52 + session["media type"] = content_type.media_type
1.53 + session["calendar data"] = data
1.54 + session["calendar size"] = len(data)
1.55 + session["calendar time"] = time.strftime("%Y-%m-%dT%T")
1.56
1.57 + # Handle directory browsing.
1.58 +
1.59 + elif method == "PROPFIND":
1.60 + trans.set_response_code(207)
1.61 trans.set_content_type(WebStack.Generic.ContentType("text/html"))
1.62 out = trans.get_response_stream()
1.63 - out.write("<html><body><h1>Calendar %s</h1></body></html>" % calendar_name)
1.64 + out.write("""<?xml version="1.0"?>
1.65 +<D:multistatus xmlns:D="DAV:">
1.66 +""")
1.67 +
1.68 + if trans.get_path_info() == "/":
1.69 + out.write("""
1.70 + <D:response>
1.71 + <D:href>%s</D:href>
1.72 + <D:propstat>
1.73 + <D:prop>
1.74 + <D:creationdate>%s</D:creationdate>
1.75 + <D:displayname>%s</D:displayname>
1.76 + <D:resourcetype>
1.77 + <D:collection/>
1.78 + </D:resourcetype>
1.79 + </D:prop>
1.80 + <D:status>HTTP/1.1 200 OK</D:status>
1.81 + </D:propstat>
1.82 + </D:response>
1.83 +""" % (trans.get_path_without_query(), time.strftime("%Y-%m-%dT%T"), trans.get_path_without_query()))
1.84 +
1.85 + if session.has_key("calendar name"):
1.86 + out.write("""
1.87 + <D:response>
1.88 + <D:href>%s%s</D:href>
1.89 + <D:propstat>
1.90 + <D:prop>
1.91 + <D:creationdate>%s</D:creationdate>
1.92 + <D:displayname>%s</D:displayname>
1.93 + <D:resourcetype/>
1.94 + <D:getcontenttype>%s</D:getcontenttype>
1.95 + </D:prop>
1.96 + <D:status>HTTP/1.1 200 OK</D:status>
1.97 + </D:propstat>
1.98 + </D:response>
1.99 +""" % (trans.get_path_without_query(), session.get("calendar name"), session.get("calendar time") or time.strftime("%Y-%m-%dT%T"),
1.100 + session.get("calendar name"), session.get("media type") or ""))
1.101 +
1.102 + out.write("""
1.103 +</D:multistatus>
1.104 +""")
1.105 +
1.106 + # Handle downloads.
1.107 +
1.108 + elif method == "GET":
1.109 + trans.set_content_type(WebStack.Generic.ContentType("text/html"))
1.110 + out = trans.get_response_stream()
1.111 + out.write("""
1.112 +<html>
1.113 + <head>
1.114 + <title>Last Uploaded Calendar</title>
1.115 + </head>
1.116 + <body>
1.117 + <h1>Calendar %s</h1>
1.118 + <p>Media type: %s</p>
1.119 + <p>Calendar size: %s</p>
1.120 + <pre>%s</pre>
1.121 + </body>
1.122 +</html>
1.123 +""" % (session.get("calendar name"), session.get("media type"), session.get("calendar size"),
1.124 + session.get("calendar data", "").replace("&", "&").replace("<", "<").replace(">", ">")))
1.125 +
1.126 + # Disallow other methods.
1.127 +
1.128 + else:
1.129 + trans.set_response_code(405)
1.130
1.131 # vim: tabstop=4 expandtab shiftwidth=4