<!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>[14423] CalendarServer/branches/users/cdaboo/pod2pod-migration</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/14423">14423</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2015-02-17 09:51:13 -0800 (Tue, 17 Feb 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Checkpoint: migration final sync delegates.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServerbranchesuserscdaboopod2podmigrationrequirementsstabletxt">CalendarServer/branches/users/cdaboo/pod2pod-migration/requirements-stable.txt</a></li>
<li><a href="#CalendarServerbranchesuserscdaboopod2podmigrationtxdavcommondatastorepoddingdirectorypy">CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/podding/directory.py</a></li>
<li><a href="#CalendarServerbranchesuserscdaboopod2podmigrationtxdavcommondatastorepoddingmigrationhome_syncpy">CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/podding/migration/home_sync.py</a></li>
<li><a href="#CalendarServerbranchesuserscdaboopod2podmigrationtxdavcommondatastorepoddingmigrationtesttest_home_syncpy">CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/podding/migration/test/test_home_sync.py</a></li>
<li><a href="#CalendarServerbranchesuserscdaboopod2podmigrationtxdavcommondatastoresql_directorypy">CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/sql_directory.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServerbranchesuserscdaboopod2podmigrationrequirementsstabletxt"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/cdaboo/pod2pod-migration/requirements-stable.txt (14422 => 14423)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/cdaboo/pod2pod-migration/requirements-stable.txt        2015-02-17 17:47:33 UTC (rev 14422)
+++ CalendarServer/branches/users/cdaboo/pod2pod-migration/requirements-stable.txt        2015-02-17 17:51:13 UTC (rev 14423)
</span><span class="lines">@@ -36,7 +36,7 @@
</span><span class="cx">             #pyOpenSSL
</span><span class="cx">         pycrypto==2.6.1
</span><span class="cx"> 
</span><del>-    --editable svn+http://svn.calendarserver.org/repository/calendarserver/twext/branches/users/cdaboo/pod2pod-migration@14416#egg=twextpy
</del><ins>+    --editable svn+http://svn.calendarserver.org/repository/calendarserver/twext/branches/users/cdaboo/pod2pod-migration@14422#egg=twextpy
</ins><span class="cx">         cffi==0.8.6
</span><span class="cx">             pycparser==2.10
</span><span class="cx">         #twisted
</span></span></pre></div>
<a id="CalendarServerbranchesuserscdaboopod2podmigrationtxdavcommondatastorepoddingdirectorypy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/podding/directory.py (14422 => 14423)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/podding/directory.py        2015-02-17 17:47:33 UTC (rev 14422)
+++ CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/podding/directory.py        2015-02-17 17:51:13 UTC (rev 14423)
</span><span class="lines">@@ -191,3 +191,126 @@
</span><span class="cx">         delegators = yield Delegates._delegatedToUIDs(txn, delegate, request[&quot;read-write&quot;], onlyThisServer=True)
</span><span class="cx"> 
</span><span class="cx">         returnValue(list(delegators))
</span><ins>+
+
+    @inlineCallbacks
+    def send_dump_individual_delegates(self, txn, delegator):
+        &quot;&quot;&quot;
+        Get L{DelegateRecords} from another pod.
+
+        @param txn: transaction to use
+        @type txn: L{CommonStoreTransaction}
+        @param delegator: delegate to lookup
+        @type delegator: L{DirectoryRecord}
+        @param readWrite: if True, read and write access delegates are returned;
+            read-only access otherwise
+        &quot;&quot;&quot;
+        if delegator.thisServer():
+            raise FailedCrossPodRequestError(&quot;Cross-pod destination on this server: {}&quot;.format(delegator.uid))
+
+        request = {
+            &quot;action&quot;: &quot;dump-individual-delegates&quot;,
+            &quot;uid&quot;: delegator.uid,
+        }
+        response = yield self.sendRequestToServer(txn, delegator.server(), request)
+        returnValue(response)
+
+
+    @inlineCallbacks
+    def recv_dump_individual_delegates(self, txn, request):
+        &quot;&quot;&quot;
+        Process an delegators cross-pod request. Request arguments as per L{send_dump_individual_delegates}.
+
+        @param request: request arguments
+        @type request: C{dict}
+        &quot;&quot;&quot;
+
+        delegator = yield txn.directoryService().recordWithUID(request[&quot;uid&quot;])
+        if delegator is None or not delegator.thisServer():
+            raise FailedCrossPodRequestError(&quot;Cross-pod delegate missing or on this server: {}&quot;.format(delegator.uid))
+
+        delegates = yield txn.dumpIndividualDelegatesLocal(delegator.uid)
+
+        returnValue(self._to_serialize_list(delegates))
+
+
+    @inlineCallbacks
+    def send_dump_group_delegates(self, txn, delegator):
+        &quot;&quot;&quot;
+        Get L{DelegateGroupsRecord},L{GroupsRecord} from another pod.
+
+        @param txn: transaction to use
+        @type txn: L{CommonStoreTransaction}
+        @param delegator: delegate to lookup
+        @type delegator: L{DirectoryRecord}
+        @param readWrite: if True, read and write access delegates are returned;
+            read-only access otherwise
+        &quot;&quot;&quot;
+        if delegator.thisServer():
+            raise FailedCrossPodRequestError(&quot;Cross-pod destination on this server: {}&quot;.format(delegator.uid))
+
+        request = {
+            &quot;action&quot;: &quot;dump-group-delegates&quot;,
+            &quot;uid&quot;: delegator.uid,
+        }
+        response = yield self.sendRequestToServer(txn, delegator.server(), request)
+        returnValue(response)
+
+
+    @inlineCallbacks
+    def recv_dump_group_delegates(self, txn, request):
+        &quot;&quot;&quot;
+        Process an delegators cross-pod request. Request arguments as per L{send_dump_group_delegates}.
+
+        @param request: request arguments
+        @type request: C{dict}
+        &quot;&quot;&quot;
+
+        delegator = yield txn.directoryService().recordWithUID(request[&quot;uid&quot;])
+        if delegator is None or not delegator.thisServer():
+            raise FailedCrossPodRequestError(&quot;Cross-pod delegate missing or on this server: {}&quot;.format(delegator.uid))
+
+        results = yield txn.dumpGroupDelegatesLocal(delegator.uid)
+
+        returnValue([[delegator.serialize(), group.serialize()] for delegator, group in results])
+
+
+    @inlineCallbacks
+    def send_dump_external_delegates(self, txn, delegator):
+        &quot;&quot;&quot;
+        Get L{ExternalDelegateGroupsRecord} from another pod.
+
+        @param txn: transaction to use
+        @type txn: L{CommonStoreTransaction}
+        @param delegator: delegate to lookup
+        @type delegator: L{DirectoryRecord}
+        @param readWrite: if True, read and write access delegates are returned;
+            read-only access otherwise
+        &quot;&quot;&quot;
+        if delegator.thisServer():
+            raise FailedCrossPodRequestError(&quot;Cross-pod destination on this server: {}&quot;.format(delegator.uid))
+
+        request = {
+            &quot;action&quot;: &quot;dump-external-delegates&quot;,
+            &quot;uid&quot;: delegator.uid,
+        }
+        response = yield self.sendRequestToServer(txn, delegator.server(), request)
+        returnValue(response)
+
+
+    @inlineCallbacks
+    def recv_dump_external_delegates(self, txn, request):
+        &quot;&quot;&quot;
+        Process an delegators cross-pod request. Request arguments as per L{send_dump_external_delegates}.
+
+        @param request: request arguments
+        @type request: C{dict}
+        &quot;&quot;&quot;
+
+        delegator = yield txn.directoryService().recordWithUID(request[&quot;uid&quot;])
+        if delegator is None or not delegator.thisServer():
+            raise FailedCrossPodRequestError(&quot;Cross-pod delegate missing or on this server: {}&quot;.format(delegator.uid))
+
+        delegates = yield txn.dumpExternalDelegatesLocal(delegator.uid)
+
+        returnValue(self._to_serialize_list(delegates))
</ins></span></pre></div>
<a id="CalendarServerbranchesuserscdaboopod2podmigrationtxdavcommondatastorepoddingmigrationhome_syncpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/podding/migration/home_sync.py (14422 => 14423)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/podding/migration/home_sync.py        2015-02-17 17:47:33 UTC (rev 14422)
+++ CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/podding/migration/home_sync.py        2015-02-17 17:51:13 UTC (rev 14423)
</span><span class="lines">@@ -150,12 +150,13 @@
</span><span class="cx">         yield self.loadRecord()
</span><span class="cx">         self.homeId = yield self.prepareCalendarHome()
</span><span class="cx"> 
</span><ins>+        # Calendar list and calendar data
</ins><span class="cx">         yield self.syncCalendarList()
</span><span class="cx"> 
</span><del>-        # sync home metadata such as alarms, default calendars, etc
</del><ins>+        # Sync home metadata such as alarms, default calendars, etc
</ins><span class="cx">         yield self.syncCalendarHomeMetaData()
</span><span class="cx"> 
</span><del>-        # TODO: sync attachments
</del><ins>+        # Sync attachments
</ins><span class="cx">         yield self.syncAttachments()
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -166,7 +167,7 @@
</span><span class="cx">         rows, recalculate quota etc.
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-        # TODO: link attachments to resources: ATTACHMENT_CALENDAR_OBJECT table
</del><ins>+        # Link attachments to resources: ATTACHMENT_CALENDAR_OBJECT table
</ins><span class="cx">         yield self.linkAttachments()
</span><span class="cx"> 
</span><span class="cx">         # TODO: Re-write attachment URIs - not sure if we need this as reverse proxy may take care of it
</span><span class="lines">@@ -182,7 +183,7 @@
</span><span class="cx">         pass
</span><span class="cx"> 
</span><span class="cx">         # TODO: delegates reconcile
</span><del>-        pass
</del><ins>+        yield self.delegateReconcile()
</ins><span class="cx"> 
</span><span class="cx">         # TODO: notifications
</span><span class="cx">         pass
</span><span class="lines">@@ -780,3 +781,54 @@
</span><span class="cx">             link._calendarObjectID = objectIDMap[link._calendarObjectID].localResourceID
</span><span class="cx"> 
</span><span class="cx">             yield link.insert()
</span><ins>+
+
+    @inlineCallbacks
+    def delegateReconcile(self):
+        &quot;&quot;&quot;
+        Sync the delegate assignments from the remote home to the local home. We won't use
+        a fake directory UID locally.
+        &quot;&quot;&quot;
+
+        yield self.individualDelegateReconcile()
+        yield self.groupDelegateReconcile()
+        yield self.externalDelegateReconcile()
+
+
+    @inTransactionWrapper
+    @inlineCallbacks
+    def individualDelegateReconcile(self, txn):
+        &quot;&quot;&quot;
+        Sync the delegate assignments from the remote home to the local home. We won't use
+        a fake directory UID locally.
+        &quot;&quot;&quot;
+        remote_records = yield txn.dumpIndividualDelegatesExternal(self.record)
+        for record in remote_records:
+            yield record.insert(txn)
+
+
+    @inTransactionWrapper
+    @inlineCallbacks
+    def groupDelegateReconcile(self, txn):
+        &quot;&quot;&quot;
+        Sync the delegate assignments from the remote home to the local home. We won't use
+        a fake directory UID locally.
+        &quot;&quot;&quot;
+        remote_records = yield txn.dumpGroupDelegatesExternal(self.record)
+        for delegator, group in remote_records:
+            # We need to make sure the group exists locally first and map the groupID to the local one
+            local_group = yield txn.groupByUID(group.groupUID)
+            delegator.groupID = local_group.groupID
+            yield delegator.insert(txn)
+
+
+    @inTransactionWrapper
+    @inlineCallbacks
+    def externalDelegateReconcile(self, txn):
+        &quot;&quot;&quot;
+        Sync the external delegate assignments from the remote home to the local home. We won't use
+        a fake directory UID locally.
+        &quot;&quot;&quot;
+        remote_records = yield txn.dumpExternalDelegatesExternal(self.record)
+        for record in remote_records:
+            yield record.insert(txn)
</ins></span></pre></div>
<a id="CalendarServerbranchesuserscdaboopod2podmigrationtxdavcommondatastorepoddingmigrationtesttest_home_syncpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/podding/migration/test/test_home_sync.py (14422 => 14423)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/podding/migration/test/test_home_sync.py        2015-02-17 17:47:33 UTC (rev 14422)
+++ CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/podding/migration/test/test_home_sync.py        2015-02-17 17:51:13 UTC (rev 14423)
</span><span class="lines">@@ -22,14 +22,17 @@
</span><span class="cx"> from txdav.common.datastore.podding.migration.home_sync import CrossPodHomeSync
</span><span class="cx"> from txdav.common.datastore.podding.migration.sync_metadata import CalendarMigrationRecord
</span><span class="cx"> from txdav.common.datastore.podding.test.util import MultiStoreConduitTest
</span><ins>+from txdav.common.datastore.sql_directory import DelegateRecord, \
+    ExternalDelegateGroupsRecord, DelegateGroupsRecord
</ins><span class="cx"> from txdav.common.datastore.sql_tables import schema
</span><ins>+from txdav.who.delegates import Delegates
</ins><span class="cx"> from txweb2.http_headers import MimeType
</span><span class="cx"> from txweb2.stream import MemoryStream
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-class TestConduitAPI(MultiStoreConduitTest):
</del><ins>+class TestCrossPodHomeSync(MultiStoreConduitTest):
</ins><span class="cx">     &quot;&quot;&quot;
</span><del>-    Test that the conduit api works.
</del><ins>+    Test that L{CrossPodHomeSync} works.
</ins><span class="cx">     &quot;&quot;&quot;
</span><span class="cx"> 
</span><span class="cx">     nowYear = {&quot;now&quot;: DateTime.getToday().getYear()}
</span><span class="lines">@@ -698,3 +701,89 @@
</span><span class="cx">         attachment = yield object1.attachmentWithManagedID(managedid0_1)
</span><span class="cx">         self.assertTrue(attachment is not None)
</span><span class="cx">         self.assertEqual(attachment.md5(), md50_1)
</span><ins>+
+
+    @inlineCallbacks
+    def test_delegate_reconcile(self):
+        &quot;&quot;&quot;
+        Test that L{delegateReconcile} copies over the full set of delegates and caches associated groups..
+        &quot;&quot;&quot;
+
+        # Create remote home
+        yield self.homeUnderTest(txn=self.theTransactionUnderTest(0), name=&quot;user01&quot;, create=True)
+        yield self.commitTransaction(0)
+
+        # Add some delegates
+        txn = self.theTransactionUnderTest(0)
+        record01 = yield txn.directoryService().recordWithUID(u&quot;user01&quot;)
+        record02 = yield txn.directoryService().recordWithUID(u&quot;user02&quot;)
+        record03 = yield txn.directoryService().recordWithUID(u&quot;user03&quot;)
+
+        group01 = yield txn.directoryService().recordWithUID(u&quot;__top_group_1__&quot;)
+        group02 = yield txn.directoryService().recordWithUID(u&quot;right_coast&quot;)
+
+        # Add user02 and user03 as individual delegates
+        yield Delegates.addDelegate(txn, record01, record02, True)
+        yield Delegates.addDelegate(txn, record01, record03, False)
+
+        # Add group delegates
+        yield Delegates.addDelegate(txn, record01, group01, True)
+        yield Delegates.addDelegate(txn, record01, group02, False)
+
+        # Add external delegates
+        yield txn.assignExternalDelegates(u&quot;user01&quot;, None, None, u&quot;external1&quot;, u&quot;external2&quot;)
+
+        yield self.commitTransaction(0)
+
+
+        # Initially no local delegates
+        txn = self.theTransactionUnderTest(1)
+        delegates = yield txn.dumpIndividualDelegatesLocal(u&quot;user01&quot;)
+        self.assertEqual(len(delegates), 0)
+        delegates = yield txn.dumpGroupDelegatesLocal(u&quot;user04&quot;)
+        self.assertEqual(len(delegates), 0)
+        externals = yield txn.dumpExternalDelegatesLocal(u&quot;user01&quot;)
+        self.assertEqual(len(externals), 0)
+        yield self.commitTransaction(1)
+
+        # Sync from remote side
+        syncer = CrossPodHomeSync(self.theStoreUnderTest(1), &quot;user01&quot;)
+        yield syncer.loadRecord()
+        yield syncer.delegateReconcile()
+
+        # Now have local delegates
+        txn = self.theTransactionUnderTest(1)
+
+        delegates = yield txn.dumpIndividualDelegatesLocal(u&quot;user01&quot;)
+        self.assertEqual(
+            set(delegates),
+            set((
+                DelegateRecord.make(delegator=&quot;user01&quot;, delegate=&quot;user02&quot;, readWrite=1),
+                DelegateRecord.make(delegator=&quot;user01&quot;, delegate=&quot;user03&quot;, readWrite=0),
+            )),
+        )
+
+        delegateGroups = yield txn.dumpGroupDelegatesLocal(u&quot;user01&quot;)
+        group_top = yield txn.groupByUID(u&quot;__top_group_1__&quot;)
+        group_right = yield txn.groupByUID(u&quot;right_coast&quot;)
+        self.assertEqual(
+            set([item[0] for item in delegateGroups]),
+            set((
+                DelegateGroupsRecord.make(delegator=&quot;user01&quot;, groupID=group_top.groupID, readWrite=1, isExternal=False),
+                DelegateGroupsRecord.make(delegator=&quot;user01&quot;, groupID=group_right.groupID, readWrite=0, isExternal=False),
+            )),
+        )
+
+        externals = yield txn.dumpExternalDelegatesLocal(u&quot;user01&quot;)
+        self.assertEqual(
+            set(externals),
+            set((
+                ExternalDelegateGroupsRecord.make(
+                    delegator=&quot;user01&quot;,
+                    groupUIDRead=&quot;external1&quot;,
+                    groupUIDWrite=&quot;external2&quot;,
+                ),
+            )),
+        )
+
+        yield self.commitTransaction(1)
</ins></span></pre></div>
<a id="CalendarServerbranchesuserscdaboopod2podmigrationtxdavcommondatastoresql_directorypy"></a>
<div class="modfile"><h4>Modified: CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/sql_directory.py (14422 => 14423)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/sql_directory.py        2015-02-17 17:47:33 UTC (rev 14422)
+++ CalendarServer/branches/users/cdaboo/pod2pod-migration/txdav/common/datastore/sql_directory.py        2015-02-17 17:51:13 UTC (rev 14423)
</span><span class="lines">@@ -16,7 +16,7 @@
</span><span class="cx"> ##
</span><span class="cx"> 
</span><span class="cx"> from twext.enterprise.dal.record import SerializableRecord, fromTable
</span><del>-from twext.enterprise.dal.syntax import SavepointAction
</del><ins>+from twext.enterprise.dal.syntax import SavepointAction, Select
</ins><span class="cx"> from twext.python.log import Logger
</span><span class="cx"> from twisted.internet.defer import inlineCallbacks, returnValue
</span><span class="cx"> from txdav.common.datastore.sql_tables import schema
</span><span class="lines">@@ -149,7 +149,39 @@
</span><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    @classmethod
+    @inlineCallbacks
+    def delegatorGroups(cls, txn, delegator):
+        &quot;&quot;&quot;
+        Get delegator/group pairs for the specified delegator.
+        &quot;&quot;&quot;
</ins><span class="cx"> 
</span><ins>+        # Do a join to get what we need
+        rows = yield Select(
+            list(DelegateGroupsRecord.table) + list(GroupsRecord.table),
+            From=DelegateGroupsRecord.table.join(GroupsRecord.table, DelegateGroupsRecord.groupID == GroupsRecord.groupID),
+            Where=(DelegateGroupsRecord.delegator == delegator.encode(&quot;utf-8&quot;))
+        ).on(txn)
+
+        results = []
+        delegatorNames = [DelegateGroupsRecord.__colmap__[column] for column in list(DelegateGroupsRecord.table)]
+        groupsNames = [GroupsRecord.__colmap__[column] for column in list(GroupsRecord.table)]
+        split_point = len(delegatorNames)
+        for row in rows:
+            delegatorRow = row[:split_point]
+            delegatorRecord = DelegateGroupsRecord()
+            delegatorRecord._attributesFromRow(zip(delegatorNames, delegatorRow))
+            delegatorRecord.transaction = txn
+            groupsRow = row[split_point:]
+            groupsRecord = GroupsRecord()
+            groupsRecord._attributesFromRow(zip(groupsNames, groupsRow))
+            groupsRecord.transaction = txn
+            results.append((delegatorRecord, groupsRecord,))
+
+        returnValue(results)
+
+
+
</ins><span class="cx"> class ExternalDelegateGroupsRecord(SerializableRecord, fromTable(schema.EXTERNAL_DELEGATE_GROUPS)):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     @DynamicAttrs
</span><span class="lines">@@ -766,3 +798,51 @@
</span><span class="cx">             yield self.addDelegateGroup(
</span><span class="cx">                 delegator, writeDelegateGroupID, True, isExternal=True
</span><span class="cx">             )
</span><ins>+
+
+    def dumpIndividualDelegatesLocal(self, delegator):
+        &quot;&quot;&quot;
+        Get the L{DelegateRecord} for all delegates associated with this delegator.
+        &quot;&quot;&quot;
+        return DelegateRecord.querysimple(self, delegator=delegator.encode(&quot;utf-8&quot;))
+
+
+    @inlineCallbacks
+    def dumpIndividualDelegatesExternal(self, delegator):
+        &quot;&quot;&quot;
+        Get the L{DelegateRecord} for all delegates associated with this delegator.
+        &quot;&quot;&quot;
+        raw_results = yield self.store().conduit.send_dump_individual_delegates(self, delegator)
+        returnValue([DelegateRecord.deserialize(row) for row in raw_results])
+
+
+    def dumpGroupDelegatesLocal(self, delegator):
+        &quot;&quot;&quot;
+        Get the L{DelegateGroupsRecord},L{GroupsRecord} for all group delegates associated with this delegator.
+        &quot;&quot;&quot;
+        return DelegateGroupsRecord.delegatorGroups(self, delegator)
+
+
+    @inlineCallbacks
+    def dumpGroupDelegatesExternal(self, delegator):
+        &quot;&quot;&quot;
+        Get the L{DelegateGroupsRecord},L{GroupsRecord} for all delegates associated with this delegator.
+        &quot;&quot;&quot;
+        raw_results = yield self.store().conduit.send_dump_group_delegates(self, delegator)
+        returnValue([(DelegateGroupsRecord.deserialize(row[0]), GroupsRecord.deserialize(row[1]),) for row in raw_results])
+
+
+    def dumpExternalDelegatesLocal(self, delegator):
+        &quot;&quot;&quot;
+        Get the L{ExternalDelegateGroupsRecord} for all delegates associated with this delegator.
+        &quot;&quot;&quot;
+        return ExternalDelegateGroupsRecord.querysimple(self, delegator=delegator.encode(&quot;utf-8&quot;))
+
+
+    @inlineCallbacks
+    def dumpExternalDelegatesExternal(self, delegator):
+        &quot;&quot;&quot;
+        Get the L{ExternalDelegateGroupsRecord} for all delegates associated with this delegator.
+        &quot;&quot;&quot;
+        raw_results = yield self.store().conduit.send_dump_external_delegates(self, delegator)
+        returnValue([ExternalDelegateGroupsRecord.deserialize(row) for row in raw_results])
</ins></span></pre>
</div>
</div>

</body>
</html>