django 1.2 compatibility fixes
authorjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 24 May 2010 06:27:53 +0000 (06:27 +0000)
committerjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 24 May 2010 06:27:53 +0000 (06:27 +0000)
git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@853 e071eeec-0327-468d-9b6a-08194a12b294

ldapdb/__init__.py
ldapdb/models/fields.py
ldapdb/models/query.py
ldapdb/tests.py

index 60b6c865710b64130469fe4f51f4a8194b1ade6f..07db7bb587b2fca5da7b0cca6cca8384efd482a7 100644 (file)
@@ -31,7 +31,15 @@ def convert(field, value, func):
         return [ func(x) for x in value ]
     else:
         return func(value)
-        
+
+def escape_ldap_filter(value):
+    value = str(value)
+    return value.replace('\\', '\\5c') \
+                .replace('*', '\\2a') \
+                .replace('(', '\\28') \
+                .replace(')', '\\29') \
+                .replace('\0', '\\00')
+
 class LdapConnection(object):
     def __init__(self, server, bind_dn, bind_password):
         self.connection = ldap.initialize(server)
index 78ec1c7569f3421d2b856e2d78856f70cbed70da..1a37fefca40ff91445b53461d8c77ad2c875bba4 100644 (file)
 
 from django.db.models import fields, SubfieldBase
 
+from ldapdb import escape_ldap_filter
+
 class CharField(fields.CharField):
     def __init__(self, *args, **kwargs):
         kwargs['max_length'] = 200
         super(CharField, self).__init__(*args, **kwargs)
 
+    def get_prep_lookup(self, lookup_type, value):
+        return escape_ldap_filter(value)
+        
 class ImageField(fields.Field):
     pass
 
index ae4b73c7b559a3eac87e4953935b271c4a46bba1..277fec922dbea9704a36b32c84372620169974af 100644 (file)
@@ -28,13 +28,7 @@ from django.db.models.sql.where import WhereNode as BaseWhereNode, Constraint as
 
 import ldapdb
 
-def escape_ldap_filter(value):
-    value = str(value)
-    return value.replace('\\', '\\5c') \
-                .replace('*', '\\2a') \
-                .replace('(', '\\28') \
-                .replace(')', '\\29') \
-                .replace('\0', '\\00')
+from ldapdb import escape_ldap_filter
 
 class Constraint(BaseConstraint):
     """
@@ -90,12 +84,18 @@ class WhereNode(BaseWhereNode):
             if isinstance(item, WhereNode):
                 bits.append(item.as_sql())
                 continue
-            (table, column, type), x, y, values = item
-            equal_bits = [ "(%s=%s)" % (column, value) for value in values ]
-            if len(equal_bits) == 1:
-                clause = equal_bits[0]
+            constraint, x, y, values = item
+            if hasattr(constraint, "col"):
+                # django 1.2
+                clause = "(%s=%s)" % (constraint.col, values)
             else:
-                clause = '(|%s)' % ''.join(equal_bits)
+                # django 1.1
+                (table, column, type) = constraint
+                equal_bits = [ "(%s=%s)" % (column, value) for value in values ]
+                if len(equal_bits) == 1:
+                    clause = equal_bits[0]
+                else:
+                    clause = '(|%s)' % ''.join(equal_bits)
             if self.negated:
                 bits.append('(!%s)' % clause)
             else:
index e3f1410c2843cd4f1e4777a1518cae137d671a8b..fdcba594b9e1a56ac18067c45a42b017006bc1fd 100644 (file)
@@ -21,8 +21,9 @@
 from django.test import TestCase
 from django.db.models.sql.where import Constraint, AND, OR
 
+from ldapdb.models.query import escape_ldap_filter
 from ldapdb.models.fields import CharField, IntegerField
-from ldapdb.models.query import WhereNode, escape_ldap_filter
+from ldapdb.models.query import WhereNode
 
 class WhereTestCase(TestCase):
     def test_escape(self):