diff --git a/kpi/deployment_backends/kobocat_backend.py b/kpi/deployment_backends/kobocat_backend.py index 59b363eca5..8244b498ed 100644 --- a/kpi/deployment_backends/kobocat_backend.py +++ b/kpi/deployment_backends/kobocat_backend.py @@ -39,6 +39,7 @@ ) from kpi.exceptions import ( AttachmentNotFoundException, + InvalidXFormException, InvalidXPathException, SubmissionIntegrityError, SubmissionNotFoundException, @@ -1049,7 +1050,10 @@ def set_validation_statuses(self, user: 'auth.User', data: dict) -> dict: @property def submission_count(self): - return self.xform.num_of_submissions + try: + return self.xform.num_of_submissions + except InvalidXFormException: + return 0 @property def submission_list_url(self): @@ -1167,9 +1171,12 @@ def xform(self): ) # Avoid extra query to validate username below .first() ) - if not (xform.user.username == self.asset.owner.username and - xform.id_string == self.xform_id_string): - raise Exception( + if not ( + xform + and xform.user.username == self.asset.owner.username + and xform.id_string == self.xform_id_string + ): + raise InvalidXFormException( 'Deployment links to an unexpected KoBoCAT XForm') setattr(self, '_xform', xform) diff --git a/kpi/exceptions.py b/kpi/exceptions.py index f33f3afa43..ec5d00ac47 100644 --- a/kpi/exceptions.py +++ b/kpi/exceptions.py @@ -74,6 +74,10 @@ class InvalidSearchException(exceptions.APIException): default_code = 'invalid_search' +class InvalidXFormException(Exception): + pass + + class InvalidXPathException(Exception): pass