fix lookups with django 1.2
authorjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 24 May 2010 09:05:29 +0000 (09:05 +0000)
committerjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 24 May 2010 09:05:29 +0000 (09:05 +0000)
git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@866 e071eeec-0327-468d-9b6a-08194a12b294

ldapdb/models/fields.py
ldapdb/models/query.py

index d21bbd0dc258c31bc05a35b8a1ee2d7a0b69eb07..921923c052469ac826248bde9743b57d48c66ed6 100644 (file)
@@ -28,6 +28,7 @@ class CharField(fields.CharField):
         super(CharField, self).__init__(*args, **kwargs)
 
     def get_db_prep_lookup(self, lookup_type, value):
+        "Returns field's value prepared for database lookup."
         if lookup_type == 'endswith':
             return ["*%s" % escape_ldap_filter(value)]
         elif lookup_type == 'startswith':
@@ -42,8 +43,20 @@ class CharField(fields.CharField):
         raise TypeError("CharField has invalid lookup: %s" % lookup_type)
 
     def get_prep_lookup(self, lookup_type, value):
-        return escape_ldap_filter(value)
-        
+        "Perform preliminary non-db specific lookup checks and conversions"
+        if lookup_type == 'endswith':
+            return "*%s" % escape_ldap_filter(value)
+        elif lookup_type == 'startswith':
+            return "%s*" % escape_ldap_filter(value)
+        elif lookup_type == 'contains':
+            return "*%s*" % escape_ldap_filter(value)
+        elif lookup_type == 'exact':
+            return escape_ldap_filter(value)
+        elif lookup_type == 'in':
+            return [escape_ldap_filter(v) for v in value]
+
+        raise TypeError("CharField has invalid lookup: %s" % lookup_type)
+
 class ImageField(fields.Field):
     def get_db_prep_lookup(self, lookup_type, value):
         raise TypeError("ImageField has invalid lookup: %s" % lookup_type)
index b8e4897a645e1132424c1b735aff28928235408d..738124f0db7f2654613bfd5c0f85612e748eb689 100644 (file)
@@ -81,11 +81,17 @@ class WhereNode(BaseWhereNode):
             if isinstance(item, WhereNode):
                 bits.append(item.as_sql())
                 continue
+
             constraint, lookup_type, y, values = item
             comp = get_lookup_operator(lookup_type)
             if hasattr(constraint, "col"):
                 # django 1.2
-                clause = "(%s%s%s)" % (constraint.col, comp, values)
+                column = constraint.col
+                if lookup_type == 'in':
+                    equal_bits = [ "(%s%s%s)" % (column, comp, value) for value in values ]
+                    clause = '(|%s)' % ''.join(equal_bits)
+                else:
+                    clause = "(%s%s%s)" % (constraint.col, comp, values)
             else:
                 # django 1.1
                 (table, column, db_type) = constraint
@@ -94,6 +100,7 @@ class WhereNode(BaseWhereNode):
                     clause = equal_bits[0]
                 else:
                     clause = '(|%s)' % ''.join(equal_bits)
+
             if self.negated:
                 bits.append('(!%s)' % clause)
             else: