X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=ldapdb%2Fmodels%2Fquery.py;h=f430286b65be5d14531e46a86b540eaa3a851ab6;hb=aa99c514ad0457d05e95f48de4b0c855a75b363e;hp=367cd2cec1eadae0cdecc06d241f5f27bf0ed860;hpb=85ee2b0a3d2ee52fc31dd8991594c07ba5cfc4b1;p=matthijs%2Fupstream%2Fdjango-ldapdb.git diff --git a/ldapdb/models/query.py b/ldapdb/models/query.py index 367cd2c..f430286 100644 --- a/ldapdb/models/query.py +++ b/ldapdb/models/query.py @@ -21,14 +21,60 @@ 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 -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 +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" % value] + elif lookup_type == 'startswith': + params = ["%s*" % value] + elif lookup_type == 'exact': + params = [value] + elif lookup_type == 'in': + params = [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: @@ -88,7 +134,7 @@ class Query(BaseQuery): keys = [] for k in ordering: attr = self.model._meta.get_field(k).db_column - keys.append(x[1].get(attr, None).lower()) + keys.append(x[1].get(attr, '').lower()) return keys vals = sorted(vals, key=lambda x: getkey(x))