X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=ldapdb%2Fmodels%2Fquery.py;h=91a3f62e2058bf8587342559c04ffefee3c58487;hb=5c3683a37e2afc87654d4b44c3f8e9fbe7872076;hp=f90c9b169d6bf276714d5da5393832074c914bbc;hpb=baba34a193fe7d304d86003b19f36ed638c7ec80;p=matthijs%2Fupstream%2Fdjango-ldapdb.git diff --git a/ldapdb/models/query.py b/ldapdb/models/query.py index f90c9b1..91a3f62 100644 --- a/ldapdb/models/query.py +++ b/ldapdb/models/query.py @@ -56,8 +56,13 @@ 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. + + NOTES: + - we redefine this class, because when self.field is None calls + Field().get_db_prep_lookup(), which short-circuits our LDAP-specific code. + - the connection argument defaults to None for django 1.1 compatibility """ - def process(self, lookup_type, value): + def process(self, lookup_type, value, connection=None): """ Returns a tuple of data suitable for inclusion in a WhereNode instance. @@ -67,10 +72,12 @@ class Constraint(BaseConstraint): try: if self.field: - params = self.field.get_db_prep_lookup(lookup_type, value) + params = self.field.get_db_prep_lookup(lookup_type, value, + connection=connection, prepared=True) db_type = self.field.db_type() else: - params = CharField().get_db_prep_lookup(lookup_type, value) + params = CharField().get_db_prep_lookup(lookup_type, value, + connection=connection, prepared=True) db_type = None except ObjectDoesNotExist: raise EmptyShortCircuit @@ -94,7 +101,7 @@ class Compiler(object): try: vals = self.connection.search_s( self.query.model.base_dn, - ldap.SCOPE_SUBTREE, + self.query.model.search_scope, filterstr=self.query._ldap_filter(), attrlist=attrlist, ) @@ -190,10 +197,11 @@ class WhereNode(BaseWhereNode): else: clause = '(|%s)' % ''.join(equal_bits) - if self.negated: - bits.append('(!%s)' % clause) - else: - bits.append(clause) + bits.append(clause) + + if not len(bits): + return '', [] + if len(bits) == 1: sql_string = bits[0] elif self.connector == AND: @@ -202,6 +210,10 @@ class WhereNode(BaseWhereNode): sql_string = '(|%s)' % ''.join(bits) else: raise Exception("Unhandled WHERE connector: %s" % self.connector) + + if self.negated: + sql_string = ('(!%s)' % sql_string) + return sql_string, [] class Query(BaseQuery): @@ -219,7 +231,7 @@ class Query(BaseQuery): try: vals = ldapdb.connection.search_s( self.model.base_dn, - ldap.SCOPE_SUBTREE, + self.model.search_scope, filterstr=self._ldap_filter(), attrlist=[], ) @@ -238,6 +250,9 @@ class Query(BaseQuery): def get_compiler(self, using=None, connection=None): return Compiler(self, ldapdb.connection, using) + def has_results(self, using): + return self.get_count() != 0 + def results_iter(self): "For django 1.1 compatibility" return self.get_compiler().results_iter() @@ -260,7 +275,7 @@ class QuerySet(BaseQuerySet): try: vals = ldapdb.connection.search_s( self.model.base_dn, - ldap.SCOPE_SUBTREE, + self.model.search_scope, filterstr=self.query._ldap_filter(), attrlist=[], )