diff --git a/docs/entities.md b/docs/entities.md index 4489d46bad..13edbcd1a7 100644 --- a/docs/entities.md +++ b/docs/entities.md @@ -152,7 +152,7 @@ Body: "species":"purpleheart", "geometry":"-1.286905 36.772845 0 0", "circumference_cm":300, - "meta/entity/label":"300cm purpleheart", + "label":"300cm purpleheart", } }, { @@ -165,7 +165,7 @@ Body: "geometry":"-1.305796 36.791849 0 0", "intake_notes":"Looks malnourished", "circumference_cm":100, - "meta/entity/label":"100cm wallaba", + "label":"100cm wallaba", } } ] @@ -197,7 +197,7 @@ Body: "species":"purpleheart", "geometry":"-1.286905 36.772845 0 0", "circumference_cm":300, - "meta/entity/label":"300cm purpleheart", + "label":"300cm purpleheart", } } ``` @@ -246,7 +246,7 @@ Body: "geometry": "-1.286805 36.772845 0 0", "species": "mora", "circumference_cm": 30, - "meta/entity/label": "30cm mora", + "label": "30cm mora", } } ``` diff --git a/onadata/apps/api/tests/viewsets/test_abstract_viewset.py b/onadata/apps/api/tests/viewsets/test_abstract_viewset.py index 1c06f39343..81ac952be4 100644 --- a/onadata/apps/api/tests/viewsets/test_abstract_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_abstract_viewset.py @@ -699,7 +699,7 @@ def _create_entity(self): "geometry": "-1.286905 36.772845 0 0", "species": "purpleheart", "circumference_cm": 300, - "meta/entity/label": "300cm purpleheart", + "label": "300cm purpleheart", }, uuid="dbee4c32-a922-451c-9df7-42f40bf78f48", ) diff --git a/onadata/apps/api/tests/viewsets/test_entity_list_viewset.py b/onadata/apps/api/tests/viewsets/test_entity_list_viewset.py index a9ce4a0bcb..44820a2565 100644 --- a/onadata/apps/api/tests/viewsets/test_entity_list_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_entity_list_viewset.py @@ -47,7 +47,7 @@ def test_get_all(self): "species": "purpleheart", "geometry": "-1.286905 36.772845 0 0", "circumference_cm": 300, - "meta/entity/label": "300cm purpleheart", + "label": "300cm purpleheart", }, uuid="dbee4c32-a922-451c-9df7-42f40bf78f48", ) @@ -190,7 +190,7 @@ def setUp(self): "species": "purpleheart", "geometry": "-1.286905 36.772845 0 0", "circumference_cm": 300, - "meta/entity/label": "300cm purpleheart", + "label": "300cm purpleheart", }, uuid="dbee4c32-a922-451c-9df7-42f40bf78f48", ) @@ -305,7 +305,7 @@ def setUp(self): "geometry": "-1.286905 36.772845 0 0", "species": "purpleheart", "circumference_cm": 300, - "meta/entity/label": "300cm purpleheart", + "label": "300cm purpleheart", }, uuid="dbee4c32-a922-451c-9df7-42f40bf78f48", ), @@ -316,7 +316,7 @@ def setUp(self): "species": "wallaba", "circumference_cm": 100, "intake_notes": "Looks malnourished", - "meta/entity/label": "100cm wallaba", + "label": "100cm wallaba", }, uuid="517185b4-bc06-450c-a6ce-44605dec5480", ) @@ -334,7 +334,7 @@ def setUp(self): "geometry": "-1.286905 36.772845 0 0", "species": "purpleheart", "circumference_cm": 300, - "meta/entity/label": "300cm purpleheart", + "label": "300cm purpleheart", }, }, { @@ -349,7 +349,7 @@ def setUp(self): "species": "wallaba", "circumference_cm": 100, "intake_notes": "Looks malnourished", - "meta/entity/label": "100cm wallaba", + "label": "100cm wallaba", }, }, ] @@ -461,7 +461,7 @@ def test_get_entity(self): "geometry": "-1.286905 36.772845 0 0", "species": "purpleheart", "circumference_cm": 300, - "meta/entity/label": "300cm purpleheart", + "label": "300cm purpleheart", }, }, ) @@ -561,7 +561,7 @@ def test_updating_entity(self, mock_now): "geometry": "-1.286805 36.772845 0 0", "species": "mora", "circumference_cm": 30, - "meta/entity/label": "30cm mora", + "label": "30cm mora", } self.assertDictEqual( @@ -612,7 +612,7 @@ def test_patch_label(self): ), "data": { **self.entity.json, - "meta/entity/label": "Patched label", + "label": "Patched label", }, } self.assertDictEqual(response.data, expected_data) @@ -668,7 +668,7 @@ def test_unset_property(self): "data": { "geometry": "-1.286905 36.772845 0 0", "circumference_cm": 300, - "meta/entity/label": "300cm purpleheart", + "label": "300cm purpleheart", }, } self.assertDictEqual(response.data, expected_data) diff --git a/onadata/apps/logger/migrations/0018_entityhistory_entitylistgroupobjectpermission_and_more.py b/onadata/apps/logger/migrations/0018_entityhistory_entitylistgroupobjectpermission_and_more.py index ecd0a90b4b..8a4fcb0671 100644 --- a/onadata/apps/logger/migrations/0018_entityhistory_entitylistgroupobjectpermission_and_more.py +++ b/onadata/apps/logger/migrations/0018_entityhistory_entitylistgroupobjectpermission_and_more.py @@ -5,6 +5,20 @@ import django.db.models.deletion +def rename_entity_label_key(apps, schema_editor): + Entity = apps.get_model("logger", "Entity") + + for entity in Entity.objects.all(): + old_key = "meta/entity/label" + + if entity.json.get(old_key): + updated_json = {**entity.json} + updated_json["label"] = entity.json[old_key] + del updated_json[old_key] + entity.json = updated_json + entity.save(update_fields=["json"]) + + class Migration(migrations.Migration): dependencies = [ @@ -197,4 +211,7 @@ class Migration(migrations.Migration): name="entitylistgroupobjectpermission", unique_together={("group", "permission", "content_object")}, ), + migrations.RunPython( + rename_entity_label_key, reverse_code=migrations.RunPython.noop + ), ] diff --git a/onadata/apps/logger/tests/models/test_entity.py b/onadata/apps/logger/tests/models/test_entity.py index 1cc99e3718..f42073e0e1 100644 --- a/onadata/apps/logger/tests/models/test_entity.py +++ b/onadata/apps/logger/tests/models/test_entity.py @@ -33,7 +33,7 @@ def test_creation(self, mock_now): entity_json = { "geometry": "-1.286905 36.772845 0 0", "circumference_cm": 300, - "meta/entity/label": "300cm purpleheart", + "label": "300cm purpleheart", } uuid = "dbee4c32-a922-451c-9df7-42f40bf78f48" entity = Entity.objects.create( @@ -131,7 +131,7 @@ def test_creation(self, mock_now): "species": "purpleheart", "geometry": "-1.286905 36.772845 0 0", "circumference_cm": 300, - "meta/entity/label": "300cm purpleheart", + "label": "300cm purpleheart", } survey_type = SurveyType.objects.create(slug="slug-foo") instance = Instance(xform=self.xform, xml=self.xml, survey_type=survey_type) diff --git a/onadata/apps/logger/tests/models/test_instance.py b/onadata/apps/logger/tests/models/test_instance.py index d332b03d8f..9d4e953fe7 100644 --- a/onadata/apps/logger/tests/models/test_instance.py +++ b/onadata/apps/logger/tests/models/test_instance.py @@ -459,7 +459,7 @@ def test_create_entity(self): "species": "purpleheart", "geometry": "-1.286905 36.772845 0 0", "circumference_cm": 300, - "meta/entity/label": "300cm purpleheart", + "label": "300cm purpleheart", } self.assertDictEqual(entity.json, expected_json) @@ -622,7 +622,7 @@ def _simulate_existing_entity(self): "species": "purpleheart", "geometry": "-1.286905 36.772845 0 0", "circumference_cm": 300, - "meta/entity/label": "300cm purpleheart", + "label": "300cm purpleheart", }, uuid="dbee4c32-a922-451c-9df7-42f40bf78f48", ) @@ -658,7 +658,7 @@ def test_update_entity(self): "geometry": "-1.286905 36.772845 0 0", "latest_visit": "2024-05-28", "circumference_cm": 30, - "meta/entity/label": "300cm purpleheart", + "label": "300cm purpleheart", } self.assertDictEqual(entity.json, expected_json) @@ -728,7 +728,7 @@ def test_update_entity_label(self): "geometry": "-1.286905 36.772845 0 0", "latest_visit": "2024-05-28", "circumference_cm": 30, - "meta/entity/label": "30cm updated", + "label": "30cm updated", }, ) @@ -823,7 +823,7 @@ def test_update_entity_true(self): "geometry": "-1.286905 36.772845 0 0", "latest_visit": "2024-05-28", "circumference_cm": 30, - "meta/entity/label": "300cm purpleheart", + "label": "300cm purpleheart", } self.entity.refresh_from_db() @@ -880,7 +880,7 @@ def test_entity_create_update_true(self): expected_json = { "latest_visit": "2024-05-28", "circumference_cm": 30, - "meta/entity/label": "30cm dbee4c32-a922-451c-9df7-42f40bf78f48", + "label": "30cm dbee4c32-a922-451c-9df7-42f40bf78f48", } self.assertDictEqual(entity.json, expected_json) @@ -895,7 +895,7 @@ def test_entity_create_update_true(self): "geometry": "-1.286905 36.772845 0 0", "latest_visit": "2024-05-28", "circumference_cm": 30, - "meta/entity/label": "30cm dbee4c32-a922-451c-9df7-42f40bf78f48", + "label": "30cm dbee4c32-a922-451c-9df7-42f40bf78f48", } self.entity.refresh_from_db() # No new Entity should be created @@ -929,7 +929,7 @@ def test_update_entity_via_instance_update(self): "geometry": "-1.286905 36.772845 0 0", "latest_visit": "2024-05-28", "circumference_cm": 30, - "meta/entity/label": "300cm purpleheart", + "label": "300cm purpleheart", } self.assertDictEqual(entity.json, expected_json) @@ -964,7 +964,7 @@ def test_update_entity_via_instance_update(self): "geometry": "-1.286905 36.772845 0 0", "latest_visit": "2024-06-19", "circumference_cm": 32, - "meta/entity/label": "32cm purpleheart", + "label": "32cm purpleheart", } self.assertDictEqual(entity.json, expected_json) @@ -1002,6 +1002,6 @@ def test_create_entity_exists(self): "species": "purpleheart", "geometry": "-1.286905 36.772845 0 0", "circumference_cm": 300, - "meta/entity/label": "300cm purpleheart", + "label": "300cm purpleheart", }, ) diff --git a/onadata/libs/serializers/entity_serializer.py b/onadata/libs/serializers/entity_serializer.py index 4b95c0a2c0..32963b55ff 100644 --- a/onadata/libs/serializers/entity_serializer.py +++ b/onadata/libs/serializers/entity_serializer.py @@ -142,7 +142,7 @@ def update(self, instance, validated_data): label = validated_data.pop("label", None) if label: - instance.json["meta/entity/label"] = label + instance.json["label"] = label if data: updated_data = {**instance.json, **data} diff --git a/onadata/libs/tests/utils/test_export_tools.py b/onadata/libs/tests/utils/test_export_tools.py index 32e1b48752..a2cf21fe59 100644 --- a/onadata/libs/tests/utils/test_export_tools.py +++ b/onadata/libs/tests/utils/test_export_tools.py @@ -1018,7 +1018,7 @@ def test_generate_export_entity_list(self): "species": "purpleheart", "geometry": "-1.286905 36.772845 0 0", "circumference_cm": 300, - "meta/entity/label": "300cm purpleheart", + "label": "300cm purpleheart", }, uuid="dbee4c32-a922-451c-9df7-42f40bf78f48", ) diff --git a/onadata/libs/tests/utils/test_logger_tools.py b/onadata/libs/tests/utils/test_logger_tools.py index b846b26e6f..e133dd1986 100644 --- a/onadata/libs/tests/utils/test_logger_tools.py +++ b/onadata/libs/tests/utils/test_logger_tools.py @@ -703,7 +703,7 @@ def test_entity_created(self): "geometry": "-1.286905 36.772845 0 0", "species": "purpleheart", "circumference_cm": 300, - "meta/entity/label": "300cm purpleheart", + "label": "300cm purpleheart", } self.assertCountEqual(entity.json, expected_json) diff --git a/onadata/libs/utils/export_tools.py b/onadata/libs/utils/export_tools.py index f40658c989..5755b2d356 100644 --- a/onadata/libs/utils/export_tools.py +++ b/onadata/libs/utils/export_tools.py @@ -140,7 +140,7 @@ def get_entity_list_dataset(entity_list: EntityList) -> Iterator[dict]: for entity in queryset_iterator(entities): data = { "name": entity.uuid, - "label": entity.json.get("meta/entity/label", ""), + "label": entity.json.get("label", ""), } for prop in dataset_properties: data[prop] = entity.json.get(prop, "") diff --git a/onadata/libs/utils/logger_tools.py b/onadata/libs/utils/logger_tools.py index 79b6bc086d..3787a0313b 100644 --- a/onadata/libs/utils/logger_tools.py +++ b/onadata/libs/utils/logger_tools.py @@ -992,7 +992,6 @@ def get_entity_json_from_instance( mapped_properties = registration_form.get_save_to(instance.version) # Field names with an alias defined property_fields = list(mapped_properties.values()) - extra_fields = ["meta/entity/label"] def convert_to_alias(field_name: str) -> str: """Convert field name to it's alias""" @@ -1027,7 +1026,12 @@ def parse_instance_json(data: dict[str, Any]) -> None: if alias_field_name != field_name: data[alias_field_name] = data[field_name] del data[field_name] - elif field_name not in extra_fields: + + elif field_name == "meta/entity/label": + data["label"] = data["meta/entity/label"] + del data["meta/entity/label"] + + else: del data[field_name] parse_instance_json(instance_json)