Skip to content

Commit

Permalink
create default ordered columns when registering xform repeats
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-muchiri committed Jan 21, 2025
1 parent 46894da commit c2c4644
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
58 changes: 38 additions & 20 deletions onadata/libs/tests/utils/test_logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,26 @@ def setUp(self):
object_id=self.xform.pk,
content_type=ContentType.objects.get_for_model(self.xform),
)
self.expected_columns = OrderedDict(
[
(
"hospital_repeat",
["hospital_repeat[1]/hospital", "hospital_repeat[2]/hospital"],
),
(
"hospital_repeat/child_repeat",
[
"hospital_repeat[1]/child_repeat[1]/name",
"hospital_repeat[1]/child_repeat[1]/birthweight",
"hospital_repeat[1]/child_repeat[2]/name",
"hospital_repeat[1]/child_repeat[2]/birthweight",
"hospital_repeat[2]/child_repeat[1]/name",
"hospital_repeat[2]/child_repeat[1]/birthweight",
],
),
("meta/instanceID", None),
]
)

def test_register(self):
"""Repeats from all instances are registered"""
Expand Down Expand Up @@ -1369,25 +1389,23 @@ def test_register(self):
ordered_columns = json.loads(
self.register.extra_data, object_pairs_hook=OrderedDict
)
expected_columns = OrderedDict(
[
(
"hospital_repeat",
["hospital_repeat[1]/hospital", "hospital_repeat[2]/hospital"],
),
(
"hospital_repeat/child_repeat",
[
"hospital_repeat[1]/child_repeat[1]/name",
"hospital_repeat[1]/child_repeat[1]/birthweight",
"hospital_repeat[1]/child_repeat[2]/name",
"hospital_repeat[1]/child_repeat[2]/birthweight",
"hospital_repeat[2]/child_repeat[1]/name",
"hospital_repeat[2]/child_repeat[1]/birthweight",
],
),
("meta/instanceID", None),
]

self.assertEqual(ordered_columns, self.expected_columns)

def test_register_not_found(self):
"""Register is created if not found"""
self.register.delete()
register_xform_export_columns(self.xform)

exists = MetaData.objects.filter(data_type="export_columns_register").exists()

self.assertTrue(exists)

register = MetaData.objects.get(
data_type="export_columns_register",
object_id=self.xform.pk,
content_type=ContentType.objects.get_for_model(self.xform),
)
ordered_columns = json.loads(register.extra_data, object_pairs_hook=OrderedDict)

self.assertEqual(ordered_columns, expected_columns)
self.assertEqual(ordered_columns, self.expected_columns)
17 changes: 13 additions & 4 deletions onadata/libs/utils/logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1564,7 +1564,7 @@ def _update_export_columns_register(instance: Instance, metadata: MetaData) -> N

@transaction.atomic()
def register_instance_export_columns(instance: Instance) -> None:
"""Register an Instance's export columns
"""Register an Instance's repeat columns for export
:param instance: Instance object
"""
Expand All @@ -1589,12 +1589,21 @@ def register_xform_export_columns(xform: XForm) -> None:
:param xform: XForm object
"""
content_type = ContentType.objects.get_for_model(xform)
# Avoid cyclic import by using importlib
csv_builder = importlib.import_module("onadata.libs.utils.csv_builder")
ordered_columns = OrderedDict()
# pylint: disable=protected-access
csv_builder.CSVDataFrameBuilder._build_ordered_columns(
xform._get_survey(), ordered_columns
)
metadata, _ = MetaData.objects.get_or_create(
content_type=content_type,
content_type=ContentType.objects.get_for_model(xform),
object_id=xform.pk,
data_type=EXPORT_COLUMNS_REGISTER,
defaults={"data_value": ""},
defaults={
"data_value": "",
"extra_data": json.dumps(ordered_columns),
},
)
instance_qs = xform.instances.filter(deleted_at__isnull=True)

Expand Down

0 comments on commit c2c4644

Please sign in to comment.