<!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>[14150] CalendarServer/trunk/calendarserver/tools</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/14150">14150</a></dd>
<dt>Author</dt> <dd>sagen@apple.com</dd>
<dt>Date</dt> <dd>2014-11-10 11:44:51 -0800 (Mon, 10 Nov 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Adding test for import of events to attendee calendar (not working yet)</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunkcalendarservertoolsimporterpy">CalendarServer/trunk/calendarserver/tools/importer.py</a></li>
<li><a href="#CalendarServertrunkcalendarservertoolstesttest_importerpy">CalendarServer/trunk/calendarserver/tools/test/test_importer.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunkcalendarservertoolsimporterpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tools/importer.py (14149 => 14150)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tools/importer.py        2014-11-09 19:58:30 UTC (rev 14149)
+++ CalendarServer/trunk/calendarserver/tools/importer.py        2014-11-10 19:44:51 UTC (rev 14150)
</span><span class="lines">@@ -178,8 +178,8 @@
</span><span class="cx">     ownerUID, collectionResourceName = sourceURI.strip(&quot;/&quot;).split(&quot;/&quot;)[-2:]
</span><span class="cx"> 
</span><span class="cx">     dir = store.directoryService()
</span><del>-    record = yield dir.recordWithUID(ownerUID)
-    if not record:
</del><ins>+    ownerRecord = yield dir.recordWithUID(ownerUID)
+    if not ownerRecord:
</ins><span class="cx">         raise ImportException(&quot;{} is not in the directory&quot;.format(ownerUID))
</span><span class="cx"> 
</span><span class="cx">     # Set properties on the collection
</span><span class="lines">@@ -201,29 +201,81 @@
</span><span class="cx">     # want to batch them?
</span><span class="cx">     groupedComponents = Component.componentsFromComponent(component)
</span><span class="cx">     for groupedComponent in groupedComponents:
</span><del>-        resourceName = &quot;{}.ics&quot;.format(str(uuid.uuid4()))
-        try:
-            yield storeComponentInHomeAndCalendar(
-                store, groupedComponent, ownerUID, collectionResourceName, resourceName
-            )
-        except UIDExistsError:
-            # That event is already in the home
</del><ins>+
+        # If event is unscheduled or the organizer matches homeUID, store the
+        # component
+
+        storeDirectly = True
+        organizer = groupedComponent.getOrganizer()
+        if organizer is not None:
+            organizerRecord = yield dir.recordWithCalendarUserAddress(organizer)
+            if organizerRecord is None:
+                # Organizer does not exist, so skip this event
+                continue
+            else:
+                if ownerRecord.uid != organizerRecord.uid:
+                    # Owner is not the organizer
+                    storeDirectly = False
+
+        if storeDirectly:
+            resourceName = &quot;{}.ics&quot;.format(str(uuid.uuid4()))
</ins><span class="cx">             try:
</span><del>-                uid = list(groupedComponent.subcomponents())[0].propertyValue(&quot;UID&quot;)
-            except:
-                uid = &quot;unknown&quot;
</del><ins>+                yield storeComponentInHomeAndCalendar(
+                    store, groupedComponent, ownerUID, collectionResourceName, resourceName
+                )
+            except UIDExistsError:
+                # That event is already in the home
+                try:
+                    uid = list(groupedComponent.subcomponents())[0].propertyValue(&quot;UID&quot;)
+                except:
+                    uid = &quot;unknown&quot;
</ins><span class="cx"> 
</span><del>-            print(&quot;Skipping since UID already exists: {}&quot;.format(uid))
</del><ins>+                print(&quot;Skipping since UID already exists: {}&quot;.format(uid))
</ins><span class="cx"> 
</span><del>-        except Exception, e:
-            print(
-                &quot;Failed to import due to: {error}\n{comp}&quot;.format(
-                    error=e,
-                    comp=groupedComponent
</del><ins>+            except Exception, e:
+                print(
+                    &quot;Failed to import due to: {error}\n{comp}&quot;.format(
+                        error=e,
+                        comp=groupedComponent
+                    )
</ins><span class="cx">                 )
</span><del>-            )
</del><span class="cx"> 
</span><ins>+        else:
+            # Owner is not the organizer
+            print(&quot;OTHER&quot;)
+            txn = store.newTransaction()
+            organizerHome = yield txn.calendarHomeWithUID(organizerRecord.uid)
+            if organizerHome is None:
+                continue
+            # Iterate owner's calendars to find the one containing the event
+            # UID
+            uid = list(groupedComponent.subcomponents())[0].propertyValue(&quot;UID&quot;)
+            for collection in (yield organizerHome.children()):
+                if collection.name() != &quot;inbox&quot;:
+                    resourceName = yield collection.resourceNameForUID(uid)
+                    print(&quot;Resource name&quot;, collection, resourceName)
+                    object = yield collection.objectResourceWithName(resourceName)
+                    component = yield object.component()
+                    print (&quot;Comp&quot;, component)
</ins><span class="cx"> 
</span><ins>+                    ownerCUA = ownerRecord.canonicalCalendarUserAddress()
+                    print(&quot;CUA&quot;, ownerCUA)
+                    attendeeProp = component.getAttendeeProperty((ownerCUA,))
+                    print(&quot;att prop&quot;, attendeeProp)
+                    if attendeeProp is not None:
+                        print(&quot;Before&quot;, attendeeProp)
+                        attendeeProp.setParameter(&quot;PARTSTAT&quot;, &quot;NEEDS-ACTION&quot;)
+                        attendeeProp.removeParameter(&quot;SCHEDULE-STATUS&quot;)
+                        print(&quot;I modified&quot;, attendeeProp)
+                        result = yield object.setComponent(component)
+                        print(&quot;Set component result&quot;, result)
+
+                    break
+
+            yield txn.commit()
+
+
+
</ins><span class="cx"> @inlineCallbacks
</span><span class="cx"> def storeComponentInHomeAndCalendar(
</span><span class="cx">     store, component, homeUID, collectionResourceName, objectResourceName
</span></span></pre></div>
<a id="CalendarServertrunkcalendarservertoolstesttest_importerpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tools/test/test_importer.py (14149 => 14150)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tools/test/test_importer.py        2014-11-09 19:58:30 UTC (rev 14149)
+++ CalendarServer/trunk/calendarserver/tools/test/test_importer.py        2014-11-10 19:44:51 UTC (rev 14150)
</span><span class="lines">@@ -18,7 +18,10 @@
</span><span class="cx"> Unit tests for L{calendarsever.tools.importer}.
</span><span class="cx"> &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-from calendarserver.tools.importer import importCollectionComponent, ImportException
</del><ins>+from calendarserver.tools.importer import (
+    importCollectionComponent, ImportException,
+    storeComponentInHomeAndCalendar
+)
</ins><span class="cx"> from twext.enterprise.jobqueue import JobItem
</span><span class="cx"> from twisted.internet import reactor
</span><span class="cx"> from twisted.internet.defer import inlineCallbacks
</span><span class="lines">@@ -145,6 +148,54 @@
</span><span class="cx"> &quot;&quot;&quot;
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+DATA_OTHER_ORGANIZER_EVENT = &quot;&quot;&quot;BEGIN:VCALENDAR
+VERSION:2.0
+PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN
+BEGIN:VEVENT
+UID:6DB84FB1-C943-4144-BE65-9B0DD9A9E2C7
+DTSTART;TZID=America/Los_Angeles:20151008T053000
+DTEND;TZID=America/Los_Angeles:20151008T070000
+ATTENDEE;CN=User 01;CUTYPE=INDIVIDUAL:urn:x-uid:user01
+ATTENDEE;CN=User 02;CUTYPE=INDIVIDUAL;ROLE=CHAIR:urn:x-uid:user02
+ATTENDEE;CN=User 03;CUTYPE=INDIVIDUAL:urn:x-uid:user03
+ATTENDEE;CN=Mercury Seven;CUTYPE=ROOM:urn:x-uid:mercury
+CREATED:20141107T172645Z
+DTSTAMP:20141107T172645Z
+LOCATION:Mercury
+ORGANIZER;CN=User 02:urn:x-uid:user02
+SEQUENCE:0
+SUMMARY:Other organizer
+END:VEVENT
+END:VCALENDAR
+&quot;&quot;&quot;
+# TRANSP:OPAQUE
+
+DATA_ATTENDEE_EVENT = &quot;&quot;&quot;BEGIN:VCALENDAR
+VERSION:2.0
+NAME:I'm an attendee
+COLOR:#0000FFFF
+SOURCE;VALUE=URI:http://example.com/calendars/__uids__/user01/calendar/
+PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN
+BEGIN:VEVENT
+UID:6DB84FB1-C943-4144-BE65-9B0DD9A9E2C7
+DTSTART;TZID=America/Los_Angeles:20151008T053000
+DTEND;TZID=America/Los_Angeles:20151008T070000
+ATTENDEE;CN=User 01;CUTYPE=INDIVIDUAL:urn:x-uid:user01
+ATTENDEE;CN=User 02;CUTYPE=INDIVIDUAL;ROLE=CHAIR:urn:x-uid:user02
+ATTENDEE;CN=User 03;CUTYPE=INDIVIDUAL:urn:x-uid:user03
+ATTENDEE;CN=Mercury Seven;CUTYPE=ROOM:urn:x-uid:mercury
+CREATED:20141107T172645Z
+DTSTAMP:20141107T172645Z
+LOCATION:Mercury
+ORGANIZER;CN=User 02:urn:x-uid:user02
+SEQUENCE:0
+SUMMARY:Other organizer
+TRANSP:OPAQUE
+END:VEVENT
+END:VCALENDAR
+&quot;&quot;&quot;
+
+
</ins><span class="cx"> class ImportTests(StoreTestCase):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     Tests for importing data to a live store.
</span><span class="lines">@@ -277,3 +328,74 @@
</span><span class="cx">         self.assertEquals(len(objects), 1)
</span><span class="cx"> 
</span><span class="cx">         yield txn.commit()
</span><ins>+
+
+    @inlineCallbacks
+    def test_ImportComponentAttendee(self):
+
+        # Have another principal invite this principal
+
+        yield storeComponentInHomeAndCalendar(
+            self.store,
+            Component.allFromString(DATA_OTHER_ORGANIZER_EVENT),
+            &quot;user02&quot;,
+            &quot;calendar&quot;,
+            &quot;invite.ics&quot;
+        )
+
+        yield JobItem.waitEmpty(self.store.newTransaction, reactor, 60)
+
+        # Delete the attendee's copy, thus declining the event
+        txn = self.store.newTransaction()
+        home = yield txn.calendarHomeWithUID(&quot;user01&quot;)
+        collection = yield home.childWithName(&quot;calendar&quot;)
+        objects = yield collection.objectResources()
+        self.assertEquals(len(objects), 1)
+        yield objects[0].remove()
+        yield txn.commit()
+        yield JobItem.waitEmpty(self.store.newTransaction, reactor, 60)
+
+        # Make sure attendee's copy is gone
+        txn = self.store.newTransaction()
+        home = yield txn.calendarHomeWithUID(&quot;user01&quot;)
+        collection = yield home.childWithName(&quot;calendar&quot;)
+        objects = yield collection.objectResources()
+        self.assertEquals(len(objects), 0)
+        yield txn.commit()
+
+        # Make sure attendee shows as declined to the organizer
+        txn = self.store.newTransaction()
+        home = yield txn.calendarHomeWithUID(&quot;user02&quot;)
+        collection = yield home.childWithName(&quot;calendar&quot;)
+        objects = yield collection.objectResources()
+        self.assertEquals(len(objects), 1)
+        component = yield objects[0].component()
+        prop = component.getAttendeeProperty((&quot;urn:x-uid:user01&quot;,))
+        self.assertEquals(
+            prop.parameterValue(&quot;PARTSTAT&quot;),
+            &quot;DECLINED&quot;
+        )
+        yield txn.commit()
+
+        # When importing the event again, instead trigger a re-invite
+        # from the organizer
+        component = Component.allFromString(DATA_ATTENDEE_EVENT)
+        yield importCollectionComponent(self.store, component)
+
+        yield JobItem.waitEmpty(self.store.newTransaction, reactor, 60)
+
+        # Make sure attendee now has a new invite
+        txn = self.store.newTransaction()
+        home = yield txn.calendarHomeWithUID(&quot;user01&quot;)
+        collection = yield home.childWithName(&quot;calendar&quot;)
+        objects = yield collection.objectResources()
+        self.assertEquals(len(objects), 1)
+        component = yield objects[0].component()
+        prop = component.getAttendeeProperty((&quot;urn:x-uid:user01&quot;,))
+        self.assertEquals(
+            prop.parameterValue(&quot;PARTSTAT&quot;),
+            &quot;NEEDS-ACTION&quot;
+        )
+        yield txn.commit()
+
+    test_ImportComponentAttendee.todo = &quot;Debug&quot;
</ins></span></pre>
</div>
</div>

</body>
</html>