Skip to content

Commit f8f980c

Browse files
authored
fix(data): dynamically add empty validation status dict DEV-1159
### 📣 Summary Fix error when loading data tables for submissions created before validation_statuses were added. ### 💭 Notes Backport of #6388 Set `_validation_status` to `{}` if it's None on the Instance (which may happen if instances are bulk updated or created) when saving to mongo, and, on the other side, set `_validation_status` to `{}` when returning mongo results if it would otherwise be empty. ### 👀 Preview steps 1. ℹ️ have an account and a project 2. Add submissions to the project 3. In a mongo shell, run `db.instances.updateMany({"_userform_id": "<username>_<asset_uid>"}, {$unset: {"_validation_status":""}})` . This will mimic old instances where `_validation_status` isn't present 4. Go to the data table for the project 5. 🔴 [on main] Error 6. 🟢 [on PR] Data table loads successfully
1 parent 16f52c0 commit f8f980c

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

kobo/apps/openrosa/apps/logger/models/instance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def get_validation_status(self):
368368
# For example:
369369
# if not self.validation_status:
370370
# self.validation_status = self.asset.settings.get("validation_statuses")[0]
371-
return self.validation_status
371+
return self.validation_status or {}
372372

373373

374374
if Instance.XML_HASH_LENGTH / 2 != sha256().digest_size:

kobo/apps/openrosa/apps/viewer/models/parsed_instance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from pymongo.errors import PyMongoError
1212

1313
from kobo.apps.hook.utils.services import call_services
14-
from kobo.apps.openrosa.apps.logger.models import Instance, Note, XForm, Attachment
14+
from kobo.apps.openrosa.apps.logger.models import Attachment, Instance, Note, XForm
1515
from kobo.apps.openrosa.apps.logger.models.attachment import AttachmentDeleteStatus
1616
from kobo.apps.openrosa.apps.logger.xform_instance_parser import add_uuid_prefix
1717
from kobo.apps.openrosa.libs.utils.common_tags import (

kpi/deployment_backends/base_backend.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,9 @@ def _inject_properties(
823823
submission, all_attachment_xpaths
824824
)
825825
submission = self._inject_root_uuid(submission)
826+
submission['_validation_status'] = (
827+
submission.get('_validation_status', None) or {}
828+
)
826829
return submission
827830

828831
def _inject_root_uuid(self, submission: dict) -> dict:

kpi/tests/api/v2/test_api_submissions.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,10 +1160,10 @@ def test_attachments_rewrite(self):
11601160
assert attachment['download_url'] == expected_new_download_urls[idx]
11611161
assert attachment['question_xpath'] == expected_question_xpaths[idx]
11621162

1163-
def test_inject_root_uuid_if_not_present(self):
1163+
def test_inject_requires_properties_if_not_present(self):
11641164
"""
1165-
Ensure `meta/rootUUid` is present in API response even if rootUuid was
1166-
not present (e.g. like old submissions)
1165+
Ensure `meta/rootUUid` and `_validation_status` are present in API response
1166+
even if not present (e.g. like old submissions)
11671167
"""
11681168
# remove "meta/rootUuid" from MongoDB
11691169
submission = self.submissions_submitted_by_someuser[0]
@@ -1172,7 +1172,8 @@ def test_inject_root_uuid_if_not_present(self):
11721172
)
11731173
root_uuid = mongo_document.pop(META_ROOT_UUID)
11741174
settings.MONGO_DB.instances.update_one(
1175-
{'_id': submission['_id']}, {'$unset': {META_ROOT_UUID: root_uuid}}
1175+
{'_id': submission['_id']},
1176+
{'$unset': {META_ROOT_UUID: root_uuid, '_validation_status': None}},
11761177
)
11771178

11781179
url = reverse(
@@ -1185,7 +1186,9 @@ def test_inject_root_uuid_if_not_present(self):
11851186
response = self.client.get(url, {'format': 'json'})
11861187
assert response.data['_id'] == submission['_id']
11871188
assert META_ROOT_UUID in response.data
1189+
assert '_validation_status' in response.data
11881190
assert response.data[META_ROOT_UUID] == root_uuid
1191+
assert response.data['_validation_status'] == {}
11891192

11901193
def test_submitted_by_persists_after_user_deletion(self):
11911194
# Simulate old submissions that don't have `submitted_by`

0 commit comments

Comments
 (0)