X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=ldapdb%2Fmodels%2Fquery.py;h=020d4eeb7d4a9bf3d03c6242ad296ade8789e0a2;hb=3912bd6478e2c17a4ea171840a50d1d78ca2694c;hp=49c183e5432f900bde835098917f9197df0cc251;hpb=45c9e99629c4af8f559b64ee57c3527af628032c;p=matthijs%2Fupstream%2Fdjango-ldapdb.git diff --git a/ldapdb/models/query.py b/ldapdb/models/query.py index 49c183e..020d4ee 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 @@ -28,13 +28,15 @@ 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.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): """ @@ -49,23 +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" % 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: + 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 @@ -90,17 +81,26 @@ 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, lookup_type, y, values = item + comp = get_lookup_operator(lookup_type) + if hasattr(constraint, "col"): + # django 1.2 + 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: - clause = '(|%s)' % ''.join(equal_bits) + # django 1.1 + (table, column, db_type) = constraint + 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: @@ -158,8 +158,15 @@ class Query(BaseQuery): yield row class QuerySet(BaseQuerySet): - def __init__(self, model=None, query=None): + def __init__(self, model=None, query=None, using=None): if not query: - query = Query(model, None, WhereNode) + import inspect + spec = inspect.getargspec(Query.__init__) + if len(spec[0]) == 3: + # django 1.2 + query = Query(model, WhereNode) + else: + # django 1.1 + query = Query(model, None, WhereNode) super(QuerySet, self).__init__(model, query)