X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=ldapdb%2Fmodels%2Fquery.py;h=020d4eeb7d4a9bf3d03c6242ad296ade8789e0a2;hb=3912bd6478e2c17a4ea171840a50d1d78ca2694c;hp=938abd5904aac6cf79230c134fd7b7f8d8958b04;hpb=5a80e0dc0886927f14b00ccf329fa4ec6d36ffa5;p=matthijs%2Fupstream%2Fdjango-ldapdb.git diff --git a/ldapdb/models/query.py b/ldapdb/models/query.py index 938abd5..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 @@ -18,42 +18,113 @@ # 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 BaseQuery -from django.db.models.sql.where import WhereNode +from django.db.models.sql import Query as BaseQuery +from django.db.models.sql.where import WhereNode as BaseWhereNode, Constraint as BaseConstraint, AND, OR import ldapdb -def compile(q): - filterstr = "" - for item in q.children: - if isinstance(item, WhereNode): - filterstr += compile(item) - continue - table, column, type, x, y, values = item - if q.negated: - filterstr += "(!(%s=%s))" % (column,values[0]) +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 + 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 + + 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 + + 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 + + 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: + # 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: + bits.append(clause) + if len(bits) == 1: + return bits[0] + elif self.connector == AND: + return '(&%s)' % ''.join(bits) + elif self.connector == OR: + return '(|%s)' % ''.join(bits) else: - filterstr += "(%s=%s)" % (column,values[0]) - return filterstr + raise Exception("Unhandled WHERE connector: %s" % self.connector) class Query(BaseQuery): def results_iter(self): # FIXME: use all object classes - filterstr = '(objectClass=%s)' % self.model._meta.object_classes[0] - filterstr += compile(self.where) + filterstr = '(objectClass=%s)' % self.model.object_classes[0] + filterstr += self.where.as_sql() filterstr = '(&%s)' % filterstr attrlist = [ x.db_column for x in self.model._meta.local_fields if x.db_column ] try: vals = ldapdb.connection.search_s( - self.model._meta.dn, + self.model.base_dn, ldap.SCOPE_SUBTREE, filterstr=filterstr, attrlist=attrlist, @@ -72,20 +143,30 @@ 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)) # process results for dn, attrs in vals: - row = [dn] + row = [] for field in iter(self.model._meta.fields): - row.append(attrs.get(field.db_column, None)) + if field.attname == 'dn': + row.append(dn) + else: + row.append(attrs.get(field.db_column, None)) 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) + 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)