Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not create export register when Instance is saved #2760

Merged
merged 13 commits into from
Jan 17, 2025
7 changes: 7 additions & 0 deletions onadata/apps/logger/tests/models/test_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from datetime import datetime, timedelta
from unittest.mock import Mock, patch

from django.contrib.contenttypes.models import ContentType
from django.http.request import HttpRequest
from django.test import override_settings
from django.utils.timezone import utc
Expand Down Expand Up @@ -1230,6 +1231,12 @@ def test_register_repeats(self):
"""
self._publish_markdown(md, self.user, project)
xform = XForm.objects.all().order_by("-pk").first()
metadata = MetaData.objects.create(
content_type=ContentType.objects.get_for_model(xform),
object_id=xform.id,
data_type="export_repeat_register",
data_value="",
)
xml = (
'<?xml version="1.0" encoding="UTF-8"?>'
'<data xmlns:jr="http://openrosa.org/javarosa" xmlns:orx='
Expand Down
77 changes: 19 additions & 58 deletions onadata/libs/tests/utils/test_logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1217,13 +1217,12 @@ def setUp(self):
xml=self.xml, user=self.user, xform=self.xform
)

def test_repeat_count_create(self):
"""MetaData of type export_repeat_register is created"""
def test_repeat_register_not_found(self):
"""Nothing happens if export repeat register is not found"""
register_instance_export_repeats(self.instance)

metadata = MetaData.objects.get(data_type="export_repeat_register")
self.assertEqual(metadata.extra_data.get("hospital_repeat"), 2)
self.assertEqual(metadata.extra_data.get("child_repeat"), 2)
exists = MetaData.objects.filter(data_type="export_repeat_register").exists()
self.assertFalse(exists)

def test_incoming_repeat_max_greater(self):
"""Repeat count is incremented if incoming repeat count is greater"""
Expand Down Expand Up @@ -1282,7 +1281,7 @@ def test_incoming_repeat_max_equal(self):
self.assertEqual(metadata.extra_data.get("child_repeat"), 2)

def test_no_repeats(self):
"""Instance has no repeats"""
"""No change in register if no repeats are found in the instance"""
md = """
| survey |
| | type | name | label |
Expand All @@ -1306,72 +1305,34 @@ def test_no_repeats(self):
"</data>"
)
instance = Instance.objects.create(xml=xml, user=self.user, xform=xform)
metadata = MetaData.objects.create(
content_type=ContentType.objects.get_for_model(self.xform),
object_id=self.xform.id,
data_type="export_repeat_register",
data_value="",
)

register_instance_export_repeats(instance)
metadata.refresh_from_db()

exists = MetaData.objects.filter(data_type="export_repeat_register").exists()
self.assertTrue(exists)
metadata = MetaData.objects.get(data_type="export_repeat_register")
self.assertEqual(metadata.extra_data, {})

def test_create_register_previous_candidates(self):
"""Previous submissions are considered when creating repeat register"""
# Existing submission with a higher repeat count for hospital_repeat
xml = (
'<?xml version="1.0" encoding="UTF-8"?>'
'<data xmlns:jr="http://openrosa.org/javarosa" xmlns:orx='
'"http://openrosa.org/xforms" id="trees_update" version="2024050801">'
f"<formhub><uuid>{self.xform.uuid}</uuid></formhub>"
"<hospital_repeat>"
"<hospital>Aga Khan</hospital>"
"<child_repeat>"
"<name>Zakayo</name>"
"<birthweight>3.3</birthweight>"
"</child_repeat>"
"<child_repeat>"
"<name>Melania</name>"
"<birthweight>3.5</birthweight>"
"</child_repeat>"
"</hospital_repeat>"
"<hospital_repeat>"
"<hospital>Mama Lucy</hospital>"
"<child_repeat>"
"<name>Winnie</name>"
"<birthweight>3.1</birthweight>"
"</child_repeat>"
"</hospital_repeat>"
"<hospital_repeat>"
"<hospital>Nairobi West</hospital>"
"<child_repeat>"
"<name>Tom</name>"
"<birthweight>3.1</birthweight>"
"</child_repeat>"
"</hospital_repeat>"
"<meta>"
"<instanceID>uuid:cb5eb8fe-a046-4e75-9c7f-72183b871698</instanceID>"
"</meta>"
"</data>"
)
Instance.objects.create(xml=xml, user=self.user, xform=self.xform)

register_instance_export_repeats(self.instance)

metadata = MetaData.objects.get(data_type="export_repeat_register")
# The previous submission should be considered since it has the most repeats
# for hospital_repeat
self.assertEqual(metadata.extra_data.get("hospital_repeat"), 3)
self.assertEqual(metadata.extra_data.get("child_repeat"), 2)

def test_submission_review_enabled(self):
"""When submission review is enabled, only approved Instance is registered"""
self.instance.delete()
MetaData.submission_review(self.xform, "true") # Enable submission review
self.instance = Instance.objects.create(
xml=self.xml, user=self.user, xform=self.xform
)
metadata = MetaData.objects.create(
content_type=ContentType.objects.get_for_model(self.xform),
object_id=self.xform.id,
data_type="export_repeat_register",
data_value="",
)
register_instance_export_repeats(self.instance)

metadata = MetaData.objects.get(data_type="export_repeat_register")
metadata.refresh_from_db()

self.assertEqual(metadata.extra_data, {})

Expand Down
42 changes: 18 additions & 24 deletions onadata/libs/utils/logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1592,24 +1592,6 @@ def _update_export_repeat_register(instance: Instance, metadata: MetaData) -> No
)


def _get_export_repeat_register(xform: XForm) -> tuple[MetaData, bool]:
"""Get or create the export repeat register

:param xform: XForm object
:return: Tuple of MetaData object and a boolean indicating if it was
created
"""
content_type = ContentType.objects.get_for_model(xform)
obj, created = MetaData.objects.get_or_create(
content_type=content_type,
object_id=xform.pk,
data_type=EXPORT_REPEAT_REGISTER,
defaults={"data_value": ""},
)

return obj, created


def _is_submission_approved(instance: Instance) -> bool:
"""Check if a submission has been approved

Expand Down Expand Up @@ -1640,13 +1622,19 @@ def register_instance_export_repeats(instance: Instance) -> None:

:param instance: Instance object
"""
metadata, created = _get_export_repeat_register(instance.xform)
content_type = ContentType.objects.get_for_model(instance.xform)

if created:
register_xform_export_repeats(instance.xform)
try:
metadata = MetaData.objects.get(
content_type=content_type,
object_id=instance.xform.pk,
data_type=EXPORT_REPEAT_REGISTER,
)

else:
_update_export_repeat_register(instance, metadata)
except MetaData.DoesNotExist:
return

_update_export_repeat_register(instance, metadata)


@transaction.atomic()
Expand All @@ -1655,7 +1643,13 @@ def register_xform_export_repeats(xform: XForm) -> None:

:param xform: XForm object
"""
metadata, _ = _get_export_repeat_register(xform)
content_type = ContentType.objects.get_for_model(xform)
metadata, _ = MetaData.objects.get_or_create(
content_type=content_type,
object_id=xform.pk,
data_type=EXPORT_REPEAT_REGISTER,
defaults={"data_value": ""},
)
instance_qs = xform.instances.filter(deleted_at__isnull=True)

for instance in queryset_iterator(instance_qs):
Expand Down
Loading