diff --git a/onadata/apps/viewer/models/data_dictionary.py b/onadata/apps/viewer/models/data_dictionary.py index 7bc55529c8..2d3845dfba 100644 --- a/onadata/apps/viewer/models/data_dictionary.py +++ b/onadata/apps/viewer/models/data_dictionary.py @@ -35,6 +35,7 @@ PROJ_FORMS_CACHE, safe_delete, ) +from onadata.libs.utils.common_tags import EXPORT_REPEAT_REGISTER from onadata.libs.utils.model_tools import get_columns_with_hxl, set_uuid @@ -438,3 +439,21 @@ def invalidate_caches(sender, instance=None, created=False, **kwargs): sender=DataDictionary, dispatch_uid="xform_invalidate_caches", ) + + +def create_export_repeat_register(sender, instance=None, created=False, **kwargs): + """Create export repeat register for the form""" + if created: + MetaData.objects.create( + content_type=ContentType.objects.get_for_model(instance), + object_id=instance.pk, + data_type=EXPORT_REPEAT_REGISTER, + data_value="", + ) + + +post_save.connect( + create_export_repeat_register, + sender=DataDictionary, + dispatch_uid="create_export_repeat_register", +) diff --git a/onadata/apps/viewer/models/tests/test_data_dictionary.py b/onadata/apps/viewer/models/tests/test_data_dictionary.py index e292faddd1..1ac6c4197e 100644 --- a/onadata/apps/viewer/models/tests/test_data_dictionary.py +++ b/onadata/apps/viewer/models/tests/test_data_dictionary.py @@ -2,6 +2,7 @@ import json +from django.contrib.contenttypes.models import ContentType from django.core.cache import cache from onadata.apps.logger.models.entity_list import EntityList @@ -307,3 +308,16 @@ def test_cache_invalidated(self): for key in cache_keys: self.assertIsNone(cache.get(key)) + + def test_export_repeat_register_created(self): + """Export repeat register is created when form is published""" + self._publish_markdown(self.registration_form, self.user) + xform = XForm.objects.all().order_by("-pk").first() + content_type = ContentType.objects.get_for_model(xform) + exists = MetaData.objects.filter( + data_type="export_repeat_register", + object_id=xform.pk, + content_type=content_type, + ).exists() + + self.assertTrue(exists)