[CalendarServer-changes] [13653] CalendarServer/branches/users/sagen/recordtypes

source_changes at macosforge.org source_changes at macosforge.org
Wed Jun 18 13:27:27 PDT 2014


Revision: 13653
          http://trac.calendarserver.org//changeset/13653
Author:   sagen at apple.com
Date:     2014-06-18 13:27:26 -0700 (Wed, 18 Jun 2014)
Log Message:
-----------
Removing some hacks because we can now pass recordTypes directly to recordsFromExpression

Modified Paths:
--------------
    CalendarServer/branches/users/sagen/recordtypes/contrib/od/test/test_live.py
    CalendarServer/branches/users/sagen/recordtypes/requirements-stable.txt
    CalendarServer/branches/users/sagen/recordtypes/txdav/who/directory.py

Modified: CalendarServer/branches/users/sagen/recordtypes/contrib/od/test/test_live.py
===================================================================
--- CalendarServer/branches/users/sagen/recordtypes/contrib/od/test/test_live.py	2014-06-18 20:26:18 UTC (rev 13652)
+++ CalendarServer/branches/users/sagen/recordtypes/contrib/od/test/test_live.py	2014-06-18 20:27:26 UTC (rev 13653)
@@ -40,7 +40,16 @@
     from twext.who.expression import (
         CompoundExpression, Operand, MatchExpression, MatchType
     )
+    from twext.who.idirectory import QueryNotSupportedError
+    from txdav.who.directory import CalendarDirectoryServiceMixin
+    from txdav.who.opendirectory import DirectoryService as OpenDirectoryService
+    from twext.who.expression import (
+        Operand, MatchType, MatchFlags, MatchExpression
+    )
 
+    class CalOpenDirectoryService(OpenDirectoryService, CalendarDirectoryServiceMixin):
+        pass
+
     LOCAL_SHORTNAMES = "odtestalbert odtestbill odtestcarl odtestdavid odtestsubgroupa".split()
     NETWORK_SHORTNAMES = "odtestamanda odtestbetty odtestcarlene odtestdenise odtestsubgroupb odtestgrouptop".split()
 
@@ -160,18 +169,119 @@
             # We should get back users and groups since we did not specify a type:
             self.verifyResults(
                 records,
-                ["odtestbetty", "odtestalbert", "anotherodtestalbert", "odtestgroupbetty", "odtestgroupalbert"],
+                [
+                    "odtestbetty", "odtestalbert", "anotherodtestalbert",
+                    "odtestgroupbetty", "odtestgroupalbert"
+                ],
                 ["odtestamanda", "odtestbill", "odtestgroupa", "odtestgroupb"]
             )
 
 
         @onlyIfPopulated
         @inlineCallbacks
-        def test_compoundWithSingleRecordType(self):
+        def test_compoundWithExplicitRecordType(self):
             expression = CompoundExpression(
                 [
                     CompoundExpression(
                         [
+                            MatchExpression(
+                                self.service.fieldName.fullNames, u"be",
+                                matchType=MatchType.contains
+                            ),
+                            MatchExpression(
+                                self.service.fieldName.emailAddresses, u"be",
+                                matchType=MatchType.startsWith
+                            ),
+                        ],
+                        Operand.OR
+                    ),
+                    CompoundExpression(
+                        [
+                            MatchExpression(
+                                self.service.fieldName.fullNames, u"test",
+                                matchType=MatchType.contains
+                            ),
+                            MatchExpression(
+                                self.service.fieldName.emailAddresses, u"test",
+                                matchType=MatchType.startsWith
+                            ),
+                        ],
+                        Operand.OR
+                    ),
+                ],
+                Operand.AND
+            )
+            records = yield self.service.recordsFromExpression(
+                expression, recordTypes=[self.service.recordType.user]
+            )
+
+            # We should get back users but not groups:
+            self.verifyResults(
+                records,
+                ["odtestbetty", "odtestalbert", "anotherodtestalbert"],
+                ["odtestamanda", "odtestbill", "odtestgroupa", "odtestgroupb"]
+            )
+
+
+        @onlyIfPopulated
+        @inlineCallbacks
+        def test_compoundWithMultipleExplicitRecordTypes(self):
+            expression = CompoundExpression(
+                [
+                    CompoundExpression(
+                        [
+                            MatchExpression(
+                                self.service.fieldName.fullNames, u"be",
+                                matchType=MatchType.contains
+                            ),
+                            MatchExpression(
+                                self.service.fieldName.emailAddresses, u"be",
+                                matchType=MatchType.startsWith
+                            ),
+                        ],
+                        Operand.OR
+                    ),
+                    CompoundExpression(
+                        [
+                            MatchExpression(
+                                self.service.fieldName.fullNames, u"test",
+                                matchType=MatchType.contains
+                            ),
+                            MatchExpression(
+                                self.service.fieldName.emailAddresses, u"test",
+                                matchType=MatchType.startsWith
+                            ),
+                        ],
+                        Operand.OR
+                    ),
+                ],
+                Operand.AND
+            )
+            records = yield self.service.recordsFromExpression(
+                expression, recordTypes=[
+                    self.service.recordType.user,
+                    self.service.recordType.group
+                ]
+            )
+
+            # We should get back users and groups:
+            self.verifyResults(
+                records,
+                [
+                    "odtestbetty", "odtestalbert", "anotherodtestalbert",
+                    "odtestgroupbetty", "odtestgroupalbert"
+                ],
+                ["odtestamanda", "odtestbill", "odtestgroupa", "odtestgroupb"]
+            )
+
+
+        @onlyIfPopulated
+        @inlineCallbacks
+        def test_compoundWithEmbeddedSingleRecordType(self):
+            expression = CompoundExpression(
+                [
+                    CompoundExpression(
+                        [
                             CompoundExpression(
                                 [
                                     MatchExpression(
@@ -207,19 +317,17 @@
                 ],
                 Operand.AND
             )
-            records = yield self.service.recordsFromExpression(expression)
+            try:
+                records = yield self.service.recordsFromExpression(expression)
+            except QueryNotSupportedError:
+                pass
+            else:
+                self.fail("This should have raised")
 
-            # We should only get users back, not groups:
-            self.verifyResults(
-                records,
-                ["odtestbetty", "odtestalbert", "anotherodtestalbert"],
-                ["odtestamanda", "odtestbill", "odtestgroupa", "odtestgroupb", "odtestgroupbetty", "odtestgroupalbert"]
-            )
 
-
         @onlyIfPopulated
         @inlineCallbacks
-        def test_compoundWithMultipleRecordTypes(self):
+        def test_compoundWithEmbeddedMultipleRecordTypes(self):
             expression = CompoundExpression(
                 [
                     CompoundExpression(
@@ -267,13 +375,111 @@
                 ],
                 Operand.AND
             )
-            records = yield self.service.recordsFromExpression(expression)
 
-            # We should get users and groups back, since we asked for either type:
+            try:
+                records = yield self.service.recordsFromExpression(expression)
+            except QueryNotSupportedError:
+                pass
+            else:
+                self.fail("This should have raised")
+
+
+        @onlyIfPopulated
+        @inlineCallbacks
+        def test_recordsMatchingTokens(self):
+            self.calService = CalOpenDirectoryService()
+            records = yield self.calService.recordsMatchingTokens([u"be", u"test"])
             self.verifyResults(
                 records,
-                ["odtestbetty", "odtestalbert", "anotherodtestalbert", "odtestgroupbetty", "odtestgroupalbert"],
+                [
+                    "odtestbetty", "odtestalbert", "anotherodtestalbert",
+                    "odtestgroupbetty", "odtestgroupalbert"
+                ],
                 ["odtestamanda", "odtestbill", "odtestgroupa", "odtestgroupb"]
             )
 
-        test_compoundWithMultipleRecordTypes.skip = "This ends up doing a brute force query!"
+
+        @onlyIfPopulated
+        @inlineCallbacks
+        def test_recordsMatchingTokensWithContextUser(self):
+            self.calService = CalOpenDirectoryService()
+            records = yield self.calService.recordsMatchingTokens(
+                [u"be", u"test"],
+                context=self.calService.searchContext_user
+            )
+            self.verifyResults(
+                records,
+                [
+                    "odtestbetty", "odtestalbert", "anotherodtestalbert",
+                ],
+                [
+                    "odtestamanda", "odtestbill", "odtestgroupa", "odtestgroupb",
+                    "odtestgroupbetty", "odtestgroupalbert"
+                ]
+            )
+
+
+        @onlyIfPopulated
+        @inlineCallbacks
+        def test_recordsMatchingTokensWithContextGroup(self):
+            self.calService = CalOpenDirectoryService()
+            records = yield self.calService.recordsMatchingTokens(
+                [u"be", u"test"],
+                context=self.calService.searchContext_group
+            )
+            self.verifyResults(
+                records,
+                [
+                    "odtestgroupbetty", "odtestgroupalbert"
+                ],
+                [
+                    "odtestamanda", "odtestbill", "odtestgroupa", "odtestgroupb",
+                    "odtestbetty", "odtestalbert", "anotherodtestalbert"
+                ]
+            )
+
+
+        @onlyIfPopulated
+        @inlineCallbacks
+        def test_recordsMatchingFieldsNoRecordType(self):
+            self.calService = CalOpenDirectoryService()
+            fields = (
+                (u"fullNames", u"be", MatchFlags.caseInsensitive, MatchType.contains),
+                (u"fullNames", u"test", MatchFlags.caseInsensitive, MatchType.contains),
+            )
+            records = (yield self.calService.recordsMatchingFields(
+                fields, operand=Operand.AND, recordType=None
+            ))
+            self.verifyResults(
+                records,
+                [
+                    "odtestgroupbetty", "odtestgroupalbert",
+                    "odtestbetty", "odtestalbert", "anotherodtestalbert"
+                ],
+                [
+                    "odtestamanda",
+                ]
+            )
+
+
+        @onlyIfPopulated
+        @inlineCallbacks
+        def test_recordsMatchingFieldsWithRecordType(self):
+            self.calService = CalOpenDirectoryService()
+            fields = (
+                (u"fullNames", u"be", MatchFlags.caseInsensitive, MatchType.contains),
+                (u"fullNames", u"test", MatchFlags.caseInsensitive, MatchType.contains),
+            )
+            records = (yield self.calService.recordsMatchingFields(
+                fields, operand=Operand.AND, recordType=self.calService.recordType.user
+            ))
+            self.verifyResults(
+                records,
+                [
+                    "odtestbetty", "odtestalbert", "anotherodtestalbert"
+                ],
+                [
+                    "odtestamanda", "odtestgroupalbert", "odtestgroupbetty",
+                ]
+            )
+

Modified: CalendarServer/branches/users/sagen/recordtypes/requirements-stable.txt
===================================================================
--- CalendarServer/branches/users/sagen/recordtypes/requirements-stable.txt	2014-06-18 20:26:18 UTC (rev 13652)
+++ CalendarServer/branches/users/sagen/recordtypes/requirements-stable.txt	2014-06-18 20:27:26 UTC (rev 13653)
@@ -5,7 +5,7 @@
 # For CalendarServer development, don't try to get these projects from PyPI; use svn.
 
 -e .
--e svn+http://svn.calendarserver.org/repository/calendarserver/twext/branches/users/sagen/recordtypes@13649#egg=twextpy
+-e svn+http://svn.calendarserver.org/repository/calendarserver/twext/branches/users/sagen/recordtypes@13652#egg=twextpy
 -e svn+http://svn.calendarserver.org/repository/calendarserver/PyKerberos/trunk@13420#egg=kerberos
 -e svn+http://svn.calendarserver.org/repository/calendarserver/PyCalendar/trunk@13621#egg=pycalendar
 

Modified: CalendarServer/branches/users/sagen/recordtypes/txdav/who/directory.py
===================================================================
--- CalendarServer/branches/users/sagen/recordtypes/txdav/who/directory.py	2014-06-18 20:26:18 UTC (rev 13652)
+++ CalendarServer/branches/users/sagen/recordtypes/txdav/who/directory.py	2014-06-18 20:27:26 UTC (rev 13653)
@@ -182,36 +182,16 @@
             # CompoundExpression that ANDs the original expression with a
             # typeSpecific one.  Collect all the results from these expressions.
             recordTypes = self.recordTypesForSearchContext(context)
-            log.debug("Tokens: {t}, recordTypes {r}", t=tokens, r=recordTypes)
-            for recordType in recordTypes:
-                typeSpecific = MatchExpression(
-                    self.fieldName.recordType,
-                    recordType,
-                    MatchType.equals,
-                    MatchFlags.none
-                )
-
-                typeSpecific = CompoundExpression(
-                    [expression, typeSpecific],
-                    Operand.AND
-                )
-
-                subResults = yield self.recordsFromExpression(typeSpecific)
-                log.debug(
-                    "Tokens ({t}) matched {n} of {r}",
-                    t=tokens, n=len(subResults), r=recordType
-                )
-                results.extend(subResults)
-
         else:
-            # No record type limits
-            results = yield self.recordsFromExpression(expression)
-            log.debug(
-                "Tokens ({t}) matched {n} records",
-                t=tokens, n=len(results)
-            )
+            recordTypes = None
 
-        log.debug("Tokens ({t}) matched records {r}", t=tokens, r=results)
+        results = yield self.recordsFromExpression(
+            expression, recordTypes=recordTypes
+        )
+        log.debug(
+            "Tokens ({t}) matched {n} records",
+            t=tokens, n=len(results)
+        )
 
         returnValue(results)
 
@@ -254,37 +234,15 @@
             )
             subExpressions.append(subExpression)
 
-        if len(subExpressions) == 1:
-            # FIXME: twext.who.opendirectory takes a CompoundExpression and
-            # inspects it for MatchExpressions on recordType, and pulls those
-            # out of the query structure.  However, if all that remains is
-            # essentially a MatchExpression, but we tell OD to use
-            # ODMatchType.compound for it, we get no results back.  Until we
-            # fix twext.who, this workaround keeds the query compound even
-            # when the record type portion is removed:
-            expression = CompoundExpression(
-                [subExpressions[0], subExpressions[0]], operand
-            )
-        else:
-            expression = CompoundExpression(subExpressions, operand)
+        expression = CompoundExpression(subExpressions, operand)
 
-        # AND in the recordType if passed in
         if recordType is not None:
-            typeExpression = MatchExpression(
-                self.fieldName.recordType,
-                recordType,
-                MatchType.equals,
-                MatchFlags.none
-            )
-            expression = CompoundExpression(
-                [
-                    expression,
-                    typeExpression
-                ],
-                Operand.AND
-            )
-        return self.recordsFromExpression(expression)
+            recordTypes = [recordType]
+        else:
+            recordTypes = None
+        return self.recordsFromExpression(expression, recordTypes=recordTypes)
 
+
     _oldRecordTypeNames = {
         "address": "addresses",
         "group": "groups",
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140618/e4e2bd66/attachment-0001.html>


More information about the calendarserver-changes mailing list