rework field preparation
authorjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 24 May 2010 07:41:43 +0000 (07:41 +0000)
committerjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 24 May 2010 07:41:43 +0000 (07:41 +0000)
git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@860 e071eeec-0327-468d-9b6a-08194a12b294

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

index 6162c7f5a21bdca57be1e4d403d540446b799803..f269d7c126f42fb9b829210094a9e08e5b9ef504 100644 (file)
@@ -27,6 +27,20 @@ class CharField(fields.CharField):
         kwargs['max_length'] = 200
         super(CharField, self).__init__(*args, **kwargs)
 
+    def get_db_prep_lookup(self, lookup_type, value):
+        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)
+
     def get_prep_lookup(self, lookup_type, value):
         return escape_ldap_filter(value)
         
@@ -34,7 +48,11 @@ class ImageField(fields.Field):
     pass
 
 class IntegerField(fields.IntegerField):
-    pass
+    def get_db_prep_lookup(self, lookup_type, value):
+        if lookup_type == 'exact':
+            return [value]
+
+        raise TypeError("IntegerField has invalid lookup: %s" % lookup_type)
 
 class ListField(fields.Field):
     __metaclass__ = SubfieldBase
index 5f522a61cd17b43cf94a91af28ce00dad7a72cf8..67d193607b7bc77c09ac74c4a802388a185df6f1 100644 (file)
@@ -28,7 +28,7 @@ from django.db.models.sql.where import WhereNode as BaseWhereNode, Constraint as
 
 import ldapdb
 
-from ldapdb import escape_ldap_filter
+from ldapdb.models.fields import CharField
 
 class Constraint(BaseConstraint):
     """
@@ -43,23 +43,12 @@ class Constraint(BaseConstraint):
         # Because of circular imports, we need to import this here.
         from django.db.models.base import ObjectDoesNotExist
 
-        if lookup_type == 'endswith':
-            params = ["*%s" % escape_ldap_filter(value)]
-        elif lookup_type == 'startswith':
-            params = ["%s*" % escape_ldap_filter(value)]
-        elif lookup_type == 'contains':
-            params = ["*%s*" % escape_ldap_filter(value)]
-        elif lookup_type == 'exact':
-            params = [escape_ldap_filter(value)]
-        elif lookup_type == 'in':
-            params = [escape_ldap_filter(v) for v in value]
-        else:
-            raise TypeError("Field has invalid lookup: %s" % lookup_type)
-
         try:
             if self.field:
+                params = self.field.get_db_prep_lookup(lookup_type, value)
                 db_type = self.field.db_type()
             else:
+                params = CharField().get_db_prep_lookup(lookup_type, value)
                 db_type = None
         except ObjectDoesNotExist:
             raise EmptyShortCircuit