<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[14443] CalendarServer/branches/users/sagen/trashcan-3/txdav</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/14443">14443</a></dd>
<dt>Author</dt> <dd>sagen@apple.com</dd>
<dt>Date</dt> <dd>2015-02-19 12:05:52 -0800 (Thu, 19 Feb 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Simple scheduling/trash unit tests passing</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServerbranchesuserssagentrashcan3txdavcaldavdatastoreschedulingicalsplitterpy">CalendarServer/branches/users/sagen/trashcan-3/txdav/caldav/datastore/scheduling/icalsplitter.py</a></li>
<li><a href="#CalendarServerbranchesuserssagentrashcan3txdavcaldavdatastoreschedulingimplicitpy">CalendarServer/branches/users/sagen/trashcan-3/txdav/caldav/datastore/scheduling/implicit.py</a></li>
<li><a href="#CalendarServerbranchesuserssagentrashcan3txdavcaldavdatastoresqlpy">CalendarServer/branches/users/sagen/trashcan-3/txdav/caldav/datastore/sql.py</a></li>
<li><a href="#CalendarServerbranchesuserssagentrashcan3txdavcommondatastoresqlpy">CalendarServer/branches/users/sagen/trashcan-3/txdav/common/datastore/sql.py</a></li>
<li><a href="#CalendarServerbranchesuserssagentrashcan3txdavcommondatastoresql_schemacurrentsql">CalendarServer/branches/users/sagen/trashcan-3/txdav/common/datastore/sql_schema/current.sql</a></li>
<li><a href="#CalendarServerbranchesuserssagentrashcan3txdavcommondatastoretesttest_sqlpy">CalendarServer/branches/users/sagen/trashcan-3/txdav/common/datastore/test/test_sql.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServerbranchesuserssagentrashcan3txdavcaldavdatastoreschedulingicalsplitterpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/sagen/trashcan-3/txdav/caldav/datastore/scheduling/icalsplitter.py (14442 => 14443)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -29,7 +29,7 @@
</span><span class="cx"> 
</span><span class="cx">     uuid_namespace = uuid.UUID(&quot;1F50F5E1-3E10-4A85-A8B4-3906DA3B8C52&quot;)
</span><span class="cx"> 
</span><del>-    def __init__(self, threshold, past):
</del><ins>+    def __init__(self, threshold=-1, past=1):
</ins><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         @param threshold: the size in bytes that will trigger a split
</span><span class="cx">         @type threshold: C{int}
</span><span class="lines">@@ -70,23 +70,27 @@
</span><span class="cx">         if len(instances) &lt;= 1 or instances[0].start &gt;= self.past or instances[-1].start &lt; self.now:
</span><span class="cx">             return False
</span><span class="cx"> 
</span><del>-        # 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 &gt;= self.past:
-                break
-            elif instance.component.hasProperty(&quot;RECURRENCE-ID&quot;):
-                past_count += 1
</del><ins>+        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 &gt;= self.past:
+                    break
+                elif instance.component.hasProperty(&quot;RECURRENCE-ID&quot;):
+                    past_count += 1
</ins><span class="cx"> 
</span><del>-        # Only split when there is more than one past override to split off
-        if past_count &lt; 2:
-            return False
</del><ins>+            # Only split when there is more than one past override to split off
+            if past_count &lt; 2:
+                return False
</ins><span class="cx"> 
</span><del>-        # Now see if overall size exceeds our threshold
-        return len(str(ical)) &gt; self.threshold
</del><ins>+            # Now see if overall size exceeds our threshold
+            return len(str(ical)) &gt; self.threshold
</ins><span class="cx"> 
</span><ins>+        else:
+            return True
</ins><span class="cx"> 
</span><ins>+
</ins><span class="cx">     def whereSplit(self, ical, break_point=None, allow_past_the_end=True):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Determine where a split is going to happen - i.e., the RECURRENCE-ID.
</span></span></pre></div>
<a id="CalendarServerbranchesuserssagentrashcan3txdavcaldavdatastoreschedulingimplicitpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/sagen/trashcan-3/txdav/caldav/datastore/scheduling/implicit.py (14442 => 14443)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -762,6 +762,8 @@
</span><span class="cx">                                     attendee.setParameter(&quot;X-CALENDARSERVER-RESET-PARTSTAT&quot;, str(seq))
</span><span class="cx">                 else:
</span><span class="cx">                     log.debug(&quot;Implicit - organizer '{organizer}' is splitting UID: '{uid}'&quot;, organizer=self.organizer, uid=self.uid)
</span><ins>+                    if self.split_details[3]:
+                        self.coerceAttendeesPartstatOnCreate()
</ins><span class="cx"> 
</span><span class="cx">                 # Check for removed attendees
</span><span class="cx">                 if not recurrence_reschedule:
</span><span class="lines">@@ -1277,7 +1279,7 @@
</span><span class="cx">                 else:
</span><span class="cx">                     # Add split details if needed
</span><span class="cx">                     if self.split_details is not None:
</span><del>-                        rid, uid, newer_piece = self.split_details
</del><ins>+                        rid, uid, newer_piece, ignore = self.split_details
</ins><span class="cx">                         itipmsg.addProperty(Property(&quot;X-CALENDARSERVER-SPLIT-RID&quot;, rid))
</span><span class="cx">                         itipmsg.addProperty(Property(&quot;X-CALENDARSERVER-SPLIT-OLDER-UID&quot; if newer_piece else &quot;X-CALENDARSERVER-SPLIT-NEWER-UID&quot;, uid))
</span><span class="cx"> 
</span><span class="lines">@@ -1353,7 +1355,7 @@
</span><span class="cx">                 else:
</span><span class="cx">                     # Add split details if needed
</span><span class="cx">                     if self.split_details is not None:
</span><del>-                        rid, uid, newer_piece = self.split_details
</del><ins>+                        rid, uid, newer_piece, ignore = self.split_details
</ins><span class="cx">                         itipmsg.addProperty(Property(&quot;X-CALENDARSERVER-SPLIT-RID&quot;, rid))
</span><span class="cx">                         itipmsg.addProperty(Property(&quot;X-CALENDARSERVER-SPLIT-OLDER-UID&quot; if newer_piece else &quot;X-CALENDARSERVER-SPLIT-NEWER-UID&quot;, uid))
</span><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServerbranchesuserssagentrashcan3txdavcaldavdatastoresqlpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/sagen/trashcan-3/txdav/caldav/datastore/sql.py (14442 => 14443)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -3094,7 +3094,7 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @inlineCallbacks
</span><del>-    def doImplicitScheduling(self, component, inserting, internal_state, options, split_details=None):
</del><ins>+    def doImplicitScheduling(self, component, inserting, internal_state, options, split_details=None, updateSelf=False):
</ins><span class="cx"> 
</span><span class="cx">         new_component = None
</span><span class="cx">         did_implicit_action = False
</span><span class="lines">@@ -3119,9 +3119,12 @@
</span><span class="cx">             scheduler = ImplicitScheduler(logItems=self._txn.logItems, options=options)
</span><span class="cx"> 
</span><span class="cx">             # PUT
</span><ins>+            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
</ins><span class="cx">             do_implicit_action, is_scheduling_resource = (yield scheduler.testImplicitSchedulingPUT(
</span><span class="cx">                 self.calendar(),
</span><del>-                None if inserting else self,
</del><ins>+                resourceToUpdate,
</ins><span class="cx">                 component,
</span><span class="cx">                 internal_request=is_internal,
</span><span class="cx">             ))
</span><span class="lines">@@ -3281,7 +3284,7 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @inlineCallbacks
</span><del>-    def _setComponentInternal(self, component, inserting=False, internal_state=ComponentUpdateState.NORMAL, options=None, split_details=None):
</del><ins>+    def _setComponentInternal(self, component, inserting=False, internal_state=ComponentUpdateState.NORMAL, options=None, split_details=None, updateSelf=False):
</ins><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Setting the component internally to the store itself. This will bypass a whole bunch of data consistency checks
</span><span class="cx">         on the assumption that those have been done prior to the component data being provided, provided the flag is set.
</span><span class="lines">@@ -3348,7 +3351,7 @@
</span><span class="cx">                 yield self.decorateHostedStatus(component)
</span><span class="cx"> 
</span><span class="cx">             # Do scheduling
</span><del>-            implicit_result = (yield self.doImplicitScheduling(component, inserting, internal_state, options))
</del><ins>+            implicit_result = (yield self.doImplicitScheduling(component, inserting, internal_state, options, updateSelf=updateSelf))
</ins><span class="cx">             if isinstance(implicit_result, int):
</span><span class="cx">                 if implicit_result == ImplicitScheduler.STATUS_ORPHANED_CANCELLED_EVENT:
</span><span class="cx">                     raise ResourceDeletedError(&quot;Resource created but immediately deleted by the server.&quot;)
</span><span class="lines">@@ -3389,7 +3392,8 @@
</span><span class="cx">         elif self._txn._migrating:
</span><span class="cx">             self.validCalendarDataCheck(component, inserting)
</span><span class="cx"> 
</span><del>-        yield self.updateDatabase(component, inserting=inserting)
</del><ins>+        # If updateSelf is True, we want to turn inserting off within updateDatabase
+        yield self.updateDatabase(component, inserting=inserting if not updateSelf else False)
</ins><span class="cx"> 
</span><span class="cx">         # update GROUP_ATTENDEE table rows
</span><span class="cx">         if inserting:
</span><span class="lines">@@ -3936,8 +3940,20 @@
</span><span class="cx">             if self._dropboxID:
</span><span class="cx">                 yield DropBoxAttachment.resourceRemoved(self._txn, self._resourceID, self._dropboxID)
</span><span class="cx">             yield ManagedAttachment.resourceRemoved(self._txn, self._resourceID)
</span><del>-        yield super(CalendarObject, self).remove()
</del><span class="cx"> 
</span><ins>+        if isinbox: # bypass the trash
+            yield super(CalendarObject, self).reallyRemove()
+        else:
+            status = calendar.mainComponent().getProperty(&quot;STATUS&quot;)
+            if status is not None:
+                status = status.strvalue()
+                if status == &quot;CANCELLED&quot;:
+                    yield super(CalendarObject, self).reallyRemove()
+                else:
+                    yield super(CalendarObject, self).remove()
+            else:
+                yield super(CalendarObject, self).remove()
+
</ins><span class="cx">         # Do scheduling
</span><span class="cx">         if scheduler is not None:
</span><span class="cx">             # Cannot do implicit in sharee's shared calendar
</span><span class="lines">@@ -4690,7 +4706,7 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @inlineCallbacks
</span><del>-    def split(self, onlyThis=False, rid=None, olderUID=None):
</del><ins>+    def split(self, onlyThis=False, rid=None, olderUID=None, coercePartstatsInExistingResource=False, splitter=None):
</ins><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Split this and all matching UID calendar objects as per L{iCalSplitter}.
</span><span class="cx"> 
</span><span class="lines">@@ -4731,7 +4747,8 @@
</span><span class="cx">         else:
</span><span class="cx">             resources = (yield CalendarStoreFeatures(self._txn._store).calendarObjectsWithUID(self._txn, self._uid))
</span><span class="cx"> 
</span><del>-        splitter = iCalSplitter(config.Scheduling.Options.Splitting.Size, config.Scheduling.Options.Splitting.PastDays)
</del><ins>+        if splitter is None:
+            splitter = iCalSplitter(config.Scheduling.Options.Splitting.Size, config.Scheduling.Options.Splitting.PastDays)
</ins><span class="cx"> 
</span><span class="cx">         # Determine the recurrence-id of the split and create a new UID for it
</span><span class="cx">         calendar = (yield self.component())
</span><span class="lines">@@ -4757,25 +4774,29 @@
</span><span class="cx">             returnValue(None)
</span><span class="cx"> 
</span><span class="cx">         # Store changed data
</span><del>-        yield self._setComponentInternal(calendar_new, internal_state=ComponentUpdateState.SPLIT_OWNER, split_details=(rid, olderUID, True,))
</del><ins>+        yield self._setComponentInternal(calendar_new, internal_state=ComponentUpdateState.SPLIT_OWNER, split_details=(rid, olderUID, True, coercePartstatsInExistingResource))
</ins><span class="cx">         olderObject = yield self.calendar()._createCalendarObjectWithNameInternal(
</span><span class="cx">             olderResourceName,
</span><span class="cx">             calendar_old,
</span><span class="cx">             ComponentUpdateState.SPLIT_OWNER,
</span><del>-            split_details=(rid, newerUID, False,)
</del><ins>+            split_details=(rid, newerUID, False, False)
</ins><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx">         # Split each one - but not this resource
</span><span class="cx">         for resource in resources:
</span><span class="cx">             if resource._resourceID == self._resourceID:
</span><span class="cx">                 continue
</span><del>-            yield resource.splitForAttendee(rid, olderUID)
</del><ins>+            yield resource.splitForAttendee(
+                rid,
+                olderUID,
+                coercePartstatsInExistingResource=coercePartstatsInExistingResource
+            )
</ins><span class="cx"> 
</span><span class="cx">         returnValue(olderObject)
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @inlineCallbacks
</span><del>-    def splitForAttendee(self, rid=None, olderUID=None):
</del><ins>+    def splitForAttendee(self, rid=None, olderUID=None, coercePartstatsInExistingResource=False):
</ins><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Split this attendee resource as per L{split}. Note this is also used on any Organizer inbox items.
</span><span class="cx">         Also, for inbox items, we are not protected by the ImplicitUID lock - it is possible that the inbox
</span><span class="lines">@@ -4796,6 +4817,12 @@
</span><span class="cx"> 
</span><span class="cx">         # Store changed data
</span><span class="cx">         if ical_new.mainType() is not None:
</span><ins>+
+            if coercePartstatsInExistingResource:
+                for attendee in ical_new.getAllAttendeeProperties():
+                    if attendee.parameterValue(&quot;SCHEDULE-AGENT&quot;, &quot;SERVER&quot;).upper() == &quot;SERVER&quot; and attendee.hasParameter(&quot;PARTSTAT&quot;):
+                        attendee.setParameter(&quot;PARTSTAT&quot;, &quot;NEEDS-ACTION&quot;)
+
</ins><span class="cx">             yield self._setComponentInternal(ical_new, internal_state=ComponentUpdateState.SPLIT_ATTENDEE)
</span><span class="cx">         else:
</span><span class="cx">             # The split removed all components from this object - remove it
</span><span class="lines">@@ -4837,6 +4864,51 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    @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 &gt;= now:
+                    # future
+                    newdata = caldata.duplicate()
+                    newdata.bumpiTIPInfo(doSequence=True)
+                    for attendee in newdata.getAllAttendeeProperties():
+                        if attendee.parameterValue(&quot;SCHEDULE-AGENT&quot;, &quot;SERVER&quot;).upper() == &quot;SERVER&quot; and attendee.hasParameter(&quot;PARTSTAT&quot;):
+                            attendee.setParameter(&quot;PARTSTAT&quot;, &quot;NEEDS-ACTION&quot;)
+
+
+                    yield self._setComponentInternal(
+                        newdata, inserting=True, updateSelf=True
+                    )
+
+                else:
+                    # past
+                    yield ImplicitScheduler().refreshAllAttendeesExceptSome(
+                        self._txn,
+                        self,
+                    )
+
+
+
+
</ins><span class="cx"> class AttachmentStorageTransport(StorageTransportBase):
</span><span class="cx"> 
</span><span class="cx">     _TEMPORARY_UPLOADS_DIRECTORY = &quot;Temporary&quot;
</span><span class="lines">@@ -5763,6 +5835,9 @@
</span><span class="cx">     def nameForResource(self, collection, objectResource):
</span><span class="cx">         return &quot;{}-{}&quot;.format(collection._resourceID, objectResource.name())
</span><span class="cx"> 
</span><ins>+    def originalParentForResource(self, objectResource):
+        parentID, resourceName = self.parseName(objectResource._name)
+        return self._home.childWithID(parentID)
</ins><span class="cx"> 
</span><span class="cx">     def parseName(self, name):
</span><span class="cx">         parentID, resourceName = name.split(&quot;-&quot;, 1)
</span></span></pre></div>
<a id="CalendarServerbranchesuserssagentrashcan3txdavcommondatastoresqlpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/sagen/trashcan-3/txdav/common/datastore/sql.py (14442 => 14443)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -5700,6 +5700,10 @@
</span><span class="cx">         return False
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    def originalParentForResource(self, objectResource):
+        return succeed(objectResource._parentCollection)
+
+
</ins><span class="cx">     def memoMe(self, key, memo):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Add this object to the memo dictionary in whatever fashion is appropriate.
</span><span class="lines">@@ -7069,6 +7073,13 @@
</span><span class="cx"> 
</span><span class="cx">         if row:
</span><span class="cx">             child = yield cls.makeClass(parent, row)
</span><ins>+
+            # Use the name that was passed in, especially to cover the case
+            # where the &quot;trashed&quot; name is prepended with the original collection
+            # id (otherwise the name will be missing that id)
+            if name is not None:
+                child._name = name
+
</ins><span class="cx">             returnValue(child)
</span><span class="cx">         else:
</span><span class="cx">             returnValue(None)
</span><span class="lines">@@ -7400,8 +7411,9 @@
</span><span class="cx">             self._txn, isTrash=True, resourceID=self._resourceID
</span><span class="cx">         )
</span><span class="cx">         yield self._parentCollection.removedObjectResource(self)
</span><ins>+        yield self._parentCollection._deleteRevision(self.name())
+
</ins><span class="cx">         trash = yield self._parentCollection._home.childWithName(&quot;trash&quot;)
</span><del>-        print(&quot;TO TRASH&quot;, trash)
</del><span class="cx">         if trash is not None:
</span><span class="cx">             yield trash._insertRevision(
</span><span class="cx">                 trash.nameForResource(
</span><span class="lines">@@ -7413,21 +7425,24 @@
</span><span class="cx"> 
</span><span class="cx">     @inlineCallbacks
</span><span class="cx">     def fromTrash(self):
</span><ins>+        trash = self._parentCollection
+        self._parentCollection = yield trash.originalParentForResource(self)
</ins><span class="cx">         yield self._updateIsTrashQuery.on(
</span><span class="cx">             self._txn, isTrash=False, resourceID=self._resourceID
</span><span class="cx">         )
</span><del>-        yield self._parentCollection.addedObjectResource(self)
-        trash = yield self._parentCollection._home.childWithName(&quot;trash&quot;)
-        print(&quot;FROM TRASH&quot;, trash)
-        if trash is not None:
-            yield trash._deleteRevision(
-                trash.nameForResource(
-                    self._parentCollection,
-                    self
-                )
</del><ins>+        yield trash._deleteRevision(
+            trash.nameForResource(
+                self._parentCollection,
+                self
</ins><span class="cx">             )
</span><ins>+        )
+        _ignored, self._name = trash.parseName(self._name)
</ins><span class="cx"> 
</span><ins>+        yield self._parentCollection.addedObjectResource(self)
+        yield self._parentCollection._insertRevision(self.name())
</ins><span class="cx"> 
</span><ins>+
+
</ins><span class="cx">     @classproperty
</span><span class="cx">     def _selectIsTrashQuery(cls):
</span><span class="cx">         obj = cls._objectSchema
</span></span></pre></div>
<a id="CalendarServerbranchesuserssagentrashcan3txdavcommondatastoresql_schemacurrentsql"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/sagen/trashcan-3/txdav/common/datastore/sql_schema/current.sql (14442 => 14443)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -132,7 +132,8 @@
</span><span class="cx">   SUPPORTED_COMPONENTS  varchar(255) default null,
</span><span class="cx">   CREATED               timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
</span><span class="cx">   MODIFIED              timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
</span><del>-  CHILD_TYPE            varchar(10)  default null -- None, inbox, trash (FIXME: convert this to enumeration)
</del><ins>+  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
</ins><span class="cx"> 
</span><span class="cx"> );
</span><span class="cx"> 
</span><span class="lines">@@ -260,8 +261,7 @@
</span><span class="cx">   CREATED              timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
</span><span class="cx">   MODIFIED             timestamp    default timezone('UTC', CURRENT_TIMESTAMP),
</span><span class="cx">   DATAVERSION          integer      default 0 not null,
</span><del>-  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
</del><ins>+  IS_TRASH             boolean      default false not null, -- entire resource is in the trash
</ins><span class="cx"> 
</span><span class="cx">   unique (CALENDAR_RESOURCE_ID, RESOURCE_NAME) -- implicit index
</span><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServerbranchesuserssagentrashcan3txdavcommondatastoretesttest_sqlpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/sagen/trashcan-3/txdav/common/datastore/test/test_sql.py (14442 => 14443)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -20,24 +20,27 @@
</span><span class="cx"> 
</span><span class="cx"> from uuid import UUID
</span><span class="cx"> 
</span><ins>+from pycalendar.datetime import DateTime
</ins><span class="cx"> from twext.enterprise.dal.syntax import Insert
</span><span class="cx"> from twext.enterprise.dal.syntax import Select
</span><ins>+from twext.enterprise.jobqueue import JobItem
+from twisted.internet import reactor
</ins><span class="cx"> from twisted.internet.defer import Deferred
</span><span class="cx"> from twisted.internet.defer import inlineCallbacks, returnValue, succeed
</span><span class="cx"> from twisted.internet.task import Clock
</span><span class="cx"> from twisted.trial.unittest import TestCase
</span><ins>+from twistedcaldav.ical import Component
</ins><span class="cx"> from twistedcaldav.test.util import StoreTestCase
</span><del>-from txdav.common.datastore.sql import fixUUIDNormalization
</del><ins>+# from twistedcaldav.vcard import Component as VCard
</ins><span class="cx"> from txdav.common.datastore.sql import (
</span><span class="cx">     log, CommonStoreTransactionMonitor,
</span><span class="cx">     CommonHome, CommonHomeChild, ECALENDARTYPE
</span><span class="cx"> )
</span><ins>+from txdav.common.datastore.sql import fixUUIDNormalization
</ins><span class="cx"> from txdav.common.datastore.sql_tables import schema
</span><span class="cx"> from txdav.common.datastore.test.util import CommonCommonTests
</span><span class="cx"> from txdav.common.icommondatastore import AllRetriesFailed
</span><span class="cx"> from txdav.xml import element as davxml
</span><del>-from twistedcaldav.ical import Component
-from twistedcaldav.vcard import Component as VCard
</del><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -470,8 +473,83 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+class StubTransaction(object):
</ins><span class="cx"> 
</span><del>-VCALENDAR_DATA_NO_SCHEDULING = &quot;&quot;&quot;BEGIN:VCALENDAR
</del><ins>+    def __init__(self, label):
+        self.label = label
+        self.action = None
+
+
+    def commit(self):
+        self.action = &quot;committed&quot;
+        return succeed(None)
+
+
+    def abort(self):
+        self.action = &quot;aborted&quot;
+        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(&quot;\r\n &quot;, &quot;&quot;))
+
+
+    @inlineCallbacks
+    def test_trashUnscheduled(self):
+        &quot;&quot;&quot;
+        Verify the &quot;resource is entirely in the trash&quot; flag
+        &quot;&quot;&quot;
+
+        data1 = &quot;&quot;&quot;BEGIN:VCALENDAR
</ins><span class="cx"> VERSION:2.0
</span><span class="cx"> PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN
</span><span class="cx"> BEGIN:VEVENT
</span><span class="lines">@@ -499,26 +577,6 @@
</span><span class="cx"> END:VCALENDAR
</span><span class="cx"> &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-VCARD_DATA = &quot;&quot;&quot;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
-&quot;&quot;&quot;
-
-
-class CommonTrashTests(StoreTestCase):
-
-    @inlineCallbacks
-    def test_isTrash(self):
-        &quot;&quot;&quot;
-        Verify the &quot;resource is entirely in the trash&quot; flag
-        &quot;&quot;&quot;
-
</del><span class="cx">         txn = self.store.newTransaction()
</span><span class="cx"> 
</span><span class="cx">         #
</span><span class="lines">@@ -536,7 +594,7 @@
</span><span class="cx">         # Create an object
</span><span class="cx">         resource = yield collection.createObjectResourceWithName(
</span><span class="cx">             &quot;test.ics&quot;,
</span><del>-            Component.allFromString(VCALENDAR_DATA_NO_SCHEDULING)
</del><ins>+            Component.allFromString(data1)
</ins><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx">         # One object in collection
</span><span class="lines">@@ -562,9 +620,13 @@
</span><span class="cx"> 
</span><span class="cx">         # One object in trash
</span><span class="cx">         objects = yield trash.listObjectResources()
</span><ins>+        print(&quot;OBJECT LIST&quot;, objects)
</ins><span class="cx">         self.assertEquals(len(objects), 1)
</span><span class="cx"> 
</span><span class="cx">         # Put back from trash
</span><ins>+        print(&quot;FETCHING FROM TRASH USING&quot;, objects[0])
+        resource = yield self._getResource(txn, &quot;user01&quot;, &quot;trash&quot;, objects[0])
+        print(&quot;OBJECT NAME&quot;, resource._name)
</ins><span class="cx">         yield resource.fromTrash()
</span><span class="cx"> 
</span><span class="cx">         # Not in trash
</span><span class="lines">@@ -579,61 +641,545 @@
</span><span class="cx">         self.assertEquals(len(objects), 0)
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-        #
-        # Now with addressbook
-        #
</del><ins>+        # Not implemented
+#         #
+#         # Now with addressbook
+#         #
</ins><span class="cx"> 
</span><del>-        home = yield txn.addressbookHomeWithUID(&quot;user01&quot;, create=True)
-        collection = yield home.childWithName(&quot;addressbook&quot;)
</del><ins>+#         data2 = &quot;&quot;&quot;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
+# &quot;&quot;&quot;
</ins><span class="cx"> 
</span><del>-        # Create an object
-        resource = yield collection.createObjectResourceWithName(
-            &quot;test.vcf&quot;,
-            VCard.fromString(VCARD_DATA)
</del><ins>+#         print(&quot;ADDRESSBOOK TIME&quot;)
+
+#         home = yield txn.addressbookHomeWithUID(&quot;user01&quot;, create=True)
+#         collection = yield home.childWithName(&quot;addressbook&quot;)
+#         trash = yield home.childWithName(&quot;trash&quot;)
+
+#         # Create an object
+#         resource = yield collection.createObjectResourceWithName(
+#             &quot;test.vcf&quot;,
+#             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(&quot;OBJECT LIST&quot;, objects)
+#         self.assertEquals(len(objects), 1)
+
+#         # Put back from trash
+#         print(&quot;FETCHING FROM TRASH USING&quot;, objects[0])
+#         resource = yield self._getResource(txn, &quot;user01&quot;, &quot;trash&quot;, 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, &quot;EnableTrashCollection&quot;, 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 = {
+            &quot;start&quot;: start,
+            &quot;end&quot;: end,
+        }
+
+        data1 = &quot;&quot;&quot;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=&quot;User 01&quot;:mailto:user01@example.com
+ATTENDEE:mailto:user01@example.com
+ATTENDEE:mailto:user02@example.com
+END:VEVENT
+END:VCALENDAR
+&quot;&quot;&quot; % subs
+
+        data2 = &quot;&quot;&quot;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=&quot;User 01&quot;:mailto:user01@example.com
+ATTENDEE:mailto:user01@example.com
+ATTENDEE;PARTSTAT=ACCEPTED:mailto:user02@example.com
+END:VEVENT
+END:VCALENDAR
+&quot;&quot;&quot; % subs
+
+        # user01 invites user02
+        print(&quot;TEST: user01 creates resource START&quot;)
+        txn = self.store.newTransaction()
+        yield self._createResource(
+            txn, &quot;user01&quot;, &quot;calendar&quot;, &quot;test.ics&quot;, data1
</ins><span class="cx">         )
</span><ins>+        yield txn.commit()
</ins><span class="cx"> 
</span><del>-        # One object
-        objects = yield collection.listObjectResources()
-        self.assertEquals(len(objects), 1)
</del><ins>+        yield JobItem.waitEmpty(self.store.newTransaction, reactor, 60)
+        print(&quot;TEST: user01 creates resource END&quot;)
</ins><span class="cx"> 
</span><del>-        # Verify it's not in the trash
-        self.assertFalse((yield resource.isTrash()))
</del><ins>+        # user01's copy has SCHEDULE-STATUS update
+        txn = self.store.newTransaction()
+        data = yield self._getResourceData(txn, &quot;user01&quot;, &quot;calendar&quot;, &quot;test.ics&quot;)
+        self.assertTrue(&quot;SCHEDULE-STATUS=1.2&quot; in data)
</ins><span class="cx"> 
</span><del>-        # Move object to trash
-        yield resource.toTrash()
</del><ins>+        # user02 has an inbox item
+        resourceNames = yield self._getResourceNames(txn, &quot;user02&quot;, &quot;inbox&quot;)
+        self.assertEqual(len(resourceNames), 1)
</ins><span class="cx"> 
</span><del>-        # Verify it's in the trash
-        self.assertTrue((yield resource.isTrash()))
</del><ins>+        # user02 accepts
+        print(&quot;TEST: user02 accepts START&quot;)
+        yield self._updateResource(txn, &quot;user02&quot;, &quot;calendar&quot;, &quot;&quot;, data2)
+        yield txn.commit()
</ins><span class="cx"> 
</span><del>-        # No objects
-        objects = yield collection.listObjectResources()
-        self.assertEquals(len(objects), 0)
</del><ins>+        yield JobItem.waitEmpty(self.store.newTransaction, reactor, 60)
+        print(&quot;TEST: user02 accepts END&quot;)
</ins><span class="cx"> 
</span><del>-        # Put back from trash
</del><ins>+        # user01 has an inbox item
+        txn = self.store.newTransaction()
+        resourceNames = yield self._getResourceNames(txn, &quot;user01&quot;, &quot;inbox&quot;)
+        self.assertEqual(len(resourceNames), 1)
+
+        # user01's copy has SCHEDULE-STATUS update
+        data = yield self._getResourceData(txn, &quot;user01&quot;, &quot;calendar&quot;, &quot;test.ics&quot;)
+        self.assertTrue(&quot;SCHEDULE-STATUS=2.0&quot; in data)
+        self.assertTrue(&quot;PARTSTAT=ACCEPTED&quot; in data)
+        resource = yield self._getResource(txn, &quot;user02&quot;, &quot;inbox&quot;, &quot;&quot;)
+        yield resource.remove()
+
+        yield txn.commit()
+
+        # user01 trashes event
+        print(&quot;TEST: user01 trashes event START&quot;)
+        txn = self.store.newTransaction()
+        resource = yield self._getResource(txn, &quot;user01&quot;, &quot;calendar&quot;, &quot;test.ics&quot;)
+        yield resource.remove()
+        yield txn.commit()
+
+        yield JobItem.waitEmpty(self.store.newTransaction, reactor, 60)
+        print(&quot;TEST: user01 trashes event END&quot;)
+
+        # user01's copy is in the trash, still with user02 accepted
+        txn = self.store.newTransaction()
+        data = yield self._getResourceData(txn, &quot;user01&quot;, &quot;trash&quot;, &quot;&quot;)
+        self.assertTrue(&quot;PARTSTAT=ACCEPTED&quot; in data)
+        yield txn.commit()
+
+        # user02's copy is cancelled
+        txn = self.store.newTransaction()
+        data = yield self._getResourceData(txn, &quot;user02&quot;, &quot;inbox&quot;, &quot;&quot;)
+        self.assertTrue(&quot;METHOD:CANCEL&quot; in data)
+        resource = yield self._getResource(txn, &quot;user02&quot;, &quot;inbox&quot;, &quot;&quot;)
+        yield resource.remove()
+        data = yield self._getResourceData(txn, &quot;user02&quot;, &quot;calendar&quot;, &quot;&quot;)
+        self.assertTrue(&quot;STATUS:CANCELLED&quot; in data)
+        resource = yield self._getResource(txn, &quot;user02&quot;, &quot;calendar&quot;, &quot;&quot;)
+        print(&quot;PLUGH REMOVE user02's copy&quot;)
+        yield resource.remove()
+        print(&quot;PLUGH REMOVE user02's copy done&quot;)
+        data = yield self._getResource(txn, &quot;user02&quot;, &quot;trash&quot;, &quot;&quot;)
+        self.assertEquals(data, None)
+        yield txn.commit()
+
+        # user01 restores event from the trash
+        print(&quot;TEST: user01 restores event START&quot;)
+        txn = self.store.newTransaction()
+        resource = yield self._getResource(txn, &quot;user01&quot;, &quot;trash&quot;, &quot;&quot;)
+        data = yield self._getResourceData(txn, &quot;user01&quot;, &quot;trash&quot;, &quot;&quot;)
+        print(&quot;user01 trashed copy&quot;, data)
+        print(&quot;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-&quot;)
</ins><span class="cx">         yield resource.fromTrash()
</span><ins>+        yield txn.commit()
</ins><span class="cx"> 
</span><del>-        # Not in trash
-        self.assertFalse((yield resource.isTrash()))
</del><ins>+        yield JobItem.waitEmpty(self.store.newTransaction, reactor, 60)
+        print(&quot;TEST: user01 restores event END&quot;)
</ins><span class="cx"> 
</span><del>-        # One object
-        objects = yield collection.listObjectResources()
-        self.assertEquals(len(objects), 1)
</del><ins>+        txn = self.store.newTransaction()
</ins><span class="cx"> 
</span><ins>+        # user01's copy should be back on their calendar
+        names = yield self._getResourceNames(txn, &quot;user01&quot;, &quot;calendar&quot;)
+        print(&quot;user01 names&quot;, names)
+        data = yield self._getResourceData(txn, &quot;user01&quot;, &quot;calendar&quot;, &quot;&quot;)
+        print(&quot;WHAT?&quot;, data)
+        data = yield self._getResourceData(txn, &quot;user01&quot;, &quot;calendar&quot;, &quot;test.ics&quot;)
+        print(&quot;user01 copy&quot;)
+        print(data)
+
+        # user02's copy should be back on their calendar
+        data = yield self._getResourceData(txn, &quot;user02&quot;, &quot;calendar&quot;, &quot;&quot;)
+        print(&quot;user02 copy&quot;)
+        print(data)
+        self.assertTrue(&quot;PARTSTAT=NEEDS-ACTION&quot; in data)
+
</ins><span class="cx">         yield txn.commit()
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-class StubTransaction(object):
</del><span class="cx"> 
</span><del>-    def __init__(self, label):
-        self.label = label
-        self.action = None
</del><ins>+    @inlineCallbacks
+    def test_trashScheduledFullyInPast(self):
</ins><span class="cx"> 
</span><ins>+        from twistedcaldav.stdconfig import config
+        self.patch(config, &quot;EnableTrashCollection&quot;, True)
</ins><span class="cx"> 
</span><del>-    def commit(self):
-        self.action = &quot;committed&quot;
-        return succeed(None)
</del><ins>+        # 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 = {
+            &quot;start&quot;: start,
+            &quot;end&quot;: end,
+        }
</ins><span class="cx"> 
</span><ins>+        data1 = &quot;&quot;&quot;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=&quot;User 01&quot;:mailto:user01@example.com
+ATTENDEE:mailto:user01@example.com
+ATTENDEE:mailto:user02@example.com
+END:VEVENT
+END:VCALENDAR
+&quot;&quot;&quot; % subs
</ins><span class="cx"> 
</span><del>-    def abort(self):
-        self.action = &quot;aborted&quot;
-        return succeed(None)
</del><ins>+        data2 = &quot;&quot;&quot;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=&quot;User 01&quot;:mailto:user01@example.com
+ATTENDEE:mailto:user01@example.com
+ATTENDEE;PARTSTAT=TENTATIVE:mailto:user02@example.com
+END:VEVENT
+END:VCALENDAR
+&quot;&quot;&quot; % subs
+
+        # user01 invites user02
+        txn = self.store.newTransaction()
+        yield self._createResource(
+            txn, &quot;user01&quot;, &quot;calendar&quot;, &quot;test.ics&quot;, 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, &quot;user01&quot;, &quot;calendar&quot;, &quot;test.ics&quot;)
+        self.assertTrue(&quot;SCHEDULE-STATUS=1.2&quot; in data)
+
+        # user02 has an inbox item
+        resourceNames = yield self._getResourceNames(txn, &quot;user02&quot;, &quot;inbox&quot;)
+        self.assertEqual(len(resourceNames), 1)
+
+        # user02 accepts
+        yield self._updateResource(txn, &quot;user02&quot;, &quot;calendar&quot;, &quot;&quot;, 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, &quot;user01&quot;, &quot;inbox&quot;)
+        self.assertEqual(len(resourceNames), 1)
+
+        # user01's copy has SCHEDULE-STATUS update
+        data = yield self._getResourceData(txn, &quot;user01&quot;, &quot;calendar&quot;, &quot;test.ics&quot;)
+        self.assertTrue(&quot;SCHEDULE-STATUS=2.0&quot; in data)
+        self.assertTrue(&quot;PARTSTAT=TENTATIVE&quot; in data)
+        resource = yield self._getResource(txn, &quot;user02&quot;, &quot;inbox&quot;, &quot;&quot;)
+        yield resource.remove()
+
+        yield txn.commit()
+
+        # user01 trashes event
+        txn = self.store.newTransaction()
+        resource = yield self._getResource(txn, &quot;user01&quot;, &quot;calendar&quot;, &quot;test.ics&quot;)
+        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, &quot;user01&quot;, &quot;trash&quot;, &quot;&quot;)
+        self.assertTrue(&quot;PARTSTAT=TENTATIVE&quot; in data)
+        yield txn.commit()
+
+        # user02's copy is cancelled
+        txn = self.store.newTransaction()
+        data = yield self._getResourceData(txn, &quot;user02&quot;, &quot;inbox&quot;, &quot;&quot;)
+        self.assertTrue(&quot;METHOD:CANCEL&quot; in data)
+        resource = yield self._getResource(txn, &quot;user02&quot;, &quot;inbox&quot;, &quot;&quot;)
+        yield resource.remove()
+        data = yield self._getResourceData(txn, &quot;user02&quot;, &quot;calendar&quot;, &quot;&quot;)
+        self.assertTrue(&quot;STATUS:CANCELLED&quot; in data)
+        resource = yield self._getResource(txn, &quot;user02&quot;, &quot;calendar&quot;, &quot;&quot;)
+        print(&quot;PLUGH REMOVE user02's copy&quot;)
+        yield resource.remove()
+        print(&quot;PLUGH REMOVE user02's copy done&quot;)
+        data = yield self._getResource(txn, &quot;user02&quot;, &quot;trash&quot;, &quot;&quot;)
+        self.assertEquals(data, None)
+        yield txn.commit()
+
+        # user01 restores event from the trash
+        txn = self.store.newTransaction()
+        resource = yield self._getResource(txn, &quot;user01&quot;, &quot;trash&quot;, &quot;&quot;)
+        data = yield self._getResourceData(txn, &quot;user01&quot;, &quot;trash&quot;, &quot;&quot;)
+        print(&quot;user01 trashed copy&quot;, data)
+        print(&quot;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-&quot;)
+        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, &quot;user01&quot;, &quot;calendar&quot;, &quot;test.ics&quot;)
+        print(&quot;user01 copy&quot;)
+        print(data)
+
+        # user02's copy should be back on their calendar
+        data = yield self._getResourceData(txn, &quot;user02&quot;, &quot;calendar&quot;, &quot;&quot;)
+        print(&quot;user02 copy&quot;)
+        print(data)
+        self.assertTrue(&quot;PARTSTAT=TENTATIVE&quot; in data)
+
+
+        yield txn.commit()
+
+
+
+    @inlineCallbacks
+    def test_trashScheduledSpanningNow(self):
+
+        from twistedcaldav.stdconfig import config
+        self.patch(config, &quot;EnableTrashCollection&quot;, 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 = {
+            &quot;start&quot;: start,
+            &quot;end&quot;: end,
+        }
+
+        data1 = &quot;&quot;&quot;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=&quot;User 01&quot;:mailto:user01@example.com
+ATTENDEE:mailto:user01@example.com
+ATTENDEE:mailto:user02@example.com
+END:VEVENT
+END:VCALENDAR
+&quot;&quot;&quot; % subs
+
+        data2 = &quot;&quot;&quot;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=&quot;User 01&quot;:mailto:user01@example.com
+ATTENDEE:mailto:user01@example.com
+ATTENDEE;PARTSTAT=ACCEPTED:mailto:user02@example.com
+END:VEVENT
+END:VCALENDAR
+&quot;&quot;&quot; % subs
+
+        # user01 invites user02
+        txn = self.store.newTransaction()
+        yield self._createResource(
+            txn, &quot;user01&quot;, &quot;calendar&quot;, &quot;test.ics&quot;, 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, &quot;user01&quot;, &quot;calendar&quot;, &quot;test.ics&quot;)
+        self.assertTrue(&quot;SCHEDULE-STATUS=1.2&quot; in data)
+
+        # user02 has an inbox item
+        resourceNames = yield self._getResourceNames(txn, &quot;user02&quot;, &quot;inbox&quot;)
+        self.assertEqual(len(resourceNames), 1)
+
+        # user02 accepts
+        yield self._updateResource(txn, &quot;user02&quot;, &quot;calendar&quot;, &quot;&quot;, 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, &quot;user01&quot;, &quot;inbox&quot;)
+        self.assertEqual(len(resourceNames), 1)
+
+        # user01's copy has SCHEDULE-STATUS update
+        data = yield self._getResourceData(txn, &quot;user01&quot;, &quot;calendar&quot;, &quot;test.ics&quot;)
+        self.assertTrue(&quot;SCHEDULE-STATUS=2.0&quot; in data)
+        self.assertTrue(&quot;PARTSTAT=ACCEPTED&quot; in data)
+        resource = yield self._getResource(txn, &quot;user02&quot;, &quot;inbox&quot;, &quot;&quot;)
+        yield resource.remove()
+
+        yield txn.commit()
+
+        # user01 trashes event
+        txn = self.store.newTransaction()
+        resource = yield self._getResource(txn, &quot;user01&quot;, &quot;calendar&quot;, &quot;test.ics&quot;)
+        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, &quot;user01&quot;, &quot;trash&quot;, &quot;&quot;)
+        self.assertTrue(&quot;PARTSTAT=ACCEPTED&quot; in data)
+        yield txn.commit()
+
+        # user02's copy is cancelled
+        txn = self.store.newTransaction()
+        data = yield self._getResourceData(txn, &quot;user02&quot;, &quot;inbox&quot;, &quot;&quot;)
+        self.assertTrue(&quot;METHOD:CANCEL&quot; in data)
+        resource = yield self._getResource(txn, &quot;user02&quot;, &quot;inbox&quot;, &quot;&quot;)
+        yield resource.remove()
+        data = yield self._getResourceData(txn, &quot;user02&quot;, &quot;calendar&quot;, &quot;&quot;)
+        self.assertTrue(&quot;STATUS:CANCELLED&quot; in data)
+        resource = yield self._getResource(txn, &quot;user02&quot;, &quot;calendar&quot;, &quot;&quot;)
+        print(&quot;PLUGH REMOVE user02's copy&quot;)
+        yield resource.remove()
+        print(&quot;PLUGH REMOVE user02's copy done&quot;)
+        data = yield self._getResource(txn, &quot;user02&quot;, &quot;trash&quot;, &quot;&quot;)
+        self.assertEquals(data, None)
+        yield txn.commit()
+
+        # user01 restores event from the trash
+        txn = self.store.newTransaction()
+        resource = yield self._getResource(txn, &quot;user01&quot;, &quot;trash&quot;, &quot;&quot;)
+        data = yield self._getResourceData(txn, &quot;user01&quot;, &quot;trash&quot;, &quot;&quot;)
+        print(&quot;user01 trashed copy&quot;, data)
+        print(&quot;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-&quot;)
+        yield resource.fromTrash()
+        yield txn.commit()
+
+        yield JobItem.waitEmpty(self.store.newTransaction, reactor, 60)
+
+        txn = self.store.newTransaction()
+
+        # resourceNames = yield self._getResourceNames(txn, &quot;user01&quot;, &quot;calendar&quot;)
+        # print(&quot;user01's calendar&quot;, resourceNames)
+
+        # user01's trash should be empty
+        resourceNames = yield self._getResourceNames(txn, &quot;user01&quot;, &quot;trash&quot;)
+        self.assertEquals(len(resourceNames), 0)
+
+        # user01 should have test.ics and a new .ics
+        resourceNames = yield self._getResourceNames(txn, &quot;user01&quot;, &quot;calendar&quot;)
+        self.assertEquals(len(resourceNames), 2)
+        self.assertTrue(&quot;test.ics&quot; in resourceNames)
+        resourceNames.remove(&quot;test.ics&quot;)
+        newName = resourceNames[0]
+
+        # user01's test.ics -- verify it got split correctly
+        data = yield self._getResourceData(txn, &quot;user01&quot;, &quot;calendar&quot;, &quot;test.ics&quot;)
+        print(&quot;user01 test.ics&quot;)
+        print(data)
+
+        # user01's new .ics -- verify it got split correctly
+        data = yield self._getResourceData(txn, &quot;user01&quot;, &quot;calendar&quot;, newName)
+        print(&quot;user01 new ics&quot;)
+        print(data)
+
+        resourceNames = yield self._getResourceNames(txn, &quot;user02&quot;, &quot;calendar&quot;)
+        print(&quot;user02's calendar&quot;, resourceNames)
+        resourceNames = yield self._getResourceNames(txn, &quot;user02&quot;, &quot;trash&quot;)
+        print(&quot;user02's trash&quot;, resourceNames)
+
+        # # user02's copy should be back on their calendar
+        # data = yield self._getResourceData(txn, &quot;user02&quot;, &quot;calendar&quot;, &quot;&quot;)
+        # print(&quot;user02 copy&quot;)
+        # print(data)
+        # # self.assertTrue(&quot;PARTSTAT=NEEDS-ACTION&quot; in data)
+
+        yield txn.commit()
+
</ins></span></pre>
</div>
</div>

</body>
</html>