<!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>[3380] CalendarServer/trunk/twistedcaldav/method</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.macosforge.org/projects/calendarserver/changeset/3380">3380</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2008-11-13 13:31:21 -0800 (Thu, 13 Nov 2008)</dd>
</dl>

<h3>Log Message</h3>
<pre>Properly handle delete's of calendar collections (or nested calendar collections) wrt implicit scheduling.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunktwistedcaldavmethoddeletepy">CalendarServer/trunk/twistedcaldav/method/delete.py</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#CalendarServertrunktwistedcaldavmethoddelete_commonpy">CalendarServer/trunk/twistedcaldav/method/delete_common.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunktwistedcaldavmethoddeletepy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/method/delete.py (3379 => 3380)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/method/delete.py        2008-11-13 21:19:35 UTC (rev 3379)
+++ CalendarServer/trunk/twistedcaldav/method/delete.py        2008-11-13 21:31:21 UTC (rev 3380)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> ##
</span><del>-# Copyright (c) 2006-2007 Apple Inc. All rights reserved.
</del><ins>+# Copyright (c) 2006-2008 Apple Inc. All rights reserved.
</ins><span class="cx"> #
</span><span class="cx"> # Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
</span><span class="cx"> # you may not use this file except in compliance with the License.
</span><span class="lines">@@ -23,13 +23,10 @@
</span><span class="cx"> from twisted.internet.defer import inlineCallbacks, returnValue
</span><span class="cx"> from twisted.web2 import responsecode
</span><span class="cx"> from twisted.web2.dav import davxml
</span><del>-from twisted.web2.dav.fileop import delete
</del><span class="cx"> from twisted.web2.dav.util import parentForURL
</span><del>-from twisted.web2.http import HTTPError, StatusResponse
</del><ins>+from twisted.web2.http import HTTPError
</ins><span class="cx"> 
</span><del>-from twistedcaldav.memcachelock import MemcacheLock, MemcacheLockTimeoutError
-from twistedcaldav.resource import isCalendarCollectionResource
-from twistedcaldav.scheduling.implicit import ImplicitScheduler
</del><ins>+from twistedcaldav.method.delete_common import DeleteResource
</ins><span class="cx"> from twistedcaldav.log import Logger
</span><span class="cx"> 
</span><span class="cx"> log = Logger()
</span><span class="lines">@@ -41,9 +38,6 @@
</span><span class="cx">     # index file has the entry for the deleted calendar component removed.
</span><span class="cx">     #
</span><span class="cx"> 
</span><del>-    # TODO: need to use transaction based delete on live scheduling object resources
-    # as the iTIP operation may fail and may need to prevent the delete from happening.
-
</del><span class="cx">     if not self.fp.exists():
</span><span class="cx">         log.err(&quot;File not found: %s&quot; % (self.fp.path,))
</span><span class="cx">         raise HTTPError(responsecode.NOT_FOUND)
</span><span class="lines">@@ -58,67 +52,8 @@
</span><span class="cx"> 
</span><span class="cx">     yield parent.authorize(request, (davxml.Unbind(),))
</span><span class="cx"> 
</span><del>-    # Do quota checks before we start deleting things
-    myquota = (yield self.quota(request))
-    if myquota is not None:
-        old_size = (yield self.quotaSize(request))
-    else:
-        old_size = 0
</del><ins>+    # Do smart delete taking into account the need to do implicit CANCELs etc
+    deleter = DeleteResource(request, self, parent, depth)
+    response = (yield deleter.run())
</ins><span class="cx"> 
</span><del>-    scheduler = None
-    isCalendarCollection = False
-    isCalendarResource = False
-    lock = None
-
-    if self.exists():
-        if isCalendarCollectionResource(parent):
-            isCalendarResource = True
-            calendar = self.iCalendar()
-            scheduler = ImplicitScheduler()
-            do_implicit_action, _ignore = (yield scheduler.testImplicitSchedulingDELETE(request, self, calendar))
-            if do_implicit_action:
-                lock = MemcacheLock(&quot;ImplicitUIDLock&quot;, calendar.resourceUID(), timeout=60.0)
-            else:
-                scheduler = None
-            
-        elif isCalendarCollectionResource(self):
-            isCalendarCollection = True
-
-    try:
-        if lock:
-            yield lock.acquire()
-
-        # Do delete
-        response = (yield delete(request.uri, self.fp, depth))
-    
-
-        # Adjust quota
-        if myquota is not None:
-            yield self.quotaSizeAdjust(request, -old_size)
-
-        if response == responsecode.NO_CONTENT:
-            if isCalendarResource:
-    
-                index = parent.index()
-                index.deleteResource(self.fp.basename())
-    
-                # Change CTag on the parent calendar collection
-                yield parent.updateCTag()
-    
-                # Do scheduling
-                if scheduler:
-                    yield scheduler.doImplicitScheduling()
-     
-            elif isCalendarCollection:
-                
-                # Do some clean up
-                yield self.deletedCalendar(request)
-
-    except MemcacheLockTimeoutError:
-        raise HTTPError(StatusResponse(responsecode.CONFLICT, &quot;Resource: %s currently in use on the server.&quot; % (self.uri,)))
-
-    finally:
-        if lock:
-            yield lock.clean()
-
</del><span class="cx">     returnValue(response)
</span></span></pre></div>
<a id="CalendarServertrunktwistedcaldavmethoddelete_commonpyfromrev3377CalendarServerbranchesuserscdabooimplicitdelete3366twistedcaldavmethoddelete_commonpy"></a>
<div class="copfile"><h4>Copied: CalendarServer/trunk/twistedcaldav/method/delete_common.py (from rev 3377, CalendarServer/branches/users/cdaboo/implicit-delete-3366/twistedcaldav/method/delete_common.py) (0 => 3380)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/method/delete_common.py                                (rev 0)
+++ CalendarServer/trunk/twistedcaldav/method/delete_common.py        2008-11-13 21:31:21 UTC (rev 3380)
</span><span class="lines">@@ -0,0 +1,251 @@
</span><ins>+##
+# Copyright (c) 2006-2008 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+from twistedcaldav.method.report_common import applyToCalendarCollections
+
+
+&quot;&quot;&quot;
+CalDAV DELETE behaviors.
+&quot;&quot;&quot;
+
+__all__ = [&quot;DeleteResource&quot;]
+
+from twisted.internet.defer import inlineCallbacks, returnValue
+from twisted.web2 import responsecode
+from twisted.web2.dav.fileop import delete
+from twisted.web2.dav.http import ResponseQueue, MultiStatusResponse
+from twisted.web2.dav.util import joinURL
+from twisted.web2.http import HTTPError, StatusResponse
+
+from twistedcaldav.log import Logger
+from twistedcaldav.memcachelock import MemcacheLock, MemcacheLockTimeoutError
+from twistedcaldav.resource import isCalendarCollectionResource,\
+    isPseudoCalendarCollectionResource
+from twistedcaldav.scheduling.implicit import ImplicitScheduler
+
+log = Logger()
+
+class DeleteResource(object):
+    
+    def __init__(self, request, resource, parent, depth):
+        
+        self.request = request
+        self.resource = resource
+        self.parent = parent
+        self.depth = depth
+
+    @inlineCallbacks
+    def deleteResource(self, delresource, deluri, parent):
+        &quot;&quot;&quot;
+        Delete a plain resource which may be a collection - but only one not containing
+        calendar resources.
+
+        @param delresource:
+        @type delresource:
+        @param deluri:
+        @type deluri:
+        @param parent:
+        @type parent:
+        &quot;&quot;&quot;
+
+        # Do quota checks before we start deleting things
+        myquota = (yield delresource.quota(self.request))
+        if myquota is not None:
+            old_size = (yield delresource.quotaSize(self.request))
+        else:
+            old_size = 0
+        
+        # Do delete
+        response = (yield delete(deluri, delresource.fp, self.depth))
+
+        # Adjust quota
+        if myquota is not None:
+            yield delresource.quotaSizeAdjust(self.request, -old_size)
+
+        if response == responsecode.NO_CONTENT:
+            if isPseudoCalendarCollectionResource(parent):
+                index = parent.index()
+                index.deleteResource(delresource.fp.basename())
+
+                # Change CTag on the parent calendar collection
+                yield parent.updateCTag()
+                
+        returnValue(response)
+
+    @inlineCallbacks
+    def deleteCalendarResource(self, delresource, deluri, parent):
+        &quot;&quot;&quot;
+        Delete a single calendar resource and do implicit scheduling actions if required.
+
+        @param delresource:
+        @type delresource:
+        @param deluri:
+        @type deluri:
+        @param parent:
+        @type parent:
+        &quot;&quot;&quot;
+
+        # TODO: need to use transaction based delete on live scheduling object resources
+        # as the iTIP operation may fail and may need to prevent the delete from happening.
+    
+        # Do quota checks before we start deleting things
+        myquota = (yield delresource.quota(self.request))
+        if myquota is not None:
+            old_size = (yield delresource.quotaSize(self.request))
+        else:
+            old_size = 0
+        
+        # Get data we need for implicit scheduling
+        calendar = delresource.iCalendar()
+        scheduler = ImplicitScheduler()
+        do_implicit_action, _ignore = (yield scheduler.testImplicitSchedulingDELETE(self.request, delresource, calendar))
+        if do_implicit_action:
+            lock = MemcacheLock(&quot;ImplicitUIDLock&quot;, calendar.resourceUID(), timeout=60.0)
+        else:
+            scheduler = None
+            lock = None
+
+        try:
+            if lock:
+                yield lock.acquire()
+    
+            # Do delete
+            response = (yield delete(deluri, delresource.fp, self.depth))
+
+            # Adjust quota
+            if myquota is not None:
+                yield delresource.quotaSizeAdjust(self.request, -old_size)
+    
+            if response == responsecode.NO_CONTENT:
+                index = parent.index()
+                index.deleteResource(delresource.fp.basename())
+    
+                # Change CTag on the parent calendar collection
+                yield parent.updateCTag()
+    
+                # Do scheduling
+                if scheduler:
+                    yield scheduler.doImplicitScheduling()
+    
+        except MemcacheLockTimeoutError:
+            raise HTTPError(StatusResponse(responsecode.CONFLICT, &quot;Resource: %s currently in use on the server.&quot; % (deluri,)))
+    
+        finally:
+            if lock:
+                yield lock.clean()
+                
+        returnValue(response)
+
+    @inlineCallbacks
+    def deleteCalendar(self, delresource, deluri, parent):
+        &quot;&quot;&quot;
+        Delete an entire calendar collection by deleting each child resource in turn to
+        ensure that proper implicit scheduling actions occur.
+        
+        This has to emulate the behavior in fileop.delete in that any errors need to be
+        reported back in a multistatus response.
+        &quot;&quot;&quot;
+
+        if self.depth != &quot;infinity&quot;:
+            msg = &quot;Client sent illegal depth header value for DELETE: %s&quot; % (self.depth,)
+            log.err(msg)
+            raise HTTPError(StatusResponse(responsecode.BAD_REQUEST, msg))
+
+        log.debug(&quot;Deleting calendar %s&quot; % (delresource.fp.path,))
+
+        errors = ResponseQueue(deluri, &quot;DELETE&quot;, responsecode.NO_CONTENT)
+
+        for childname in delresource.listChildren():
+
+            childurl = joinURL(deluri, childname)
+            child = (yield self.request.locateChildResource(delresource, childname))
+
+            try:
+                yield self.deleteCalendarResource(child, childurl, delresource)
+            except:
+                errors.add(childurl, responsecode.BAD_REQUEST)
+
+        # Now do normal delete
+        more_responses = (yield self.deleteResource(delresource, deluri, parent))
+        
+        if isinstance(more_responses, MultiStatusResponse):
+            # Merge errors
+            errors.responses.update(more_responses.children)                
+
+        response = errors.response()
+        
+        if response == responsecode.NO_CONTENT:
+            # Do some clean up
+            yield delresource.deletedCalendar(self.request)
+
+        returnValue(response)
+
+    @inlineCallbacks
+    def deleteCollection(self):
+        &quot;&quot;&quot;
+        Delete a regular collection with special processing for any calendar collections
+        contained within it.
+        &quot;&quot;&quot;
+        if self.depth != &quot;infinity&quot;:
+            msg = &quot;Client sent illegal depth header value for DELETE: %s&quot; % (self.depth,)
+            log.err(msg)
+            raise HTTPError(StatusResponse(responsecode.BAD_REQUEST, msg))
+
+        log.debug(&quot;Deleting collection %s&quot; % (self.resource.fp.path,))
+
+        errors = ResponseQueue(self.request.uri, &quot;DELETE&quot;, responsecode.NO_CONTENT)

+        @inlineCallbacks
+        def doDeleteCalendar(delresource, deluri):
+            
+            delparent = (yield delresource.locateParent(self.request, deluri))
+
+            response = (yield self.deleteCalendar(delresource, deluri, delparent))
+
+            if isinstance(response, MultiStatusResponse):
+                # Merge errors
+                errors.responses.update(response.children)                
+
+            returnValue(True)
+
+        yield applyToCalendarCollections(self.resource, self.request, self.request.uri, self.depth, doDeleteCalendar, None)
+
+        # Now do normal delete
+        more_responses = (yield self.deleteResource(self.resource, self.request.uri, self.parent))
+        
+        if isinstance(more_responses, MultiStatusResponse):
+            # Merge errors
+            errors.responses.update(more_responses.children)                
+
+        response = errors.response()
+
+        returnValue(response)
+        
+    @inlineCallbacks
+    def run(self):
+
+        if isCalendarCollectionResource(self.parent):
+            response = (yield self.deleteCalendarResource(self.resource, self.request.uri, self.parent))
+            
+        elif isCalendarCollectionResource(self.resource):
+            response = (yield self.deleteCalendar(self.resource, self.request.uri, self.parent))
+        
+        elif self.resource.isCollection():
+            response = (yield self.deleteCollection())
+
+        else:
+            response = (yield self.deleteResource(self.resource, self.request.uri, self.parent))
+
+        returnValue(response)
</ins></span></pre>
</div>
</div>

</body>
</html>