Parcourir la source

Add Last-Modified HTTP header for GET requests.

Guillaume Ayoub il y a 15 ans
Parent
commit
391037c24c
3 fichiers modifiés avec 17 ajouts et 0 suppressions
  1. 2 0
      NEWS
  2. 1 0
      radicale/__init__.py
  3. 14 0
      radicale/ical.py

+ 2 - 0
NEWS

@@ -9,6 +9,8 @@
 0.5 - **Not released yet**
 ==========================
 
+* Last-Modified HTTP header
+
 
 0.3 - Dancing Flowers
 =====================

+ 1 - 0
radicale/__init__.py

@@ -169,6 +169,7 @@ class CalendarHTTPHandler(server.BaseHTTPRequestHandler):
         self.send_response(client.OK)
         self.send_header("Content-Length", len(answer))
         self.send_header("Content-Type", "text/calendar")
+        self.send_header("Last-Modified", self._calendar.last_modified)
         self.send_header("ETag", etag)
         self.end_headers()
         self.wfile.write(answer)

+ 14 - 0
radicale/ical.py

@@ -27,6 +27,7 @@ Define the main classes of a calendar as seen from the server.
 
 import os
 import codecs
+import time
 
 from radicale import config
 
@@ -136,6 +137,9 @@ class Calendar(object):
         self.encoding = "utf-8"
         self.owner = path.split("/")[0]
         self.path = os.path.join(FOLDER, path.replace("/", os.path.sep))
+        # Create calendar if needed, useful for ``self.last_modified``
+        if not os.path.exists(self.path):
+            self.write()
 
     @staticmethod
     def _parse(text, item_types, name=None):
@@ -267,3 +271,13 @@ class Calendar(object):
     def timezones(self):
         """Get list of ``Timezome`` items in calendar."""
         return self._parse(self.text, (Timezone,))
+
+    @property
+    def last_modified(self):
+        """Get the last time the calendar has been modified.
+
+        The date is formatted according to rfc1123-5.2.14.
+
+        """
+        modification_time = time.localtime(os.path.getmtime(self.path))
+        return time.strftime("%a, %d %b %Y %H:%M:%S %Z", modification_time)