From: jlaine Date: Mon, 31 May 2010 19:52:13 +0000 (+0000) Subject: catch ldap.NO_SUCH_OBJECT for calls to ldap.search_s X-Git-Url: https://git.stderr.nl/gitweb?a=commitdiff_plain;h=1586dbefbbebc802312bccfe946fae20febeaf5a;p=matthijs%2Fupstream%2Fdjango-ldapdb.git catch ldap.NO_SUCH_OBJECT for calls to ldap.search_s git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@911 e071eeec-0327-468d-9b6a-08194a12b294 --- diff --git a/ldapdb/models/query.py b/ldapdb/models/query.py index 4f0273c..8999c17 100644 --- a/ldapdb/models/query.py +++ b/ldapdb/models/query.py @@ -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)