From: jlaine Date: Mon, 27 Dec 2010 11:58:44 +0000 (+0000) Subject: fix "exclude" operations X-Git-Url: https://git.stderr.nl/gitweb?a=commitdiff_plain;h=641160553d6db632259d325be2b22ee7c3059f3c;p=matthijs%2Fupstream%2Fdjango-ldapdb.git fix "exclude" operations git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@967 e071eeec-0327-468d-9b6a-08194a12b294 --- diff --git a/examples/tests.py b/examples/tests.py index 0905e12..bd47291 100644 --- a/examples/tests.py +++ b/examples/tests.py @@ -97,6 +97,19 @@ class GroupTestCase(BaseTestCase): qs = LdapGroup.objects.all() self.assertEquals(len(qs), 3) + def test_ldap_filter(self): + qs = LdapGroup.objects.filter(name='foogroup') + self.assertEquals(qs.query._ldap_filter(), '(&(objectClass=posixGroup)(cn=foogroup))') + + qs = LdapGroup.objects.filter(name='foogroup', gid=1000) + self.assertEquals(qs.query._ldap_filter(), '(&(objectClass=posixGroup)(&(gidNumber=1000)(cn=foogroup)))') + + qs = LdapGroup.objects.exclude(name='foogroup') + self.assertEquals(qs.query._ldap_filter(), '(&(objectClass=posixGroup)(!(cn=foogroup)))') + + qs = LdapGroup.objects.exclude(name='foogroup', gid=1000) + self.assertEquals(qs.query._ldap_filter(), '(&(objectClass=posixGroup)(!(&(gidNumber=1000)(cn=foogroup))))') + def test_filter(self): qs = LdapGroup.objects.filter(name='foogroup') self.assertEquals(qs.count(), 1) diff --git a/ldapdb/models/query.py b/ldapdb/models/query.py index ab5f19e..1fddeee 100644 --- a/ldapdb/models/query.py +++ b/ldapdb/models/query.py @@ -190,10 +190,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 +203,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):