From c92a7be6f574fceac92e13b5805aebdbc31c281e Mon Sep 17 00:00:00 2001 From: jlaine Date: Mon, 24 May 2010 06:27:53 +0000 Subject: [PATCH] django 1.2 compatibility fixes git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@853 e071eeec-0327-468d-9b6a-08194a12b294 --- ldapdb/__init__.py | 10 +++++++++- ldapdb/models/fields.py | 5 +++++ ldapdb/models/query.py | 24 ++++++++++++------------ ldapdb/tests.py | 3 ++- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/ldapdb/__init__.py b/ldapdb/__init__.py index 60b6c86..07db7bb 100644 --- a/ldapdb/__init__.py +++ b/ldapdb/__init__.py @@ -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) diff --git a/ldapdb/models/fields.py b/ldapdb/models/fields.py index 78ec1c7..1a37fef 100644 --- a/ldapdb/models/fields.py +++ b/ldapdb/models/fields.py @@ -20,11 +20,16 @@ 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 diff --git a/ldapdb/models/query.py b/ldapdb/models/query.py index ae4b73c..277fec9 100644 --- a/ldapdb/models/query.py +++ b/ldapdb/models/query.py @@ -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: diff --git a/ldapdb/tests.py b/ldapdb/tests.py index e3f1410..fdcba59 100644 --- a/ldapdb/tests.py +++ b/ldapdb/tests.py @@ -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): -- 2.30.2