Skip to content

Commit

Permalink
create export register when form is published
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-muchiri committed Jan 16, 2025
1 parent 77abf1e commit 08ddc05
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
19 changes: 19 additions & 0 deletions onadata/apps/viewer/models/data_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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",
)
14 changes: 14 additions & 0 deletions onadata/apps/viewer/models/tests/test_data_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 08ddc05

Please sign in to comment.