<!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>[14901] CalendarServer/trunk</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/14901">14901</a></dd>
<dt>Author</dt> <dd>sagen@apple.com</dd>
<dt>Date</dt> <dd>2015-06-19 11:06:12 -0700 (Fri, 19 Jun 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Adds ability to filter attendee lookup results so only records with names *starting* with all the search tokens are returned</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunktwistedcaldavextensionspy">CalendarServer/trunk/twistedcaldav/extensions.py</a></li>
<li><a href="#CalendarServertrunktwistedcaldavstdconfigpy">CalendarServer/trunk/twistedcaldav/stdconfig.py</a></li>
<li><a href="#CalendarServertrunktwistedcaldavtesttest_upgradepy">CalendarServer/trunk/twistedcaldav/test/test_upgrade.py</a></li>
<li><a href="#CalendarServertrunktxdavcommondatastoretestaccountsaccountsxml">CalendarServer/trunk/txdav/common/datastore/test/accounts/accounts.xml</a></li>
<li><a href="#CalendarServertrunktxdavwhodirectorypy">CalendarServer/trunk/txdav/who/directory.py</a></li>
<li><a href="#CalendarServertrunktxdavwhotesttest_directorypy">CalendarServer/trunk/txdav/who/test/test_directory.py</a></li>
<li><a href="#CalendarServertrunktxdavwhotesttest_utilpy">CalendarServer/trunk/txdav/who/test/test_util.py</a></li>
<li><a href="#CalendarServertrunktxdavwhoutilpy">CalendarServer/trunk/txdav/who/util.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunktwistedcaldavextensionspy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/extensions.py (14900 => 14901)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/extensions.py        2015-06-18 15:00:11 UTC (rev 14900)
+++ CalendarServer/trunk/twistedcaldav/extensions.py        2015-06-19 18:06:12 UTC (rev 14901)
</span><span class="lines">@@ -1041,7 +1041,9 @@
</span><span class="cx">             applyTo = True
</span><span class="cx"> 
</span><span class="cx">         elif child.qname() == (calendarserver_namespace, &quot;search-token&quot;):
</span><del>-            tokens.append(child.toString())
</del><ins>+            tokenValue = child.toString().strip()
+            if tokenValue:
+                tokens.append(tokenValue)
</ins><span class="cx"> 
</span><span class="cx">         elif child.qname() == (calendarserver_namespace, &quot;limit&quot;):
</span><span class="cx">             try:
</span></span></pre></div>
<a id="CalendarServertrunktwistedcaldavstdconfigpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/stdconfig.py (14900 => 14901)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/stdconfig.py        2015-06-18 15:00:11 UTC (rev 14900)
+++ CalendarServer/trunk/twistedcaldav/stdconfig.py        2015-06-19 18:06:12 UTC (rev 14901)
</span><span class="lines">@@ -316,6 +316,10 @@
</span><span class="cx"> 
</span><span class="cx">     &quot;DirectoryRealmName&quot;: &quot;&quot;,
</span><span class="cx"> 
</span><ins>+    # Apply an additional filter for attendee lookups where names must start
+    # with the search tokens rather than just contain them.
+    &quot;DirectoryFilterStartsWith&quot;: False,
+
</ins><span class="cx">     #
</span><span class="cx">     # Locations and Resources service
</span><span class="cx">     #
</span></span></pre></div>
<a id="CalendarServertrunktwistedcaldavtesttest_upgradepy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/test/test_upgrade.py (14900 => 14901)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/test/test_upgrade.py        2015-06-18 15:00:11 UTC (rev 14900)
+++ CalendarServer/trunk/twistedcaldav/test/test_upgrade.py        2015-06-19 18:06:12 UTC (rev 14901)
</span><span class="lines">@@ -1739,8 +1739,8 @@
</span><span class="cx"> UID:1E238CA1-3C95-4468-B8CD-C8A399F78C71
</span><span class="cx"> DTSTART;TZID=US/Pacific:20090203T120000
</span><span class="cx"> DTEND;TZID=US/Pacific:20090203T130000
</span><del>-ATTENDEE;CN=Wilfredo Sanchez;CUTYPE=INDIVIDUAL;EMAIL=wsanchez@example.com;
- PARTSTAT=ACCEPTED:urn:x-uid:6423F94A-6B76-4A3A-815B-D52CFD77935D
</del><ins>+ATTENDEE;CN=Wilfredo Sanchez-Vega;CUTYPE=INDIVIDUAL;EMAIL=wsanchez@example
+ .com;PARTSTAT=ACCEPTED:urn:x-uid:6423F94A-6B76-4A3A-815B-D52CFD77935D
</ins><span class="cx"> ATTENDEE;CN=Double 'quotey' Quotes;CUTYPE=INDIVIDUAL;EMAIL=doublequotes@ex
</span><span class="cx">  ample.com;PARTSTAT=ACCEPTED:urn:x-uid:8E04787E-336D-41ED-A70B-D233AD0DCE6
</span><span class="cx">  F
</span></span></pre></div>
<a id="CalendarServertrunktxdavcommondatastoretestaccountsaccountsxml"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/common/datastore/test/accounts/accounts.xml (14900 => 14901)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/common/datastore/test/accounts/accounts.xml        2015-06-18 15:00:11 UTC (rev 14900)
+++ CalendarServer/trunk/txdav/common/datastore/test/accounts/accounts.xml        2015-06-19 18:06:12 UTC (rev 14901)
</span><span class="lines">@@ -30,7 +30,7 @@
</span><span class="cx">     &lt;guid&gt;6423F94A-6B76-4A3A-815B-D52CFD77935D&lt;/guid&gt;
</span><span class="cx">     &lt;short-name&gt;wsanchez&lt;/short-name&gt;
</span><span class="cx">     &lt;password&gt;zehcnasw&lt;/password&gt;
</span><del>-    &lt;full-name&gt;Wilfredo Sanchez&lt;/full-name&gt;
</del><ins>+    &lt;full-name&gt;Wilfredo Sanchez-Vega&lt;/full-name&gt;
</ins><span class="cx">     &lt;email&gt;wsanchez@example.com&lt;/email&gt;
</span><span class="cx">   &lt;/record&gt;
</span><span class="cx">   &lt;record type=&quot;user&quot;&gt;
</span></span></pre></div>
<a id="CalendarServertrunktxdavwhodirectorypy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/who/directory.py (14900 => 14901)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/directory.py        2015-06-18 15:00:11 UTC (rev 14900)
+++ CalendarServer/trunk/txdav/who/directory.py        2015-06-19 18:06:12 UTC (rev 14901)
</span><span class="lines">@@ -200,10 +200,20 @@
</span><span class="cx">         else:
</span><span class="cx">             recordTypes = None
</span><span class="cx"> 
</span><del>-        results = yield self.recordsFromExpression(
-            expression, recordTypes=recordTypes, limitResults=limitResults,
-            timeoutSeconds=timeoutSeconds
-        )
</del><ins>+        # If a filter has been set, pass self.recordsFromExpression to it for
+        # result processing
+        if getattr(self, &quot;_resultFilter&quot;, None):
+            results = yield self._resultFilter(
+                self.recordsFromExpression, tokens, expression,
+                recordTypes=recordTypes, limitResults=limitResults,
+                timeoutSeconds=timeoutSeconds
+            )
+        else:
+            results = yield self.recordsFromExpression(
+                expression, recordTypes=recordTypes, limitResults=limitResults,
+                timeoutSeconds=timeoutSeconds
+            )
+
</ins><span class="cx">         log.debug(
</span><span class="cx">             &quot;Tokens ({t}) matched {n} records&quot;,
</span><span class="cx">             t=tokens, n=len(results)
</span><span class="lines">@@ -256,6 +266,18 @@
</span><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+    def setFilter(self, filter):
+        &quot;&quot;&quot;
+        Assign a filter for post-processing recordsMatchingTokens and
+        recordsMatchingFields results.
+
+        @param filter: a callable taking the following args:
+            method, tokens, expression, recordTypes, limitResults, timeoutSeconds
+            ...and returning a deferred firing with the list of records
+        &quot;&quot;&quot;
+        self._resultFilter = filter
+
+
</ins><span class="cx">     _oldRecordTypeNames = {
</span><span class="cx">         &quot;address&quot;: &quot;addresses&quot;,
</span><span class="cx">         &quot;group&quot;: &quot;groups&quot;,
</span></span></pre></div>
<a id="CalendarServertrunktxdavwhotesttest_directorypy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/who/test/test_directory.py (14900 => 14901)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/test/test_directory.py        2015-06-18 15:00:11 UTC (rev 14900)
+++ CalendarServer/trunk/txdav/who/test/test_directory.py        2015-06-19 18:06:12 UTC (rev 14901)
</span><span class="lines">@@ -23,6 +23,7 @@
</span><span class="cx"> from twext.who.directory import DirectoryRecord
</span><span class="cx"> from twext.who.idirectory import FieldName, RecordType
</span><span class="cx"> from txdav.who.directory import CalendarDirectoryRecordMixin, AutoScheduleMode
</span><ins>+from txdav.who.util import startswithFilter
</ins><span class="cx"> from uuid import UUID
</span><span class="cx"> from twext.who.expression import (
</span><span class="cx">     MatchType, MatchFlags, MatchExpression
</span><span class="lines">@@ -50,7 +51,7 @@
</span><span class="cx"> 
</span><span class="cx">         expanded = yield record.expandedMembers()
</span><span class="cx">         self.assertEquals(
</span><del>-            set([u&quot;Chris Lecroy&quot;, u&quot;Cyrus Daboo&quot;, u&quot;David Reid&quot;, u&quot;Wilfredo Sanchez&quot;]),
</del><ins>+            set([u&quot;Chris Lecroy&quot;, u&quot;Cyrus Daboo&quot;, u&quot;David Reid&quot;, u&quot;Wilfredo Sanchez-Vega&quot;]),
</ins><span class="cx">             set([r.displayName for r in expanded])
</span><span class="cx">         )
</span><span class="cx"> 
</span><span class="lines">@@ -205,6 +206,53 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @inlineCallbacks
</span><ins>+    def test_recordsMatchingTokensNoFilter(self):
+        &quot;&quot;&quot;
+        Records with names containing the token are returned
+        &quot;&quot;&quot;
+
+        records = (yield self.directory.recordsMatchingTokens(
+            [u&quot;anche&quot;]
+        ))
+        matchingShortNames = set()
+        for r in records:
+            for shortName in r.shortNames:
+                matchingShortNames.add(shortName)
+        self.assertTrue(&quot;dre&quot; in matchingShortNames)
+        self.assertTrue(&quot;wsanchez&quot; in matchingShortNames)
+
+
+    @inlineCallbacks
+    def test_recordsMatchingTokensStartswithFilter(self):
+        &quot;&quot;&quot;
+        Records with names starting with the token are returned, because of
+        the filter installed.  Note that hyphens and spaces are used to split
+        fullname into names.
+        &quot;&quot;&quot;
+        self.directory.setFilter(startswithFilter)
+
+        records = (yield self.directory.recordsMatchingTokens(
+            [u&quot;anche&quot;]
+        ))
+        matchingShortNames = set()
+        for r in records:
+            for shortName in r.shortNames:
+                matchingShortNames.add(shortName)
+        self.assertTrue(&quot;dre&quot; not in matchingShortNames)
+        self.assertTrue(&quot;wsanchez&quot; not in matchingShortNames)
+
+        records = (yield self.directory.recordsMatchingTokens(
+            [u&quot;vega&quot;, u&quot;wilf&quot;]
+        ))
+        matchingShortNames = set()
+        for r in records:
+            for shortName in r.shortNames:
+                matchingShortNames.add(shortName)
+        self.assertTrue(&quot;dre&quot; not in matchingShortNames)
+        self.assertTrue(&quot;wsanchez&quot; in matchingShortNames)
+
+
+    @inlineCallbacks
</ins><span class="cx">     def test_getAutoScheduleMode(self):
</span><span class="cx"> 
</span><span class="cx">         apollo = yield self.directory.recordWithUID(u&quot;apollo&quot;)
</span></span></pre></div>
<a id="CalendarServertrunktxdavwhotesttest_utilpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/who/test/test_util.py (14900 => 14901)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/test/test_util.py        2015-06-18 15:00:11 UTC (rev 14900)
+++ CalendarServer/trunk/txdav/who/test/test_util.py        2015-06-19 18:06:12 UTC (rev 14901)
</span><span class="lines">@@ -114,6 +114,7 @@
</span><span class="cx">                     &quot;InProcessCachingSeconds&quot;: 60,
</span><span class="cx">                     &quot;InSidecarCachingSeconds&quot;: 120,
</span><span class="cx">                 },
</span><ins>+                &quot;DirectoryFilterStartsWith&quot;: False,
</ins><span class="cx">             }
</span><span class="cx">         )
</span><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServertrunktxdavwhoutilpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/txdav/who/util.py (14900 => 14901)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/txdav/who/util.py        2015-06-18 15:00:11 UTC (rev 14900)
+++ CalendarServer/trunk/txdav/who/util.py        2015-06-19 18:06:12 UTC (rev 14901)
</span><span class="lines">@@ -14,7 +14,8 @@
</span><span class="cx"> # limitations under the License.
</span><span class="cx"> ##
</span><span class="cx"> 
</span><del>-
</del><ins>+import re
+from twisted.internet.defer import inlineCallbacks, returnValue
</ins><span class="cx"> from twext.python.log import Logger
</span><span class="cx"> from twext.python.types import MappingProxyType
</span><span class="cx"> from twext.who.aggregate import DirectoryService as AggregateDirectoryService
</span><span class="lines">@@ -69,14 +70,15 @@
</span><span class="cx">         config.AugmentService,
</span><span class="cx">         config.Authentication.Wiki,
</span><span class="cx">         serversDB=serversDB,
</span><del>-        cachingSeconds=config.DirectoryProxy.InSidecarCachingSeconds
</del><ins>+        cachingSeconds=config.DirectoryProxy.InSidecarCachingSeconds,
+        filterStartsWith=config.DirectoryFilterStartsWith
</ins><span class="cx">     )
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> def buildDirectory(
</span><span class="cx">     store, dataRoot, servicesInfo, augmentServiceInfo, wikiServiceInfo,
</span><del>-    serversDB=None, cachingSeconds=0
</del><ins>+    serversDB=None, cachingSeconds=0, filterStartsWith=False
</ins><span class="cx"> ):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     Return a directory without using a config object; suitable for tests
</span><span class="lines">@@ -315,6 +317,9 @@
</span><span class="cx">     if serversDB is not None:
</span><span class="cx">         augmented.setServersDB(serversDB)
</span><span class="cx"> 
</span><ins>+    if filterStartsWith:
+        augmented.setFilter(startswithFilter)
+
</ins><span class="cx">     return augmented
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -327,3 +332,49 @@
</span><span class="cx"> 
</span><span class="cx"> &lt;augments/&gt;
</span><span class="cx"> &quot;&quot;&quot;
</span><ins>+
+
+@inlineCallbacks
+def startswithFilter(
+    method, tokens, expression, recordTypes=None, records=None,
+    limitResults=None, timeoutSeconds=None
+):
+    &quot;&quot;&quot;
+    Call the passed-in method to retrieve records from the directory, but
+    further filter the results by only returning records whose email addresses
+    and names actually start with the tokens.  Without this filter, it's only
+    required that the record's fullname *contains* the tokens. &quot;Names&quot; are split
+    from the record's fullname, delimited by whitespace and hypens.
+    &quot;&quot;&quot;
+
+    tokens = [t.lower() for t in tokens]
+
+    results = []
+    records = yield method(
+        expression, recordTypes=recordTypes, limitResults=1000,
+        timeoutSeconds=timeoutSeconds
+    )
+    count = 0
+    for record in records:
+        try:
+            names = list(record.emailAddresses)
+        except AttributeError:
+            names = []
+        for fullName in record.fullNames:
+            names.extend(re.split(' |-', fullName))
+        match = True # assume it will match
+        for token in tokens:
+            for name in names:
+                if name.lower().startswith(token):
+                    break
+            else:
+                # there was no match for this token
+                match = False
+                break
+        if match:
+            results.append(record)
+            count += 1
+            if limitResults and count == limitResults:
+                break
+
+    returnValue(results)
</ins></span></pre>
</div>
</div>

</body>
</html>