[CalendarServer-changes] [6297] CalendarServer/trunk

source_changes at macosforge.org source_changes at macosforge.org
Wed Sep 15 13:04:32 PDT 2010


Revision: 6297
          http://trac.macosforge.org/projects/calendarserver/changeset/6297
Author:   cdaboo at apple.com
Date:     2010-09-15 13:04:31 -0700 (Wed, 15 Sep 2010)
Log Message:
-----------
Improve detection of dropbox ids.

Modified Paths:
--------------
    CalendarServer/trunk/twistedcaldav/ical.py
    CalendarServer/trunk/txdav/caldav/datastore/util.py

Added Paths:
-----------
    CalendarServer/trunk/txdav/caldav/datastore/test/test_util.py

Modified: CalendarServer/trunk/twistedcaldav/ical.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/ical.py	2010-09-15 20:02:02 UTC (rev 6296)
+++ CalendarServer/trunk/twistedcaldav/ical.py	2010-09-15 20:04:31 UTC (rev 6297)
@@ -1805,6 +1805,35 @@
 
         return None
 
+    def getAllPropertiesInAnyComponent(self, properties, depth=2):
+        """
+        Get the all of any set of properties in any component down to a
+        specified depth.
+        
+        @param properties: property name(s) to test for
+        @type properties: C{list}, C{tuple} or C{str}
+        @param depth: how deep to go in looking at sub-components:
+            0: do not go into sub-components, 1: go into one level of sub-components, 
+            2: two levels (which is effectively all the levels supported in iCalendar)
+        @type depth: int
+        """
+
+        results = []
+
+        if isinstance(properties, str):
+            properties = (properties,)
+            
+        for property in properties:
+            props = tuple(self.properties(property))
+            if props:
+                results.extend(props)
+
+        if depth > 0:
+            for component in self.subcomponents():
+                results.extend(component.getAllPropertiesInAnyComponent(properties, depth - 1))
+
+        return results
+
     def hasPropertyValueInAllComponents(self, property):
         """
         Test for the existence of a property with a specific value in any sub-component.

Added: CalendarServer/trunk/txdav/caldav/datastore/test/test_util.py
===================================================================
--- CalendarServer/trunk/txdav/caldav/datastore/test/test_util.py	                        (rev 0)
+++ CalendarServer/trunk/txdav/caldav/datastore/test/test_util.py	2010-09-15 20:04:31 UTC (rev 6297)
@@ -0,0 +1,203 @@
+##
+# Copyright (c) 2010 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+"""
+Tests for txdav.caldav.datastore.util.
+"""
+
+from twistedcaldav.ical import Component
+from twistedcaldav.test.util import TestCase
+from txdav.caldav.datastore.util import dropboxIDFromCalendarObject
+
+class DropboxIDTests(TestCase):
+    """
+    Test dropbox ID extraction from calendar data.
+    """
+
+    class FakeCalendarResource(object):
+        """
+        Fake object resource to work with tests.
+        """
+        
+        def __init__(self, data):
+            
+            self.ical = Component.fromString(data)
+            
+        def component(self):
+            return self.ical
+    
+        def uid(self):
+            return self.ical.resourceUID()
+
+    def test_noAttachOrXdash(self):
+
+        resource = DropboxIDTests.FakeCalendarResource("""BEGIN:VCALENDAR
+VERSION:2.0
+BEGIN:VEVENT
+UID:12345-67890
+DTSTART:20071114T000000Z
+ATTENDEE:mailto:user1 at example.com
+ATTENDEE:mailto:user2 at example.com
+END:VEVENT
+END:VCALENDAR
+""")
+        
+        self.assertEquals(dropboxIDFromCalendarObject(resource), "12345-67890.dropbox")
+
+    def test_okXdash(self):
+
+        resource = DropboxIDTests.FakeCalendarResource("""BEGIN:VCALENDAR
+VERSION:2.0
+BEGIN:VEVENT
+UID:12345-67890
+DTSTART:20071114T000000Z
+ATTENDEE:mailto:user1 at example.com
+ATTENDEE:mailto:user2 at example.com
+X-APPLE-DROPBOX:http://example.com/calendars/__uids__/1234/dropbox/12345-67890X.dropbox
+END:VEVENT
+END:VCALENDAR
+""")
+        
+        self.assertEquals(dropboxIDFromCalendarObject(resource), "12345-67890X.dropbox")
+
+    def test_badXdash(self):
+
+        resource = DropboxIDTests.FakeCalendarResource("""BEGIN:VCALENDAR
+VERSION:2.0
+BEGIN:VEVENT
+UID:12345-67890
+DTSTART:20071114T000000Z
+ATTENDEE:mailto:user1 at example.com
+ATTENDEE:mailto:user2 at example.com
+X-APPLE-DROPBOX:
+END:VEVENT
+END:VCALENDAR
+""")
+        
+        self.assertEquals(dropboxIDFromCalendarObject(resource), "")
+
+    def test_okAttach(self):
+
+        resource = DropboxIDTests.FakeCalendarResource("""BEGIN:VCALENDAR
+VERSION:2.0
+BEGIN:VEVENT
+UID:12345-67890
+DTSTART:20071114T000000Z
+ATTENDEE:mailto:user1 at example.com
+ATTENDEE:mailto:user2 at example.com
+ATTACH;VALUE=URI:http://example.com/calendars/__uids__/1234/dropbox/12345-67890Y.dropbox/text.txt
+END:VEVENT
+END:VCALENDAR
+""")
+        
+        self.assertEquals(dropboxIDFromCalendarObject(resource), "12345-67890Y.dropbox")
+
+    def test_badAttach(self):
+
+        resource = DropboxIDTests.FakeCalendarResource("""BEGIN:VCALENDAR
+VERSION:2.0
+BEGIN:VEVENT
+UID:12345-67890
+DTSTART:20071114T000000Z
+ATTENDEE:mailto:user1 at example.com
+ATTENDEE:mailto:user2 at example.com
+ATTACH;VALUE=URI:tag:bogus
+END:VEVENT
+END:VCALENDAR
+""")
+        
+        self.assertEquals(dropboxIDFromCalendarObject(resource), "12345-67890.dropbox")
+
+    def test_inlineAttach(self):
+
+        resource = DropboxIDTests.FakeCalendarResource("""BEGIN:VCALENDAR
+VERSION:2.0
+BEGIN:VEVENT
+UID:12345-67890
+DTSTART:20071114T000000Z
+ATTENDEE:mailto:user1 at example.com
+ATTENDEE:mailto:user2 at example.com
+ATTACH:bmFzZTY0
+END:VEVENT
+END:VCALENDAR
+""")
+        
+        self.assertEquals(dropboxIDFromCalendarObject(resource), "12345-67890.dropbox")
+
+    def test_multipleAttach(self):
+
+        resource = DropboxIDTests.FakeCalendarResource("""BEGIN:VCALENDAR
+VERSION:2.0
+BEGIN:VEVENT
+UID:12345-67890
+DTSTART:20071114T000000Z
+ATTENDEE:mailto:user1 at example.com
+ATTENDEE:mailto:user2 at example.com
+ATTACH;VALUE=URI:tag:bogus
+ATTACH:bmFzZTY0
+ATTACH;VALUE=URI:http://example.com/calendars/__uids__/1234/dropbox/12345-67890Z.dropbox/text.txt
+END:VEVENT
+END:VCALENDAR
+""")
+        
+        self.assertEquals(dropboxIDFromCalendarObject(resource), "12345-67890Z.dropbox")
+
+    def test_okAttachRecurring(self):
+
+        resource = DropboxIDTests.FakeCalendarResource("""BEGIN:VCALENDAR
+VERSION:2.0
+BEGIN:VEVENT
+UID:12345-67890
+DTSTART:20071114T000000Z
+ATTENDEE:mailto:user1 at example.com
+ATTENDEE:mailto:user2 at example.com
+RRULE:FREQ=YEARLY
+END:VEVENT
+BEGIN:VEVENT
+UID:12345-67890
+RECURRENCE-ID:20081114T000000Z
+DTSTART:20071114T000000Z
+ATTENDEE:mailto:user1 at example.com
+ATTENDEE:mailto:user2 at example.com
+ATTACH;VALUE=URI:http://example.com/calendars/__uids__/1234/dropbox/12345-67890Y.dropbox/text.txt
+END:VEVENT
+END:VCALENDAR
+""")
+        
+        self.assertEquals(dropboxIDFromCalendarObject(resource), "12345-67890Y.dropbox")
+
+
+    def test_okAttachAlarm(self):
+
+        resource = DropboxIDTests.FakeCalendarResource("""BEGIN:VCALENDAR
+VERSION:2.0
+BEGIN:VEVENT
+UID:12345-67890
+DTSTART:20071114T000000Z
+ATTENDEE:mailto:user1 at example.com
+ATTENDEE:mailto:user2 at example.com
+BEGIN:VALARM
+ACTION:AUDIO
+ATTACH;VALUE=URI:Ping
+TRIGGER:-PT15M
+X-WR-ALARMUID:5548D654-8FDA-49DB-8983-8FCAD1F322B1
+END:VALARM
+END:VEVENT
+END:VCALENDAR
+""")
+        
+        self.assertEquals(dropboxIDFromCalendarObject(resource), "12345-67890.dropbox")
+

Modified: CalendarServer/trunk/txdav/caldav/datastore/util.py
===================================================================
--- CalendarServer/trunk/txdav/caldav/datastore/util.py	2010-09-15 20:02:02 UTC (rev 6296)
+++ CalendarServer/trunk/txdav/caldav/datastore/util.py	2010-09-15 20:04:31 UTC (rev 6297)
@@ -74,23 +74,33 @@
     @param calendarObject: The calendar object to retrieve a dropbox ID for.
     @type calendarObject: L{ICalendarObject}
     """
+
+    # Try "X-APPLE-DROPBOX" first
     dropboxProperty = calendarObject.component(
         ).getFirstPropertyInAnyComponent("X-APPLE-DROPBOX")
     if dropboxProperty is not None:
         componentDropboxID = dropboxProperty.value().split("/")[-1]
         return componentDropboxID
-    attachProperty = calendarObject.component().getFirstPropertyInAnyComponent(
-        "ATTACH"
+
+    # Now look at each ATTACH property and see if it might be a dropbox item
+    # and if so extract the id from that
+
+    attachments = calendarObject.component().getAllPropertiesInAnyComponent(
+        "ATTACH",
+        depth=1,
     )
-    if attachProperty is not None:
-        # Make sure the value type is URI
-        valueType = attachProperty.params().get("VALUE", ("TEXT",))
-        if valueType[0] == "URI":
-            # FIXME: more aggressive checking to see if this URI is really the
-            # 'right' URI.  Maybe needs to happen in the front end.
-            attachPath = attachProperty.value().split("/")[-2]
-            return attachPath
+    for attachment in attachments:
 
+        # Make sure the value type is URI and http(s) and it is in a dropbox
+        valueType = attachment.params().get("VALUE", ("TEXT",))
+        if valueType[0] == "URI" and attachment.value().startswith("http"):
+            segments = attachment.value().split("/")
+            try:
+                if segments[-3] == "dropbox":
+                    return segments[-2]
+            except IndexError:
+                pass
+
     return calendarObject.uid() + ".dropbox"
 
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20100915/a80e5ae1/attachment-0001.html>


More information about the calendarserver-changes mailing list