X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=ldapdb%2Fmodels%2Fquery.py;h=5f522a61cd17b43cf94a91af28ce00dad7a72cf8;hb=23a36d706cedeed4d02d0f97b46669403ce5b943;hp=463ecf6b2305e0bb74975652801c2c536a817be6;hpb=85099b38b796976f9a9f3a86f094e37548fd47f8;p=matthijs%2Fupstream%2Fdjango-ldapdb.git diff --git a/ldapdb/models/query.py b/ldapdb/models/query.py index 463ecf6..5f522a6 100644 --- a/ldapdb/models/query.py +++ b/ldapdb/models/query.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # django-ldapdb -# Copyright (C) 2009 Bolloré telecom +# Copyright (C) 2009-2010 Bolloré telecom # See AUTHORS file for a full list of contributors. # # This program is free software: you can redistribute it and/or modify @@ -21,7 +21,6 @@ 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 @@ -29,6 +28,8 @@ from django.db.models.sql.where import WhereNode as BaseWhereNode, Constraint as import ldapdb +from ldapdb import escape_ldap_filter + class Constraint(BaseConstraint): """ An object that can be passed to WhereNode.add() and knows how to @@ -43,11 +44,15 @@ class Constraint(BaseConstraint): from django.db.models.base import ObjectDoesNotExist if lookup_type == 'endswith': - params = ["*%s" % value] + params = ["*%s" % escape_ldap_filter(value)] elif lookup_type == 'startswith': - params = ["%s*" % value] + params = ["%s*" % escape_ldap_filter(value)] + elif lookup_type == 'contains': + params = ["*%s*" % escape_ldap_filter(value)] elif lookup_type == 'exact': - params = [value] + 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) @@ -79,17 +84,18 @@ class WhereNode(BaseWhereNode): if isinstance(item, WhereNode): bits.append(item.as_sql()) continue - if len(item) == 4: - # django 1.1 - (table, column, type), x, y, values = item - else: - # django 1.0 - 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: