try to add support for endswith and startswith
authorjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Wed, 19 May 2010 07:50:04 +0000 (07:50 +0000)
committerjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Wed, 19 May 2010 07:50:04 +0000 (07:50 +0000)
git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@844 e071eeec-0327-468d-9b6a-08194a12b294

ldapdb/models/query.py
ldapdb/tests.py

index a3a960886050664ae035384e3a9a9d10ac986043..463ecf6b2305e0bb74975652801c2c536a817be6 100644 (file)
 from copy import deepcopy
 import ldap
 
+from django.db.models.fields import Field
 from django.db.models.query import QuerySet as BaseQuerySet
 from django.db.models.query_utils import Q
 from django.db.models.sql import Query as BaseQuery
-from django.db.models.sql.where import WhereNode as BaseWhereNode, AND, OR
+from django.db.models.sql.where import WhereNode as BaseWhereNode, Constraint as BaseConstraint, AND, OR
 
 import ldapdb
 
+class Constraint(BaseConstraint):
+    """
+    An object that can be passed to WhereNode.add() and knows how to
+    pre-process itself prior to including in the WhereNode.
+    """
+    def process(self, lookup_type, value):
+        """
+        Returns a tuple of data suitable for inclusion in a WhereNode
+        instance.
+        """
+        # Because of circular imports, we need to import this here.
+        from django.db.models.base import ObjectDoesNotExist
+
+        if lookup_type == 'endswith':
+            params = ["*%s" % value]
+        elif lookup_type == 'startswith':
+            params = ["%s*" % value]
+        elif lookup_type == 'exact':
+            params = [value]
+        else:
+            raise TypeError("Field has invalid lookup: %s" % lookup_type)
+
+        try:
+            if self.field:
+                db_type = self.field.db_type()
+            else:
+                db_type = None
+        except ObjectDoesNotExist:
+            raise EmptyShortCircuit
+
+        return (self.alias, self.col, db_type), params
+
 class WhereNode(BaseWhereNode):
+    def add(self, data, connector):
+        if not isinstance(data, (list, tuple)):
+            super(WhereNode, self).add(data, connector)
+            return
+
+        # we replace the native Constraint by our own
+        obj, lookup_type, value = data
+        if hasattr(obj, "process"):
+            obj = Constraint(obj.alias, obj.col, obj.field)
+        super(WhereNode, self).add((obj, lookup_type, value), connector)
+
     def as_sql(self):
         bits = []
         for item in self.children:
index 396d2180690ecbae054c956517599dbbabc5ab87..806c764b95dd56a6fe545ba1dfeb33261a993d36 100644 (file)
@@ -29,6 +29,14 @@ class WhereTestCase(TestCase):
         where.add((Constraint("cn", "cn", None), 'exact', "test"), AND)
         self.assertEquals(where.as_sql(), "(cn=test)")
 
+        where = WhereNode()
+        where.add((Constraint("cn", "cn", None), 'startswith', "test"), AND)
+        self.assertEquals(where.as_sql(), "(cn=test*)")
+
+        where = WhereNode()
+        where.add((Constraint("cn", "cn", None), 'endswith', "test"), AND)
+        self.assertEquals(where.as_sql(), "(cn=*test)")
+
     def test_and(self):
         where = WhereNode()
         where.add((Constraint("cn", "cn", None), 'exact', "foo"), AND)