X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=ldapdb%2Fmodels%2Fquery.py;h=a8c614f7ccfe6a2ee487ac35b38bb474951efa0c;hb=ab216ac000ca0b6aeea64d9ce112395bbb7962d5;hp=463ecf6b2305e0bb74975652801c2c536a817be6;hpb=85099b38b796976f9a9f3a86f094e37548fd47f8;p=matthijs%2Fupstream%2Fdjango-ldapdb.git diff --git a/ldapdb/models/query.py b/ldapdb/models/query.py index 463ecf6..a8c614f 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,16 @@ from django.db.models.sql.where import WhereNode as BaseWhereNode, Constraint as import ldapdb +from ldapdb.models.fields import CharField + +def get_lookup_operator(lookup_type): + if lookup_type == 'gte': + return '>=' + elif lookup_type == 'lte': + return '<=' + else: + return '=' + class Constraint(BaseConstraint): """ An object that can be passed to WhereNode.add() and knows how to @@ -42,19 +51,12 @@ class Constraint(BaseConstraint): # Because of circular imports, we need to import this here. from django.db.models.base import ObjectDoesNotExist - if lookup_type == 'endswith': - params = ["*%s" % value] - elif lookup_type == 'startswith': - params = ["%s*" % value] - elif lookup_type == 'exact': - params = [value] - else: - raise TypeError("Field has invalid lookup: %s" % lookup_type) - try: if self.field: + params = self.field.get_db_prep_lookup(lookup_type, value) db_type = self.field.db_type() else: + params = CharField().get_db_prep_lookup(lookup_type, value) db_type = None except ObjectDoesNotExist: raise EmptyShortCircuit @@ -79,17 +81,20 @@ 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 + comp = get_lookup_operator(constraint.lookup_type) + clause = "(%s%s%s)" % (constraint.col, comp, values) else: - clause = '(|%s)' % ''.join(equal_bits) + # django 1.1 + (table, column, db_type) = constraint + comp = get_lookup_operator(x) + equal_bits = [ "(%s%s%s)" % (column, comp, 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: