X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=ldapdb%2Fmodels%2Fquery.py;h=5f522a61cd17b43cf94a91af28ce00dad7a72cf8;hb=23a36d706cedeed4d02d0f97b46669403ce5b943;hp=7d952d45b23aca76514f787f7c22e87d9498345d;hpb=81037a75f672596982475a7081946bfd9b4fd286;p=matthijs%2Fupstream%2Fdjango-ldapdb.git diff --git a/ldapdb/models/query.py b/ldapdb/models/query.py index 7d952d4..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 @@ -18,35 +18,88 @@ # along with this program. If not, see . # -# -*- coding: utf-8 -*- - from copy import deepcopy import ldap 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 +from ldapdb import escape_ldap_filter + +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" % escape_ldap_filter(value)] + elif lookup_type == 'startswith': + params = ["%s*" % escape_ldap_filter(value)] + elif lookup_type == 'contains': + params = ["*%s*" % escape_ldap_filter(value)] + elif lookup_type == 'exact': + 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) + + 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: if isinstance(item, WhereNode): bits.append(item.as_sql()) continue - if len(item) == 4: - # django 1.1 - (table, column, type), x, y, values = item + constraint, x, y, values = item + if hasattr(constraint, "col"): + # django 1.2 + clause = "(%s=%s)" % (constraint.col, values) else: - # django 1.0 - table, column, type, x, y, values = item + # 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=%s))' % (column,values[0])) + bits.append('(!%s)' % clause) else: - bits.append('(%s=%s)' % (column,values[0])) + bits.append(clause) if len(bits) == 1: return bits[0] elif self.connector == AND: @@ -85,7 +138,7 @@ class Query(BaseQuery): keys = [] for k in ordering: attr = self.model._meta.get_field(k).db_column - keys.append(x[1][attr]) + keys.append(x[1].get(attr, '').lower()) return keys vals = sorted(vals, key=lambda x: getkey(x))