1.1 --- a/examples/Common/Calendar/__init__.py Thu Aug 18 21:09:33 2005 +0000
1.2 +++ b/examples/Common/Calendar/__init__.py Thu Aug 18 21:10:09 2005 +0000
1.3 @@ -4,8 +4,9 @@
1.4
1.5 import WebStack.Generic
1.6 import time
1.7 +import os
1.8
1.9 -class DirectoryResource:
1.10 +class CalendarResource:
1.11
1.12 """
1.13 A resource which handles incoming calendars and viewing requests.
1.14 @@ -14,6 +15,12 @@
1.15 calendars, yet also accepts incoming calendars.
1.16 """
1.17
1.18 + resource_dir = os.path.join(os.path.split(__file__)[0], "calendars")
1.19 +
1.20 + def __init__(self):
1.21 + if not os.path.exists(self.resource_dir):
1.22 + os.mkdir(self.resource_dir)
1.23 +
1.24 def respond(self, trans):
1.25
1.26 """
1.27 @@ -30,8 +37,10 @@
1.28 method = trans.get_request_method()
1.29
1.30 # NOTE: Some frameworks do not pass in the content type.
1.31 + # NOTE: We always assume that calendar files are being uploaded.
1.32
1.33 content_type = trans.get_content_type()
1.34 + calendar_name = trans.get_virtual_path_info().split("/")[-1]
1.35
1.36 # Handle uploads.
1.37
1.38 @@ -40,17 +49,14 @@
1.39 # Get the last path component as the name of the calendar.
1.40 # NOTE: This could be improved to permit hierarchical naming.
1.41
1.42 - calendar_name = trans.get_path_without_query().split("/")[-1]
1.43 input = trans.get_request_stream()
1.44 data = input.read()
1.45
1.46 - # Store the calendar in the session.
1.47 + # Store the calendar in the directory.
1.48
1.49 - session["calendar name"] = calendar_name
1.50 - session["media type"] = content_type.media_type
1.51 - session["calendar data"] = data
1.52 - session["calendar size"] = len(data)
1.53 - session["calendar time"] = time.strftime("%Y-%m-%dT%T")
1.54 + f = open(os.path.join(self.resource_dir, calendar_name), "wb")
1.55 + f.write(data)
1.56 + f.close()
1.57
1.58 # Handle directory browsing.
1.59
1.60 @@ -62,7 +68,8 @@
1.61 <D:multistatus xmlns:D="DAV:">
1.62 """)
1.63
1.64 - if trans.get_path_info() == "/":
1.65 + if trans.get_virtual_path_info() == "/":
1.66 + time_now = time.strftime("%Y-%m-%dT%TZ", time.gmtime(time.time()))
1.67 out.write("""
1.68 <D:response>
1.69 <D:href>%s</D:href>
1.70 @@ -77,9 +84,12 @@
1.71 <D:status>HTTP/1.1 200 OK</D:status>
1.72 </D:propstat>
1.73 </D:response>
1.74 -""" % (trans.get_path_without_query(), time.strftime("%Y-%m-%dT%T"), trans.get_path_without_query()))
1.75 +""" % (trans.get_path_without_query(), time_now, trans.get_path_without_query()))
1.76
1.77 - if session.has_key("calendar name"):
1.78 + for filename in os.listdir(self.resource_dir):
1.79 + pathname = os.path.join(self.resource_dir, filename)
1.80 + created = time.strftime("%Y-%m-%dT%TZ", time.gmtime(os.path.getctime(pathname)))
1.81 + size = os.path.getsize(pathname)
1.82 out.write("""
1.83 <D:response>
1.84 <D:href>%s%s</D:href>
1.85 @@ -89,12 +99,12 @@
1.86 <D:displayname>%s</D:displayname>
1.87 <D:resourcetype/>
1.88 <D:getcontenttype>%s</D:getcontenttype>
1.89 + <D:getcontentlength>%s</D:getcontentlength>
1.90 </D:prop>
1.91 <D:status>HTTP/1.1 200 OK</D:status>
1.92 </D:propstat>
1.93 </D:response>
1.94 -""" % (trans.get_path_without_query(), session.get("calendar name"), session.get("calendar time") or time.strftime("%Y-%m-%dT%T"),
1.95 - session.get("calendar name"), session.get("media type") or ""))
1.96 +""" % (trans.get_path_without_query(), filename, created, filename, "text/calendar", size))
1.97
1.98 out.write("""
1.99 </D:multistatus>
1.100 @@ -105,20 +115,48 @@
1.101 elif method == "GET":
1.102 trans.set_content_type(WebStack.Generic.ContentType("text/html"))
1.103 out = trans.get_response_stream()
1.104 - out.write("""
1.105 -<html>
1.106 - <head>
1.107 - <title>Last Uploaded Calendar</title>
1.108 - </head>
1.109 - <body>
1.110 - <h1>Calendar %s</h1>
1.111 - <p>Media type: %s</p>
1.112 - <p>Calendar size: %s</p>
1.113 - <pre>%s</pre>
1.114 - </body>
1.115 -</html>
1.116 -""" % (session.get("calendar name"), session.get("media type"), session.get("calendar size"),
1.117 - session.get("calendar data", "").replace("&", "&").replace("<", "<").replace(">", ">")))
1.118 + f = open(os.path.join(self.resource_dir, calendar_name))
1.119 + out.write(f.read())
1.120 + f.close()
1.121 +
1.122 + # Handle deletion.
1.123 +
1.124 + elif method == "DELETE":
1.125 + try:
1.126 + os.remove(os.path.join(self.resource_dir, calendar_name))
1.127 + except OSError:
1.128 + trans.set_response_code(500)
1.129 +
1.130 + # Handle renaming.
1.131 +
1.132 + elif method in ("MOVE", "COPY"):
1.133 + destinations = trans.get_header_values("Destination")
1.134 + if len(destinations) != 1:
1.135 + trans.set_response_code(500)
1.136 + else:
1.137 + try:
1.138 + # Convert the URL into a filename.
1.139 + # NOTE: Assume that the URL references the same "directory".
1.140 +
1.141 + destination = destinations[0].split("/")[-1]
1.142 +
1.143 + if method == "MOVE":
1.144 + os.rename(os.path.join(self.resource_dir, calendar_name), os.path.join(self.resource_dir, destination))
1.145 + elif method == "COPY":
1.146 + f_old = open(os.path.join(self.resource_dir, calendar_name), "rb")
1.147 + f_new = open(os.path.join(self.resource_dir, destination), "wb")
1.148 + f_new.write(f_old.read())
1.149 + f_new.close()
1.150 + f_old.close()
1.151 +
1.152 + # NOTE: We do not observe the rules regarding overwriting
1.153 + # NOTE: and the appropriate status codes.
1.154 +
1.155 + trans.set_header_value("Location", destinations[0])
1.156 + trans.set_response_code(201)
1.157 +
1.158 + except OSError:
1.159 + trans.set_response_code(500)
1.160
1.161 # Disallow other methods.
1.162