# HG changeset patch # User paulb # Date 1114967078 0 # Node ID f887efc28abde834f3c7c301cc0212f11fe87c48 # Parent d6ab8969b2acf839fa7a684044774db9b93ec7ed [project @ 2005-05-01 17:04:38 by paulb] Made the calendar example a bit more useful, although still misnamed in relation to what it does. diff -r d6ab8969b2ac -r f887efc28abd examples/Common/Calendar/__init__.py --- a/examples/Common/Calendar/__init__.py Sun May 01 17:04:13 2005 +0000 +++ b/examples/Common/Calendar/__init__.py Sun May 01 17:04:38 2005 +0000 @@ -3,6 +3,7 @@ "A calendar application." import WebStack.Generic +import time class DirectoryResource: @@ -20,33 +21,108 @@ one. """ - # Get the last path component as the name of the calendar. - # NOTE: This could be improved to permit hierarchical naming. + # Remember uploaded calendars using a session. - calendar_name = trans.get_path_without_query().split("/")[-1] + session = trans.get_session(create=1) # Determine the action to be taken. method = trans.get_request_method() - print method # NOTE: Some frameworks do not pass in the content type. content_type = trans.get_content_type() + # Handle uploads. + if method == "PUT": + + # Get the last path component as the name of the calendar. + # NOTE: This could be improved to permit hierarchical naming. + + calendar_name = trans.get_path_without_query().split("/")[-1] input = trans.get_request_stream() - print "Name:", calendar_name - print "Content type:", content_type.media_type - print "--------" - print input.read() - print "--------" - else: + data = input.read() + + # Store the calendar in the session. - # NOTE: Send the appropriate kind of response. + session["calendar name"] = calendar_name + session["media type"] = content_type.media_type + session["calendar data"] = data + session["calendar size"] = len(data) + session["calendar time"] = time.strftime("%Y-%m-%dT%T") + # Handle directory browsing. + + elif method == "PROPFIND": + trans.set_response_code(207) trans.set_content_type(WebStack.Generic.ContentType("text/html")) out = trans.get_response_stream() - out.write("

Calendar %s

" % calendar_name) + out.write(""" + +""") + + if trans.get_path_info() == "/": + out.write(""" + + %s + + + %s + %s + + + + + HTTP/1.1 200 OK + + +""" % (trans.get_path_without_query(), time.strftime("%Y-%m-%dT%T"), trans.get_path_without_query())) + + if session.has_key("calendar name"): + out.write(""" + + %s%s + + + %s + %s + + %s + + HTTP/1.1 200 OK + + +""" % (trans.get_path_without_query(), session.get("calendar name"), session.get("calendar time") or time.strftime("%Y-%m-%dT%T"), + session.get("calendar name"), session.get("media type") or "")) + + out.write(""" + +""") + + # Handle downloads. + + elif method == "GET": + trans.set_content_type(WebStack.Generic.ContentType("text/html")) + out = trans.get_response_stream() + out.write(""" + + + Last Uploaded Calendar + + +

Calendar %s

+

Media type: %s

+

Calendar size: %s

+
%s
+ + +""" % (session.get("calendar name"), session.get("media type"), session.get("calendar size"), + session.get("calendar data", "").replace("&", "&").replace("<", "<").replace(">", ">"))) + + # Disallow other methods. + + else: + trans.set_response_code(405) # vim: tabstop=4 expandtab shiftwidth=4