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 0905e1298da4de8e8d487a74b0ca8cca9e753586..bd47291b0fc55949a897973dcf49e8319625210c 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 ab5f19ec727b574cc845d44995d1b4371611ead7..1fddeeeb1aea3b8bb0ece0f4a7b079fa8d802b84 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):