[CalendarServer-changes] [14443] CalendarServer/branches/users/sagen/trashcan-3/txdav

source_changes at macosforge.org source_changes at macosforge.org
Thu Feb 19 12:05:52 PST 2015


Revision: 14443
          http://trac.calendarserver.org//changeset/14443
Author:   sagen at apple.com
Date:     2015-02-19 12:05:52 -0800 (Thu, 19 Feb 2015)
Log Message:
-----------
Simple scheduling/trash unit tests passing

Modified Paths:
--------------
    CalendarServer/branches/users/sagen/trashcan-3/txdav/caldav/datastore/scheduling/icalsplitter.py
    CalendarServer/branches/users/sagen/trashcan-3/txdav/caldav/datastore/scheduling/implicit.py
    CalendarServer/branches/users/sagen/trashcan-3/txdav/caldav/datastore/sql.py
    CalendarServer/branches/users/sagen/trashcan-3/txdav/common/datastore/sql.py
    CalendarServer/branches/users/sagen/trashcan-3/txdav/common/datastore/sql_schema/current.sql
    CalendarServer/branches/users/sagen/trashcan-3/txdav/common/datastore/test/test_sql.py

Modified: CalendarServer/branches/users/sagen/trashcan-3/txdav/caldav/datastore/scheduling/icalsplitter.py
===================================================================
--- CalendarServer/branches/users/sagen/trashcan-3/txdav/caldav/datastore/scheduling/icalsplitter.py	2015-02-19 19:53:37 UTC (rev 14442)
+++ CalendarServer/branches/users/sagen/trashcan-3/txdav/caldav/datastore/scheduling/icalsplitter.py	2015-02-19 20:05:52 UTC (rev 14443)
@@ -29,7 +29,7 @@
 
     uuid_namespace = uuid.UUID("1F50F5E1-3E10-4A85-A8B4-3906DA3B8C52")
 
-    def __init__(self, threshold, past):
+    def __init__(self, threshold=-1, past=1):
         """
         @param threshold: the size in bytes that will trigger a split
         @type threshold: C{int}
@@ -70,23 +70,27 @@
         if len(instances) <= 1 or instances[0].start >= self.past or instances[-1].start < self.now:
             return False
 
-        # Make sure there are some overridden components in the past - as splitting only makes sense when
-        # overrides are present
-        past_count = 0
-        for instance in instances:
-            if instance.start >= self.past:
-                break
-            elif instance.component.hasProperty("RECURRENCE-ID"):
-                past_count += 1
+        if self.threshold != -1:
+            # Make sure there are some overridden components in the past - as splitting only makes sense when
+            # overrides are present
+            past_count = 0
+            for instance in instances:
+                if instance.start >= self.past:
+                    break
+                elif instance.component.hasProperty("RECURRENCE-ID"):
+                    past_count += 1
 
-        # Only split when there is more than one past override to split off
-        if past_count < 2:
-            return False
+            # Only split when there is more than one past override to split off
+            if past_count < 2:
+                return False
 
-        # Now see if overall size exceeds our threshold
-        return len(str(ical)) > self.threshold
+            # Now see if overall size exceeds our threshold
+            return len(str(ical)) > self.threshold
 
+        else:
+            return True
 
+
     def whereSplit(self, ical, break_point=None, allow_past_the_end=True):
         """
         Determine where a split is going to happen - i.e., the RECURRENCE-ID.

Modified: CalendarServer/branches/users/sagen/trashcan-3/txdav/caldav/datastore/scheduling/implicit.py
===================================================================
--- CalendarServer/branches/users/sagen/trashcan-3/txdav/caldav/datastore/scheduling/implicit.py	2015-02-19 19:53:37 UTC (rev 14442)
+++ CalendarServer/branches/users/sagen/trashcan-3/txdav/caldav/datastore/scheduling/implicit.py	2015-02-19 20:05:52 UTC (rev 14443)
@@ -762,6 +762,8 @@
                                     attendee.setParameter("X-CALENDARSERVER-RESET-PARTSTAT", str(seq))
                 else:
                     log.debug("Implicit - organizer '{organizer}' is splitting UID: '{uid}'", organizer=self.organizer, uid=self.uid)
+                    if self.split_details[3]:
+                        self.coerceAttendeesPartstatOnCreate()
 
                 # Check for removed attendees
                 if not recurrence_reschedule:
@@ -1277,7 +1279,7 @@
                 else:
                     # Add split details if needed
                     if self.split_details is not None:
-                        rid, uid, newer_piece = self.split_details
+                        rid, uid, newer_piece, ignore = self.split_details
                         itipmsg.addProperty(Property("X-CALENDARSERVER-SPLIT-RID", rid))
                         itipmsg.addProperty(Property("X-CALENDARSERVER-SPLIT-OLDER-UID" if newer_piece else "X-CALENDARSERVER-SPLIT-NEWER-UID", uid))
 
@@ -1353,7 +1355,7 @@
                 else:
                     # Add split details if needed
                     if self.split_details is not None:
-                        rid, uid, newer_piece = self.split_details
+                        rid, uid, newer_piece, ignore = self.split_details
                         itipmsg.addProperty(Property("X-CALENDARSERVER-SPLIT-RID", rid))
                         itipmsg.addProperty(Property("X-CALENDARSERVER-SPLIT-OLDER-UID" if newer_piece else "X-CALENDARSERVER-SPLIT-NEWER-UID", uid))
 

Modified: CalendarServer/branches/users/sagen/trashcan-3/txdav/caldav/datastore/sql.py
===================================================================
--- CalendarServer/branches/users/sagen/trashcan-3/txdav/caldav/datastore/sql.py	2015-02-19 19:53:37 UTC (rev 14442)
+++ CalendarServer/branches/users/sagen/trashcan-3/txdav/caldav/datastore/sql.py	2015-02-19 20:05:52 UTC (rev 14443)
@@ -3094,7 +3094,7 @@
 
 
     @inlineCallbacks
-    def doImplicitScheduling(self, component, inserting, internal_state, options, split_details=None):
+    def doImplicitScheduling(self, component, inserting, internal_state, options, split_details=None, updateSelf=False):
 
         new_component = None
         did_implicit_action = False
@@ -3119,9 +3119,12 @@
             scheduler = ImplicitScheduler(logItems=self._txn.logItems, options=options)
 
             # PUT
+            resourceToUpdate = None if inserting else self
+            # (If we're in the trash, it's okay to update this, even if inserting=True)
+            resourceToUpdate = self if updateSelf else resourceToUpdate
             do_implicit_action, is_scheduling_resource = (yield scheduler.testImplicitSchedulingPUT(
                 self.calendar(),
-                None if inserting else self,
+                resourceToUpdate,
                 component,
                 internal_request=is_internal,
             ))
@@ -3281,7 +3284,7 @@
 
 
     @inlineCallbacks
-    def _setComponentInternal(self, component, inserting=False, internal_state=ComponentUpdateState.NORMAL, options=None, split_details=None):
+    def _setComponentInternal(self, component, inserting=False, internal_state=ComponentUpdateState.NORMAL, options=None, split_details=None, updateSelf=False):
         """
         Setting the component internally to the store itself. This will bypass a whole bunch of data consistency checks
         on the assumption that those have been done prior to the component data being provided, provided the flag is set.
@@ -3348,7 +3351,7 @@
                 yield self.decorateHostedStatus(component)
 
             # Do scheduling
-            implicit_result = (yield self.doImplicitScheduling(component, inserting, internal_state, options))
+            implicit_result = (yield self.doImplicitScheduling(component, inserting, internal_state, options, updateSelf=updateSelf))
             if isinstance(implicit_result, int):
                 if implicit_result == ImplicitScheduler.STATUS_ORPHANED_CANCELLED_EVENT:
                     raise ResourceDeletedError("Resource created but immediately deleted by the server.")
@@ -3389,7 +3392,8 @@
         elif self._txn._migrating:
             self.validCalendarDataCheck(component, inserting)
 
-        yield self.updateDatabase(component, inserting=inserting)
+        # If updateSelf is True, we want to turn inserting off within updateDatabase
+        yield self.updateDatabase(component, inserting=inserting if not updateSelf else False)
 
         # update GROUP_ATTENDEE table rows
         if inserting:
@@ -3936,8 +3940,20 @@
             if self._dropboxID:
                 yield DropBoxAttachment.resourceRemoved(self._txn, self._resourceID, self._dropboxID)
             yield ManagedAttachment.resourceRemoved(self._txn, self._resourceID)
-        yield super(CalendarObject, self).remove()
 
+        if isinbox: # bypass the trash
+            yield super(CalendarObject, self).reallyRemove()
+        else:
+            status = calendar.mainComponent().getProperty("STATUS")
+            if status is not None:
+                status = status.strvalue()
+                if status == "CANCELLED":
+                    yield super(CalendarObject, self).reallyRemove()
+                else:
+                    yield super(CalendarObject, self).remove()
+            else:
+                yield super(CalendarObject, self).remove()
+
         # Do scheduling
         if scheduler is not None:
             # Cannot do implicit in sharee's shared calendar
@@ -4690,7 +4706,7 @@
 
 
     @inlineCallbacks
-    def split(self, onlyThis=False, rid=None, olderUID=None):
+    def split(self, onlyThis=False, rid=None, olderUID=None, coercePartstatsInExistingResource=False, splitter=None):
         """
         Split this and all matching UID calendar objects as per L{iCalSplitter}.
 
@@ -4731,7 +4747,8 @@
         else:
             resources = (yield CalendarStoreFeatures(self._txn._store).calendarObjectsWithUID(self._txn, self._uid))
 
-        splitter = iCalSplitter(config.Scheduling.Options.Splitting.Size, config.Scheduling.Options.Splitting.PastDays)
+        if splitter is None:
+            splitter = iCalSplitter(config.Scheduling.Options.Splitting.Size, config.Scheduling.Options.Splitting.PastDays)
 
         # Determine the recurrence-id of the split and create a new UID for it
         calendar = (yield self.component())
@@ -4757,25 +4774,29 @@
             returnValue(None)
 
         # Store changed data
-        yield self._setComponentInternal(calendar_new, internal_state=ComponentUpdateState.SPLIT_OWNER, split_details=(rid, olderUID, True,))
+        yield self._setComponentInternal(calendar_new, internal_state=ComponentUpdateState.SPLIT_OWNER, split_details=(rid, olderUID, True, coercePartstatsInExistingResource))
         olderObject = yield self.calendar()._createCalendarObjectWithNameInternal(
             olderResourceName,
             calendar_old,
             ComponentUpdateState.SPLIT_OWNER,
-            split_details=(rid, newerUID, False,)
+            split_details=(rid, newerUID, False, False)
         )
 
         # Split each one - but not this resource
         for resource in resources:
             if resource._resourceID == self._resourceID:
                 continue
-            yield resource.splitForAttendee(rid, olderUID)
+            yield resource.splitForAttendee(
+                rid,
+                olderUID,
+                coercePartstatsInExistingResource=coercePartstatsInExistingResource
+            )
 
         returnValue(olderObject)
 
 
     @inlineCallbacks
-    def splitForAttendee(self, rid=None, olderUID=None):
+    def splitForAttendee(self, rid=None, olderUID=None, coercePartstatsInExistingResource=False):
         """
         Split this attendee resource as per L{split}. Note this is also used on any Organizer inbox items.
         Also, for inbox items, we are not protected by the ImplicitUID lock - it is possible that the inbox
@@ -4796,6 +4817,12 @@
 
         # Store changed data
         if ical_new.mainType() is not None:
+
+            if coercePartstatsInExistingResource:
+                for attendee in ical_new.getAllAttendeeProperties():
+                    if attendee.parameterValue("SCHEDULE-AGENT", "SERVER").upper() == "SERVER" and attendee.hasParameter("PARTSTAT"):
+                        attendee.setParameter("PARTSTAT", "NEEDS-ACTION")
+
             yield self._setComponentInternal(ical_new, internal_state=ComponentUpdateState.SPLIT_ATTENDEE)
         else:
             # The split removed all components from this object - remove it
@@ -4837,6 +4864,51 @@
 
 
 
+    @inlineCallbacks
+    def fromTrash(self):
+        yield super(CalendarObject, self).fromTrash()
+
+        caldata = yield self.componentForUser()
+        if caldata.getOrganizer():
+            # This is a scheduled event
+
+            splitter = iCalSplitter()
+            willSplit = splitter.willSplit(caldata)
+            if willSplit:
+                newPastResource = yield self.split(
+                    coercePartstatsInExistingResource=True,
+                    splitter=splitter
+                )
+                # original resource is the ongoing one,
+                # the new resource is the past one
+            else:
+                now = DateTime.getNowUTC()
+                now.setHHMMSS(0, 0, 0)
+                instances = caldata.cacheExpandedTimeRanges(now)
+                instances = sorted(instances.instances.values(), key=lambda x: x.start)
+                if instances[0].start >= now:
+                    # future
+                    newdata = caldata.duplicate()
+                    newdata.bumpiTIPInfo(doSequence=True)
+                    for attendee in newdata.getAllAttendeeProperties():
+                        if attendee.parameterValue("SCHEDULE-AGENT", "SERVER").upper() == "SERVER" and attendee.hasParameter("PARTSTAT"):
+                            attendee.setParameter("PARTSTAT", "NEEDS-ACTION")
+
+
+                    yield self._setComponentInternal(
+                        newdata, inserting=True, updateSelf=True
+                    )
+
+                else:
+                    # past
+                    yield ImplicitScheduler().refreshAllAttendeesExceptSome(
+                        self._txn,
+                        self,
+                    )
+
+
+
+
 class AttachmentStorageTransport(StorageTransportBase):
 
     _TEMPORARY_UPLOADS_DIRECTORY = "Temporary"
@@ -5763,6 +5835,9 @@
     def nameForResource(self, collection, objectResource):
         return "{}-{}".format(collection._resourceID, objectResource.name())
 
+    def originalParentForResource(self, objectResource):
+        parentID, resourceName = self.parseName(objectResource._name)
+        return self._home.childWithID(parentID)
 
     def parseName(self, name):
         parentID, resourceName = name.split("-", 1)

Modified: CalendarServer/branches/users/sagen/trashcan-3/txdav/common/datastore/sql.py
===================================================================
--- CalendarServer/branches/users/sagen/trashcan-3/txdav/common/datastore/sql.py	2015-02-19 19:53:37 UTC (rev 14442)
+++ CalendarServer/branches/users/sagen/trashcan-3/txdav/common/datastore/sql.py	2015-02-19 20:05:52 UTC (rev 14443)
@@ -5700,6 +5700,10 @@
         return False
 
 
+    def originalParentForResource(self, objectResource):
+        return succeed(objectResource._parentCollection)
+
+
     def memoMe(self, key, memo):
         """
         Add this object to the memo dictionary in whatever fashion is appropriate.
@@ -7069,6 +7073,13 @@
 
         if row:
             child = yield cls.makeClass(parent, row)
+
+            # Use the name that was passed in, especially to cover the case
+            # where the "trashed" name is prepended with the original collection
+            # id (otherwise the name will be missing that id)
+            if name is not None:
+                child._name = name
+
             returnValue(child)
         else:
             returnValue(None)
@@ -7400,8 +7411,9 @@
             self._txn, isTrash=True, resourceID=self._resourceID
         )
         yield self._parentCollection.removedObjectResource(self)
+        yield self._parentCollection._deleteRevision(self.name())
+
         trash = yield self._parentCollection._home.childWithName("trash")
-        print("TO TRASH", trash)
         if trash is not None:
             yield trash._insertRevision(
                 trash.nameForResource(
@@ -7413,21 +7425,24 @@
 
     @inlineCallbacks
     def fromTrash(self):
+        trash = self._parentCollection
+        self._parentCollection = yield trash.originalParentForResource(self)
         yield self._updateIsTrashQuery.on(
             self._txn, isTrash=False, resourceID=self._resourceID
         )
-        yield self._parentCollection.addedObjectResource(self)
-        trash = yield self._parentCollection._home.childWithName("trash")
-        print("FROM TRASH", trash)
-        if trash is not None:
-            yield trash._deleteRevision(
-                trash.nameForResource(
-                    self._parentCollection,
-                    self
-                )
+        yield trash._deleteRevision(
+            trash.nameForResource(
+                self._parentCollection,
+                self
             )
+        )
+        _ignored, self._name = trash.parseName(self._name)
 
+        yield self._parentCollection.addedObjectResource(self)
+        yield self._parentCollection._insertRevision(self.name())
 
+
+
     @classproperty
     def _selectIsTrashQuery(cls):
         obj = cls._objectSchema

Modified: CalendarServer/branches/users/sagen/trashcan-3/txdav/common/datastore/sql_schema/current.sql
===================================================================
--- CalendarServer/branches/users/sagen/trashcan-3/txdav/common/datastore/sql_schema/current.sql	2015-02-19 19:53:37 UTC (rev 14442)
+++ CalendarServer/branches/users/sagen/trashcan-3/txdav/common/datastore/sql_schema/current.sql	2015-02-19 20:05:52 UTC (rev 14443)
@@ -132,7 +132,8 @@
   SUPPORTED_COMPONENTS  varchar(255) default null,
   CREATED               timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
   MODIFIED              timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
-  CHILD_TYPE            varchar(10)  default null -- None, inbox, trash (FIXME: convert this to enumeration)
+  CHILD_TYPE            varchar(10)  default null, -- None, inbox, trash (FIXME: convert this to enumeration)
+  IS_TRASH             boolean      default false not null -- collection is in the trash
 
 );
 
@@ -260,8 +261,7 @@
   CREATED              timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
   MODIFIED             timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
   DATAVERSION          integer      default 0 not null,
-  IS_TRASH             boolean      default false not null, -- all instances are in the trash
-  HAS_TRASH            boolean      default false not null, -- has at least one instance in trash
+  IS_TRASH             boolean      default false not null, -- entire resource is in the trash
 
   unique (CALENDAR_RESOURCE_ID, RESOURCE_NAME) -- implicit index
 

Modified: CalendarServer/branches/users/sagen/trashcan-3/txdav/common/datastore/test/test_sql.py
===================================================================
--- CalendarServer/branches/users/sagen/trashcan-3/txdav/common/datastore/test/test_sql.py	2015-02-19 19:53:37 UTC (rev 14442)
+++ CalendarServer/branches/users/sagen/trashcan-3/txdav/common/datastore/test/test_sql.py	2015-02-19 20:05:52 UTC (rev 14443)
@@ -20,24 +20,27 @@
 
 from uuid import UUID
 
+from pycalendar.datetime import DateTime
 from twext.enterprise.dal.syntax import Insert
 from twext.enterprise.dal.syntax import Select
+from twext.enterprise.jobqueue import JobItem
+from twisted.internet import reactor
 from twisted.internet.defer import Deferred
 from twisted.internet.defer import inlineCallbacks, returnValue, succeed
 from twisted.internet.task import Clock
 from twisted.trial.unittest import TestCase
+from twistedcaldav.ical import Component
 from twistedcaldav.test.util import StoreTestCase
-from txdav.common.datastore.sql import fixUUIDNormalization
+# from twistedcaldav.vcard import Component as VCard
 from txdav.common.datastore.sql import (
     log, CommonStoreTransactionMonitor,
     CommonHome, CommonHomeChild, ECALENDARTYPE
 )
+from txdav.common.datastore.sql import fixUUIDNormalization
 from txdav.common.datastore.sql_tables import schema
 from txdav.common.datastore.test.util import CommonCommonTests
 from txdav.common.icommondatastore import AllRetriesFailed
 from txdav.xml import element as davxml
-from twistedcaldav.ical import Component
-from twistedcaldav.vcard import Component as VCard
 
 
 
@@ -470,8 +473,83 @@
 
 
 
+class StubTransaction(object):
 
-VCALENDAR_DATA_NO_SCHEDULING = """BEGIN:VCALENDAR
+    def __init__(self, label):
+        self.label = label
+        self.action = None
+
+
+    def commit(self):
+        self.action = "committed"
+        return succeed(None)
+
+
+    def abort(self):
+        self.action = "aborted"
+        return succeed(None)
+
+
+
+class CommonTrashTests(StoreTestCase):
+
+    @inlineCallbacks
+    def _collectionForUser(self, txn, userName, collectionName):
+        home = yield txn.calendarHomeWithUID(userName, create=True)
+        collection = yield home.childWithName(collectionName)
+        returnValue(collection)
+
+
+    @inlineCallbacks
+    def _createResource(self, txn, userName, collectionName, resourceName, data):
+        collection = yield self._collectionForUser(txn, userName, collectionName)
+        resource = yield collection.createObjectResourceWithName(
+            resourceName, Component.allFromString(data)
+        )
+        returnValue(resource)
+
+
+    @inlineCallbacks
+    def _getResource(self, txn, userName, collectionName, resourceName):
+        collection = yield self._collectionForUser(txn, userName, collectionName)
+        if not resourceName:
+            # Get the first one
+            resourceNames = yield collection.listObjectResources()
+            if len(resourceNames) == 0:
+                returnValue(None)
+            resourceName = resourceNames[0]
+        resource = yield collection.calendarObjectWithName(resourceName)
+        returnValue(resource)
+
+
+    @inlineCallbacks
+    def _getResourceNames(self, txn, userName, collectionName):
+        collection = yield self._collectionForUser(txn, userName, collectionName)
+        resourceNames = yield collection.listObjectResources()
+        returnValue(resourceNames)
+
+
+    @inlineCallbacks
+    def _updateResource(self, txn, userName, collectionName, resourceName, data):
+        resource = yield self._getResource(txn, userName, collectionName, resourceName)
+        yield resource.setComponent(Component.fromString(data))
+        returnValue(resource)
+
+
+    @inlineCallbacks
+    def _getResourceData(self, txn, userName, collectionName, resourceName):
+        resource = yield self._getResource(txn, userName, collectionName, resourceName)
+        component = yield resource.component()
+        returnValue(str(component).replace("\r\n ", ""))
+
+
+    @inlineCallbacks
+    def test_trashUnscheduled(self):
+        """
+        Verify the "resource is entirely in the trash" flag
+        """
+
+        data1 = """BEGIN:VCALENDAR
 VERSION:2.0
 PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN
 BEGIN:VEVENT
@@ -499,26 +577,6 @@
 END:VCALENDAR
 """
 
-VCARD_DATA = """BEGIN:VCARD
-VERSION:3.0
-PRODID:-//Apple Inc.//iOS 6.0//EN
-UID:41425f1b-b831-40f2-b0bb-e70ec0938afd
-FN:Test User
-N:User;Test;;;
-REV:20120613T002007Z
-TEL;type=CELL;type=VOICE;type=pref:(408) 555-1212
-END:VCARD
-"""
-
-
-class CommonTrashTests(StoreTestCase):
-
-    @inlineCallbacks
-    def test_isTrash(self):
-        """
-        Verify the "resource is entirely in the trash" flag
-        """
-
         txn = self.store.newTransaction()
 
         #
@@ -536,7 +594,7 @@
         # Create an object
         resource = yield collection.createObjectResourceWithName(
             "test.ics",
-            Component.allFromString(VCALENDAR_DATA_NO_SCHEDULING)
+            Component.allFromString(data1)
         )
 
         # One object in collection
@@ -562,9 +620,13 @@
 
         # One object in trash
         objects = yield trash.listObjectResources()
+        print("OBJECT LIST", objects)
         self.assertEquals(len(objects), 1)
 
         # Put back from trash
+        print("FETCHING FROM TRASH USING", objects[0])
+        resource = yield self._getResource(txn, "user01", "trash", objects[0])
+        print("OBJECT NAME", resource._name)
         yield resource.fromTrash()
 
         # Not in trash
@@ -579,61 +641,545 @@
         self.assertEquals(len(objects), 0)
 
 
-        #
-        # Now with addressbook
-        #
+        # Not implemented
+#         #
+#         # Now with addressbook
+#         #
 
-        home = yield txn.addressbookHomeWithUID("user01", create=True)
-        collection = yield home.childWithName("addressbook")
+#         data2 = """BEGIN:VCARD
+# VERSION:3.0
+# PRODID:-//Apple Inc.//iOS 6.0//EN
+# UID:41425f1b-b831-40f2-b0bb-e70ec0938afd
+# FN:Test User
+# N:User;Test;;;
+# REV:20120613T002007Z
+# TEL;type=CELL;type=VOICE;type=pref:(408) 555-1212
+# END:VCARD
+# """
 
-        # Create an object
-        resource = yield collection.createObjectResourceWithName(
-            "test.vcf",
-            VCard.fromString(VCARD_DATA)
+#         print("ADDRESSBOOK TIME")
+
+#         home = yield txn.addressbookHomeWithUID("user01", create=True)
+#         collection = yield home.childWithName("addressbook")
+#         trash = yield home.childWithName("trash")
+
+#         # Create an object
+#         resource = yield collection.createObjectResourceWithName(
+#             "test.vcf",
+#             VCard.fromString(data2)
+#         )
+
+#         # One object
+#         objects = yield collection.listObjectResources()
+#         self.assertEquals(len(objects), 1)
+
+#         # Verify it's not in the trash
+#         self.assertFalse((yield resource.isTrash()))
+
+#         # Move object to trash
+#         yield resource.toTrash()
+
+#         # Verify it's in the trash
+#         self.assertTrue((yield resource.isTrash()))
+
+#         # No objects
+#         objects = yield collection.listObjectResources()
+#         self.assertEquals(len(objects), 0)
+
+#         # One object in trash
+#         objects = yield trash.listObjectResources()
+#         print("OBJECT LIST", objects)
+#         self.assertEquals(len(objects), 1)
+
+#         # Put back from trash
+#         print("FETCHING FROM TRASH USING", objects[0])
+#         resource = yield self._getResource(txn, "user01", "trash", objects[0])
+#         yield resource.fromTrash()
+
+#         # Not in trash
+#         self.assertFalse((yield resource.isTrash()))
+
+#         # One object
+#         objects = yield collection.listObjectResources()
+#         self.assertEquals(len(objects), 1)
+
+#         yield txn.commit()
+
+
+
+
+    @inlineCallbacks
+    def test_trashScheduledFullyInFuture(self):
+
+        from twistedcaldav.stdconfig import config
+        self.patch(config, "EnableTrashCollection", True)
+
+        # A month in the future
+        start = DateTime.getNowUTC()
+        start.setHHMMSS(0, 0, 0)
+        start.offsetMonth(1)
+        end = DateTime.getNowUTC()
+        end.setHHMMSS(1, 0, 0)
+        end.offsetMonth(1)
+        subs = {
+            "start": start,
+            "end": end,
+        }
+
+        data1 = """BEGIN:VCALENDAR
+VERSION:2.0
+PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN
+BEGIN:VEVENT
+UID:12345-67890-attendee-reply
+DTSTART;TZID=America/Los_Angeles:%(start)s
+DTEND;TZID=America/Los_Angeles:%(end)s
+DTSTAMP:20150204T192546Z
+SUMMARY:Scheduled
+ORGANIZER;CN="User 01":mailto:user01 at example.com
+ATTENDEE:mailto:user01 at example.com
+ATTENDEE:mailto:user02 at example.com
+END:VEVENT
+END:VCALENDAR
+""" % subs
+
+        data2 = """BEGIN:VCALENDAR
+VERSION:2.0
+PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN
+BEGIN:VEVENT
+UID:12345-67890-attendee-reply
+DTSTART;TZID=America/Los_Angeles:%(start)s
+DTEND;TZID=America/Los_Angeles:%(end)s
+DTSTAMP:20150204T192546Z
+SUMMARY:Scheduled
+ORGANIZER;CN="User 01":mailto:user01 at example.com
+ATTENDEE:mailto:user01 at example.com
+ATTENDEE;PARTSTAT=ACCEPTED:mailto:user02 at example.com
+END:VEVENT
+END:VCALENDAR
+""" % subs
+
+        # user01 invites user02
+        print("TEST: user01 creates resource START")
+        txn = self.store.newTransaction()
+        yield self._createResource(
+            txn, "user01", "calendar", "test.ics", data1
         )
+        yield txn.commit()
 
-        # One object
-        objects = yield collection.listObjectResources()
-        self.assertEquals(len(objects), 1)
+        yield JobItem.waitEmpty(self.store.newTransaction, reactor, 60)
+        print("TEST: user01 creates resource END")
 
-        # Verify it's not in the trash
-        self.assertFalse((yield resource.isTrash()))
+        # user01's copy has SCHEDULE-STATUS update
+        txn = self.store.newTransaction()
+        data = yield self._getResourceData(txn, "user01", "calendar", "test.ics")
+        self.assertTrue("SCHEDULE-STATUS=1.2" in data)
 
-        # Move object to trash
-        yield resource.toTrash()
+        # user02 has an inbox item
+        resourceNames = yield self._getResourceNames(txn, "user02", "inbox")
+        self.assertEqual(len(resourceNames), 1)
 
-        # Verify it's in the trash
-        self.assertTrue((yield resource.isTrash()))
+        # user02 accepts
+        print("TEST: user02 accepts START")
+        yield self._updateResource(txn, "user02", "calendar", "", data2)
+        yield txn.commit()
 
-        # No objects
-        objects = yield collection.listObjectResources()
-        self.assertEquals(len(objects), 0)
+        yield JobItem.waitEmpty(self.store.newTransaction, reactor, 60)
+        print("TEST: user02 accepts END")
 
-        # Put back from trash
+        # user01 has an inbox item
+        txn = self.store.newTransaction()
+        resourceNames = yield self._getResourceNames(txn, "user01", "inbox")
+        self.assertEqual(len(resourceNames), 1)
+
+        # user01's copy has SCHEDULE-STATUS update
+        data = yield self._getResourceData(txn, "user01", "calendar", "test.ics")
+        self.assertTrue("SCHEDULE-STATUS=2.0" in data)
+        self.assertTrue("PARTSTAT=ACCEPTED" in data)
+        resource = yield self._getResource(txn, "user02", "inbox", "")
+        yield resource.remove()
+
+        yield txn.commit()
+
+        # user01 trashes event
+        print("TEST: user01 trashes event START")
+        txn = self.store.newTransaction()
+        resource = yield self._getResource(txn, "user01", "calendar", "test.ics")
+        yield resource.remove()
+        yield txn.commit()
+
+        yield JobItem.waitEmpty(self.store.newTransaction, reactor, 60)
+        print("TEST: user01 trashes event END")
+
+        # user01's copy is in the trash, still with user02 accepted
+        txn = self.store.newTransaction()
+        data = yield self._getResourceData(txn, "user01", "trash", "")
+        self.assertTrue("PARTSTAT=ACCEPTED" in data)
+        yield txn.commit()
+
+        # user02's copy is cancelled
+        txn = self.store.newTransaction()
+        data = yield self._getResourceData(txn, "user02", "inbox", "")
+        self.assertTrue("METHOD:CANCEL" in data)
+        resource = yield self._getResource(txn, "user02", "inbox", "")
+        yield resource.remove()
+        data = yield self._getResourceData(txn, "user02", "calendar", "")
+        self.assertTrue("STATUS:CANCELLED" in data)
+        resource = yield self._getResource(txn, "user02", "calendar", "")
+        print("PLUGH REMOVE user02's copy")
+        yield resource.remove()
+        print("PLUGH REMOVE user02's copy done")
+        data = yield self._getResource(txn, "user02", "trash", "")
+        self.assertEquals(data, None)
+        yield txn.commit()
+
+        # user01 restores event from the trash
+        print("TEST: user01 restores event START")
+        txn = self.store.newTransaction()
+        resource = yield self._getResource(txn, "user01", "trash", "")
+        data = yield self._getResourceData(txn, "user01", "trash", "")
+        print("user01 trashed copy", data)
+        print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
         yield resource.fromTrash()
+        yield txn.commit()
 
-        # Not in trash
-        self.assertFalse((yield resource.isTrash()))
+        yield JobItem.waitEmpty(self.store.newTransaction, reactor, 60)
+        print("TEST: user01 restores event END")
 
-        # One object
-        objects = yield collection.listObjectResources()
-        self.assertEquals(len(objects), 1)
+        txn = self.store.newTransaction()
 
+        # user01's copy should be back on their calendar
+        names = yield self._getResourceNames(txn, "user01", "calendar")
+        print("user01 names", names)
+        data = yield self._getResourceData(txn, "user01", "calendar", "")
+        print("WHAT?", data)
+        data = yield self._getResourceData(txn, "user01", "calendar", "test.ics")
+        print("user01 copy")
+        print(data)
+
+        # user02's copy should be back on their calendar
+        data = yield self._getResourceData(txn, "user02", "calendar", "")
+        print("user02 copy")
+        print(data)
+        self.assertTrue("PARTSTAT=NEEDS-ACTION" in data)
+
         yield txn.commit()
 
 
-class StubTransaction(object):
 
-    def __init__(self, label):
-        self.label = label
-        self.action = None
+    @inlineCallbacks
+    def test_trashScheduledFullyInPast(self):
 
+        from twistedcaldav.stdconfig import config
+        self.patch(config, "EnableTrashCollection", True)
 
-    def commit(self):
-        self.action = "committed"
-        return succeed(None)
+        # A month in the past
+        start = DateTime.getNowUTC()
+        start.setHHMMSS(0, 0, 0)
+        start.offsetMonth(-1)
+        end = DateTime.getNowUTC()
+        end.setHHMMSS(1, 0, 0)
+        end.offsetMonth(-1)
+        subs = {
+            "start": start,
+            "end": end,
+        }
 
+        data1 = """BEGIN:VCALENDAR
+VERSION:2.0
+PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN
+BEGIN:VEVENT
+UID:12345-67890-attendee-reply
+DTSTART;TZID=America/Los_Angeles:%(start)s
+DTEND;TZID=America/Los_Angeles:%(end)s
+DTSTAMP:20150204T192546Z
+SUMMARY:Scheduled
+ORGANIZER;CN="User 01":mailto:user01 at example.com
+ATTENDEE:mailto:user01 at example.com
+ATTENDEE:mailto:user02 at example.com
+END:VEVENT
+END:VCALENDAR
+""" % subs
 
-    def abort(self):
-        self.action = "aborted"
-        return succeed(None)
+        data2 = """BEGIN:VCALENDAR
+VERSION:2.0
+PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN
+BEGIN:VEVENT
+UID:12345-67890-attendee-reply
+DTSTART;TZID=America/Los_Angeles:%(start)s
+DTEND;TZID=America/Los_Angeles:%(end)s
+DTSTAMP:20150204T192546Z
+SUMMARY:Scheduled
+ORGANIZER;CN="User 01":mailto:user01 at example.com
+ATTENDEE:mailto:user01 at example.com
+ATTENDEE;PARTSTAT=TENTATIVE:mailto:user02 at example.com
+END:VEVENT
+END:VCALENDAR
+""" % subs
+
+        # user01 invites user02
+        txn = self.store.newTransaction()
+        yield self._createResource(
+            txn, "user01", "calendar", "test.ics", data1
+        )
+        yield txn.commit()
+
+        yield JobItem.waitEmpty(self.store.newTransaction, reactor, 60)
+
+        # user01's copy has SCHEDULE-STATUS update
+        txn = self.store.newTransaction()
+        data = yield self._getResourceData(txn, "user01", "calendar", "test.ics")
+        self.assertTrue("SCHEDULE-STATUS=1.2" in data)
+
+        # user02 has an inbox item
+        resourceNames = yield self._getResourceNames(txn, "user02", "inbox")
+        self.assertEqual(len(resourceNames), 1)
+
+        # user02 accepts
+        yield self._updateResource(txn, "user02", "calendar", "", data2)
+        yield txn.commit()
+
+        yield JobItem.waitEmpty(self.store.newTransaction, reactor, 60)
+
+        # user01 has an inbox item
+        txn = self.store.newTransaction()
+        resourceNames = yield self._getResourceNames(txn, "user01", "inbox")
+        self.assertEqual(len(resourceNames), 1)
+
+        # user01's copy has SCHEDULE-STATUS update
+        data = yield self._getResourceData(txn, "user01", "calendar", "test.ics")
+        self.assertTrue("SCHEDULE-STATUS=2.0" in data)
+        self.assertTrue("PARTSTAT=TENTATIVE" in data)
+        resource = yield self._getResource(txn, "user02", "inbox", "")
+        yield resource.remove()
+
+        yield txn.commit()
+
+        # user01 trashes event
+        txn = self.store.newTransaction()
+        resource = yield self._getResource(txn, "user01", "calendar", "test.ics")
+        yield resource.remove()
+        yield txn.commit()
+
+        yield JobItem.waitEmpty(self.store.newTransaction, reactor, 60)
+
+        # user01's copy is in the trash, still with user02 partstat
+        txn = self.store.newTransaction()
+        data = yield self._getResourceData(txn, "user01", "trash", "")
+        self.assertTrue("PARTSTAT=TENTATIVE" in data)
+        yield txn.commit()
+
+        # user02's copy is cancelled
+        txn = self.store.newTransaction()
+        data = yield self._getResourceData(txn, "user02", "inbox", "")
+        self.assertTrue("METHOD:CANCEL" in data)
+        resource = yield self._getResource(txn, "user02", "inbox", "")
+        yield resource.remove()
+        data = yield self._getResourceData(txn, "user02", "calendar", "")
+        self.assertTrue("STATUS:CANCELLED" in data)
+        resource = yield self._getResource(txn, "user02", "calendar", "")
+        print("PLUGH REMOVE user02's copy")
+        yield resource.remove()
+        print("PLUGH REMOVE user02's copy done")
+        data = yield self._getResource(txn, "user02", "trash", "")
+        self.assertEquals(data, None)
+        yield txn.commit()
+
+        # user01 restores event from the trash
+        txn = self.store.newTransaction()
+        resource = yield self._getResource(txn, "user01", "trash", "")
+        data = yield self._getResourceData(txn, "user01", "trash", "")
+        print("user01 trashed copy", data)
+        print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
+        yield resource.fromTrash()
+        yield txn.commit()
+
+        yield JobItem.waitEmpty(self.store.newTransaction, reactor, 60)
+
+        txn = self.store.newTransaction()
+
+        # user01's copy should be back on their calendar
+        data = yield self._getResourceData(txn, "user01", "calendar", "test.ics")
+        print("user01 copy")
+        print(data)
+
+        # user02's copy should be back on their calendar
+        data = yield self._getResourceData(txn, "user02", "calendar", "")
+        print("user02 copy")
+        print(data)
+        self.assertTrue("PARTSTAT=TENTATIVE" in data)
+
+
+        yield txn.commit()
+
+
+
+    @inlineCallbacks
+    def test_trashScheduledSpanningNow(self):
+
+        from twistedcaldav.stdconfig import config
+        self.patch(config, "EnableTrashCollection", True)
+
+        # A month in the past
+        start = DateTime.getNowUTC()
+        start.setHHMMSS(0, 0, 0)
+        start.offsetMonth(-1)
+        end = DateTime.getNowUTC()
+        end.setHHMMSS(1, 0, 0)
+        end.offsetMonth(-1)
+        subs = {
+            "start": start,
+            "end": end,
+        }
+
+        data1 = """BEGIN:VCALENDAR
+VERSION:2.0
+PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN
+BEGIN:VEVENT
+UID:12345-67890-attendee-reply
+DTSTART;TZID=America/Los_Angeles:%(start)s
+DTEND;TZID=America/Los_Angeles:%(end)s
+DTSTAMP:20150204T192546Z
+RRULE:FREQ=WEEKLY;COUNT=20
+SUMMARY:Scheduled
+ORGANIZER;CN="User 01":mailto:user01 at example.com
+ATTENDEE:mailto:user01 at example.com
+ATTENDEE:mailto:user02 at example.com
+END:VEVENT
+END:VCALENDAR
+""" % subs
+
+        data2 = """BEGIN:VCALENDAR
+VERSION:2.0
+PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN
+BEGIN:VEVENT
+UID:12345-67890-attendee-reply
+DTSTART;TZID=America/Los_Angeles:%(start)s
+DTEND;TZID=America/Los_Angeles:%(end)s
+DTSTAMP:20150204T192546Z
+RRULE:FREQ=WEEKLY;COUNT=20
+SUMMARY:Scheduled
+ORGANIZER;CN="User 01":mailto:user01 at example.com
+ATTENDEE:mailto:user01 at example.com
+ATTENDEE;PARTSTAT=ACCEPTED:mailto:user02 at example.com
+END:VEVENT
+END:VCALENDAR
+""" % subs
+
+        # user01 invites user02
+        txn = self.store.newTransaction()
+        yield self._createResource(
+            txn, "user01", "calendar", "test.ics", data1
+        )
+        yield txn.commit()
+
+        yield JobItem.waitEmpty(self.store.newTransaction, reactor, 60)
+
+        # user01's copy has SCHEDULE-STATUS update
+        txn = self.store.newTransaction()
+        data = yield self._getResourceData(txn, "user01", "calendar", "test.ics")
+        self.assertTrue("SCHEDULE-STATUS=1.2" in data)
+
+        # user02 has an inbox item
+        resourceNames = yield self._getResourceNames(txn, "user02", "inbox")
+        self.assertEqual(len(resourceNames), 1)
+
+        # user02 accepts
+        yield self._updateResource(txn, "user02", "calendar", "", data2)
+        yield txn.commit()
+
+        yield JobItem.waitEmpty(self.store.newTransaction, reactor, 60)
+
+        # user01 has an inbox item
+        txn = self.store.newTransaction()
+        resourceNames = yield self._getResourceNames(txn, "user01", "inbox")
+        self.assertEqual(len(resourceNames), 1)
+
+        # user01's copy has SCHEDULE-STATUS update
+        data = yield self._getResourceData(txn, "user01", "calendar", "test.ics")
+        self.assertTrue("SCHEDULE-STATUS=2.0" in data)
+        self.assertTrue("PARTSTAT=ACCEPTED" in data)
+        resource = yield self._getResource(txn, "user02", "inbox", "")
+        yield resource.remove()
+
+        yield txn.commit()
+
+        # user01 trashes event
+        txn = self.store.newTransaction()
+        resource = yield self._getResource(txn, "user01", "calendar", "test.ics")
+        yield resource.remove()
+        yield txn.commit()
+
+        yield JobItem.waitEmpty(self.store.newTransaction, reactor, 60)
+
+        # user01's copy is in the trash, still with user02 accepted
+        txn = self.store.newTransaction()
+        data = yield self._getResourceData(txn, "user01", "trash", "")
+        self.assertTrue("PARTSTAT=ACCEPTED" in data)
+        yield txn.commit()
+
+        # user02's copy is cancelled
+        txn = self.store.newTransaction()
+        data = yield self._getResourceData(txn, "user02", "inbox", "")
+        self.assertTrue("METHOD:CANCEL" in data)
+        resource = yield self._getResource(txn, "user02", "inbox", "")
+        yield resource.remove()
+        data = yield self._getResourceData(txn, "user02", "calendar", "")
+        self.assertTrue("STATUS:CANCELLED" in data)
+        resource = yield self._getResource(txn, "user02", "calendar", "")
+        print("PLUGH REMOVE user02's copy")
+        yield resource.remove()
+        print("PLUGH REMOVE user02's copy done")
+        data = yield self._getResource(txn, "user02", "trash", "")
+        self.assertEquals(data, None)
+        yield txn.commit()
+
+        # user01 restores event from the trash
+        txn = self.store.newTransaction()
+        resource = yield self._getResource(txn, "user01", "trash", "")
+        data = yield self._getResourceData(txn, "user01", "trash", "")
+        print("user01 trashed copy", data)
+        print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
+        yield resource.fromTrash()
+        yield txn.commit()
+
+        yield JobItem.waitEmpty(self.store.newTransaction, reactor, 60)
+
+        txn = self.store.newTransaction()
+
+        # resourceNames = yield self._getResourceNames(txn, "user01", "calendar")
+        # print("user01's calendar", resourceNames)
+
+        # user01's trash should be empty
+        resourceNames = yield self._getResourceNames(txn, "user01", "trash")
+        self.assertEquals(len(resourceNames), 0)
+
+        # user01 should have test.ics and a new .ics
+        resourceNames = yield self._getResourceNames(txn, "user01", "calendar")
+        self.assertEquals(len(resourceNames), 2)
+        self.assertTrue("test.ics" in resourceNames)
+        resourceNames.remove("test.ics")
+        newName = resourceNames[0]
+
+        # user01's test.ics -- verify it got split correctly
+        data = yield self._getResourceData(txn, "user01", "calendar", "test.ics")
+        print("user01 test.ics")
+        print(data)
+
+        # user01's new .ics -- verify it got split correctly
+        data = yield self._getResourceData(txn, "user01", "calendar", newName)
+        print("user01 new ics")
+        print(data)
+
+        resourceNames = yield self._getResourceNames(txn, "user02", "calendar")
+        print("user02's calendar", resourceNames)
+        resourceNames = yield self._getResourceNames(txn, "user02", "trash")
+        print("user02's trash", resourceNames)
+
+        # # user02's copy should be back on their calendar
+        # data = yield self._getResourceData(txn, "user02", "calendar", "")
+        # print("user02 copy")
+        # print(data)
+        # # self.assertTrue("PARTSTAT=NEEDS-ACTION" in data)
+
+        yield txn.commit()
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20150219/207446e7/attachment-0001.html>


More information about the calendarserver-changes mailing list