[CalendarServer-changes] [13582] CalendarServer/trunk/txdav

source_changes at macosforge.org source_changes at macosforge.org
Fri May 30 17:39:56 PDT 2014


Revision: 13582
          http://trac.calendarserver.org//changeset/13582
Author:   sagen at apple.com
Date:     2014-05-30 17:39:56 -0700 (Fri, 30 May 2014)
Log Message:
-----------
Add 'context' support to recordsMatchingTokens()

Modified Paths:
--------------
    CalendarServer/trunk/txdav/dps/test/test_client.py
    CalendarServer/trunk/txdav/who/directory.py

Modified: CalendarServer/trunk/txdav/dps/test/test_client.py
===================================================================
--- CalendarServer/trunk/txdav/dps/test/test_client.py	2014-05-31 00:14:35 UTC (rev 13581)
+++ CalendarServer/trunk/txdav/dps/test/test_client.py	2014-05-31 00:39:56 UTC (rev 13582)
@@ -398,6 +398,75 @@
 
 
     @inlineCallbacks
+    def test_recordsMatchingTokensWithContext(self):
+
+        testData = [
+            # (
+            #     tokens,
+            #     context,
+            #     short names expected to be in results,
+            #     short names not expected to be in results
+            # ),
+            (
+                [u"an"],
+                "user",  # just users
+                (u"dre", u"wsanchez"),
+                (u"transporter", u"managers"),
+            ),
+            (
+                [u"an"],
+                "group",  # just groups
+                (u"managers",),
+                (u"dre", u"wsanchez", u"transporter"),
+            ),
+            (
+                [u"an"],
+                "location",  # just locations
+                (u"sanchezoffice",),
+                (u"dre", u"wsanchez", u"transporter", u"managers"),
+            ),
+            (
+                [u"an"],
+                "resource",  # just resources
+                (u"transporter", u"ftlcpu"),
+                (u"dre", u"wsanchez", u"managers", u"sanchezoffice"),
+            ),
+            (
+                [u"an"],
+                "attendee",  # just users, groups, resources
+                (
+                    u"dre", u"wsanchez", u"managers",
+                    u"transporter", u"ftlcpu"
+                ),
+                (u"sanchezoffice",),
+            ),
+            (
+                [u"an"],
+                None,   # any type
+                (
+                    u"dre", u"wsanchez", u"managers", u"sanchezoffice",
+                    u"transporter", u"ftlcpu"
+                ),
+                (),
+            ),
+        ]
+
+        for tokens, context, expected, unexpected in testData:
+            # print("Tokens", tokens, "context", context)
+            records = yield self.directory.recordsMatchingTokens(
+                tokens, context
+            )
+            matchingShortNames = set()
+            for r in records:
+                for shortName in r.shortNames:
+                    matchingShortNames.add(shortName)
+            for name in expected:
+                self.assertTrue(name in matchingShortNames)
+            for name in unexpected:
+                self.assertFalse(name in matchingShortNames)
+
+
+    @inlineCallbacks
     def test_recordsMatchingFields_anyType(self):
         fields = (
             (u"fullNames", "anche", MatchFlags.caseInsensitive, MatchType.contains),

Modified: CalendarServer/trunk/txdav/who/directory.py
===================================================================
--- CalendarServer/trunk/txdav/who/directory.py	2014-05-31 00:14:35 UTC (rev 13581)
+++ CalendarServer/trunk/txdav/who/directory.py	2014-05-31 00:39:56 UTC (rev 13582)
@@ -108,13 +108,48 @@
 
         returnValue(None)
 
+    searchContext_location = "location"
+    searchContext_resource = "resource"
+    searchContext_user     = "user"
+    searchContext_group    = "group"
+    searchContext_attendee = "attendee"
 
+
+    def recordTypesForSearchContext(self, context):
+        """
+        Map calendarserver-principal-search REPORT context value to applicable record types
+
+        @param context: The context value to map
+        @type context: C{unicode}
+        @returns: The list of record types the context maps to
+        @rtype: C{list} of C{NamedConstant}
+        """
+        if context == "location":
+            recordTypes = [self.recordType.location]
+        elif context == "resource":
+            recordTypes = [self.recordType.resource]
+        elif context == "user":
+            recordTypes = [self.recordType.user]
+        elif context == "group":
+            recordTypes = [self.recordType.group]
+        elif context == "attendee":
+            recordTypes = [
+                self.recordType.user,
+                self.recordType.group,
+                self.recordType.resource
+            ]
+        else:
+            recordTypes = list(self.recordTypes())
+        return recordTypes
+
+
     def recordsMatchingTokens(self, tokens, context=None, limitResults=50,
                               timeoutSeconds=10):
         fields = [
             ("fullNames", MatchType.contains),
             ("emailAddresses", MatchType.startsWith),
         ]
+
         outer = []
         for token in tokens:
             inner = []
@@ -139,6 +174,36 @@
         else:
             expression = CompoundExpression(outer, Operand.AND)
 
+        if context is not None:
+            recordTypes = self.recordTypesForSearchContext(context)
+            log.debug("Context {c}, recordTypes {r}", c=context, r=recordTypes)
+            typeSpecific = []
+            for recordType in recordTypes:
+                typeSpecific.append(
+                    MatchExpression(
+                        self.fieldName.recordType,
+                        recordType,
+                        MatchType.equals,
+                        MatchFlags.none
+                    )
+                )
+
+            if len(typeSpecific) == 1:
+                typeSpecific = typeSpecific[0]
+
+            else:
+                typeSpecific = CompoundExpression(
+                    typeSpecific,
+                    Operand.OR
+                )
+
+            expression = CompoundExpression(
+                [expression, typeSpecific],
+                Operand.AND
+            )
+
+        log.debug("Expression {e}", e=expression)
+
         return self.recordsFromExpression(expression)
 
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140530/1128737d/attachment.html>


More information about the calendarserver-changes mailing list