From 29c4b4ac0569ebb0877aa9b32211dc91afb248c5 Mon Sep 17 00:00:00 2001 From: jlaine Date: Mon, 24 May 2010 09:05:29 +0000 Subject: [PATCH] fix lookups with django 1.2 git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@866 e071eeec-0327-468d-9b6a-08194a12b294 --- ldapdb/models/fields.py | 17 +++++++++++++++-- ldapdb/models/query.py | 9 ++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/ldapdb/models/fields.py b/ldapdb/models/fields.py index d21bbd0..921923c 100644 --- a/ldapdb/models/fields.py +++ b/ldapdb/models/fields.py @@ -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) diff --git a/ldapdb/models/query.py b/ldapdb/models/query.py index b8e4897..738124f 100644 --- a/ldapdb/models/query.py +++ b/ldapdb/models/query.py @@ -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: -- 2.30.2