From 3e12add70243783af6f55ef7ccd2df4a35c4f169 Mon Sep 17 00:00:00 2001 From: Olivier Leger Date: Thu, 17 Nov 2022 12:17:38 -0500 Subject: [PATCH] =?UTF-8?q?Return=200=20when=20getting=20the=20submission?= =?UTF-8?q?=20count=E2=80=A6=20of=20an=20invalid/not=20existing=20XForm?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kpi/deployment_backends/kobocat_backend.py | 15 +++++++++++---- kpi/exceptions.py | 4 ++++ 2 files changed, 15 insertions(+), 4 deletions(-) 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