catch ldap.NO_SUCH_OBJECT for calls to ldap.search_s
authorjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 31 May 2010 19:52:13 +0000 (19:52 +0000)
committerjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 31 May 2010 19:52:13 +0000 (19:52 +0000)
git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@911 e071eeec-0327-468d-9b6a-08194a12b294

ldapdb/models/query.py

index 4f0273cc2ed6478f7fcd78897251cd1732bb932d..8999c17a4622d16654ad2bf25da77a9ddcc90111 100644 (file)
@@ -73,12 +73,16 @@ class Compiler(object):
         query = self.query
         attrlist = [ x.db_column for x in query.model._meta.local_fields if x.db_column ]
 
-        vals = self.connection.search_s(
-            query.model.base_dn,
-            ldap.SCOPE_SUBTREE,
-            filterstr=query._ldap_filter(),
-            attrlist=attrlist,
-        )
+        try:
+            vals = self.connection.search_s(
+                query.model.base_dn,
+                ldap.SCOPE_SUBTREE,
+                filterstr=query._ldap_filter(),
+                attrlist=attrlist,
+            )
+        except ldap.NO_SUCH_OBJECT:
+            return
+            raise query.model.DoesNotExist
 
         # perform sorting
         if query.extra_order_by:
@@ -186,12 +190,15 @@ class Query(BaseQuery):
         return '(&%s)' % filterstr
 
     def get_count(self, using=None):
-        vals = ldapdb.connection.search_s(
-            self.model.base_dn,
-            ldap.SCOPE_SUBTREE,
-            filterstr=self._ldap_filter(),
-            attrlist=[],
-        )
+        try:
+            vals = ldapdb.connection.search_s(
+                self.model.base_dn,
+                ldap.SCOPE_SUBTREE,
+                filterstr=self._ldap_filter(),
+                attrlist=[],
+            )
+        except ldap.NO_SUCH_OBJECT:
+            return 0
         return len(vals)
 
     def get_compiler(self, using=None, connection=None):
@@ -216,12 +223,16 @@ class QuerySet(BaseQuerySet):
 
     def delete(self):
         "Bulk deletion."
-        vals = ldapdb.connection.search_s(
-            self.model.base_dn,
-            ldap.SCOPE_SUBTREE,
-            filterstr=self.query._ldap_filter(),
-            attrlist=[],
-        )
+        try:
+            vals = ldapdb.connection.search_s(
+                self.model.base_dn,
+                ldap.SCOPE_SUBTREE,
+                filterstr=self.query._ldap_filter(),
+                attrlist=[],
+            )
+        except ldap.NO_SUCH_OBJECT:
+            return
+
         # FIXME : there is probably a more efficient way to do this 
         for dn, attrs in vals:
             ldapdb.connection.delete_s(dn)