[CalendarServer-changes] [3048] CalendarServer/branches/users/sagen/principal-property-search-3029/ twistedcaldav/directory

source_changes at macosforge.org source_changes at macosforge.org
Wed Sep 24 13:14:03 PDT 2008


Revision: 3048
          http://trac.macosforge.org/projects/calendarserver/changeset/3048
Author:   sagen at apple.com
Date:     2008-09-24 13:14:03 -0700 (Wed, 24 Sep 2008)
Log Message:
-----------
Adds "caseless" (yes/no) and "match-type" (starts-with/contains) XML attributes to "match" element

Modified Paths:
--------------
    CalendarServer/branches/users/sagen/principal-property-search-3029/twistedcaldav/directory/aggregate.py
    CalendarServer/branches/users/sagen/principal-property-search-3029/twistedcaldav/directory/appleopendirectory.py
    CalendarServer/branches/users/sagen/principal-property-search-3029/twistedcaldav/directory/directory.py

Modified: CalendarServer/branches/users/sagen/principal-property-search-3029/twistedcaldav/directory/aggregate.py
===================================================================
--- CalendarServer/branches/users/sagen/principal-property-search-3029/twistedcaldav/directory/aggregate.py	2008-09-24 20:13:34 UTC (rev 3047)
+++ CalendarServer/branches/users/sagen/principal-property-search-3029/twistedcaldav/directory/aggregate.py	2008-09-24 20:14:03 UTC (rev 3048)
@@ -103,8 +103,7 @@
     def recordWithCalendarUserAddress(self, address):
         return self._queryAll("recordWithCalendarUserAddress", address)
 
-    def recordsMatchingFields(self, fields, caseInsensitive=True, operand="or",
-        recordType=None):
+    def recordsMatchingFields(self, fields, operand="or", recordType=None):
         if recordType:
             services = (self.serviceForRecordType(recordType),)
         else:
@@ -112,8 +111,7 @@
 
         for service in services:
             for record in service.recordsMatchingFields(fields,
-                caseInsensitive=caseInsensitive, operand=operand,
-                recordType=recordType):
+                operand=operand, recordType=recordType):
                     yield record
 
     def serviceForRecordType(self, recordType):

Modified: CalendarServer/branches/users/sagen/principal-property-search-3029/twistedcaldav/directory/appleopendirectory.py
===================================================================
--- CalendarServer/branches/users/sagen/principal-property-search-3029/twistedcaldav/directory/appleopendirectory.py	2008-09-24 20:13:34 UTC (rev 3047)
+++ CalendarServer/branches/users/sagen/principal-property-search-3029/twistedcaldav/directory/appleopendirectory.py	2008-09-24 20:14:03 UTC (rev 3048)
@@ -481,17 +481,23 @@
 
     _fromODRecordTypes = dict([(b, a) for a, b in _toODRecordTypes.iteritems()])
 
-    def recordsMatchingFields(self, fields, caseInsensitive=True, operand="or",
-        recordType=None):
+    def recordsMatchingFields(self, fields, operand="or", recordType=None):
 
-        comparison = dsattributes.eDSStartsWith
+        # Note that OD applies case-sensitivity globally across the entire
+        # query, not per expression, so the current code uses whatever is
+        # specified in the last field in the fields list
+
         operand = (dsquery.expression.OR if operand == "or"
             else dsquery.expression.AND)
 
         expressions = []
-        for field, value in fields:
+        for field, value, caseless, matchType in fields:
             if field in self._ODFields:
                 ODField = self._ODFields[field]
+                if matchType == "starts-with":
+                    comparison = dsattributes.eDSStartsWith
+                else:
+                    comparison = dsattributes.eDSContains
                 expressions.append(dsquery.match(ODField, value, comparison))
 
 
@@ -503,12 +509,11 @@
         for recordType in recordTypes:
 
             try:
-                self.log_info("Calling OD: %s %s %s" % (recordType, operand,
-                    fields))
+                self.log_info("Calling OD: Type %s, Operand %s, Caseless %s, %s" % (recordType, operand, caseless, fields))
                 results = opendirectory.queryRecordsWithAttributes(
                     self.directory,
                     dsquery.expression(operand, expressions).generate(),
-                    caseInsensitive,
+                    caseless,
                     recordType,
                     [
                         dsattributes.kDS1AttrGeneratedUID,

Modified: CalendarServer/branches/users/sagen/principal-property-search-3029/twistedcaldav/directory/directory.py
===================================================================
--- CalendarServer/branches/users/sagen/principal-property-search-3029/twistedcaldav/directory/directory.py	2008-09-24 20:13:34 UTC (rev 3047)
+++ CalendarServer/branches/users/sagen/principal-property-search-3029/twistedcaldav/directory/directory.py	2008-09-24 20:14:03 UTC (rev 3048)
@@ -138,8 +138,7 @@
             for record in self.listRecords(recordType):
                 yield record
 
-    def recordsMatchingFields(self, fields, caseInsensitive=True, operand="or",
-        recordType=None):
+    def recordsMatchingFields(self, fields, operand="or", recordType=None):
         # Default, bruteforce method; override with one optimized for each
         # service
 
@@ -153,18 +152,27 @@
         else:
             recordTypes = (recordType,)
 
-        def fieldMatches(fieldValue, value):
-            if caseInsensitive:
+        def fieldMatches(fieldValue, value, caseless, matchType):
+            if caseless:
+                fieldValue = fieldValue.lower()
+                value = value.lower()
+
+            if matchType == 'starts-with':
                 return fieldValue.lower().startswith(value.lower())
             else:
-                return fieldValue.startswith(value)
+                try:
+                    discard = fieldValue.lower().index(value.lower())
+                    return True
+                except ValueError:
+                    return False
 
         def recordMatches(record):
             if operand == "and":
-                for fieldName, value in fields:
+                for fieldName, value, caseless, matchType in fields:
                     try:
                         fieldValue = getattr(record, fieldName)
-                        if not fieldMatches(fieldValue, value):
+                        if not fieldMatches(fieldValue, value, caseless,
+                            matchType):
                             return False
                     except AttributeError:
                         # No property => no match
@@ -172,10 +180,11 @@
                 # we hit on every property
                 return True
             else: # "or"
-                for fieldName, value in fields:
+                for fieldName, value, caseless, matchType in fields:
                     try:
                         fieldValue = getattr(record, fieldName)
-                        if fieldMatches(fieldValue, value):
+                        if fieldMatches(fieldValue, value, caseless,
+                            matchType):
                             return True
                     except AttributeError:
                         # No value
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20080924/cecfb30b/attachment-0001.html 


More information about the calendarserver-changes mailing list