fix "exclude" operations
authorjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 27 Dec 2010 11:58:44 +0000 (11:58 +0000)
committerjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 27 Dec 2010 11:58:44 +0000 (11:58 +0000)
git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@967 e071eeec-0327-468d-9b6a-08194a12b294

examples/tests.py
ldapdb/models/query.py

index 0905e12..bd47291 100644 (file)
@@ -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)
index ab5f19e..1fddeee 100644 (file)
@@ -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):