Skip to content

Commit

Permalink
rename Entity label key
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-muchiri committed Jun 20, 2024
1 parent a06ce61 commit d0e9a51
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 33 deletions.
8 changes: 4 additions & 4 deletions docs/entities.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
},
{
Expand All @@ -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",
}
}
]
Expand Down Expand Up @@ -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",
}
}
```
Expand Down Expand Up @@ -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",
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion onadata/apps/api/tests/viewsets/test_abstract_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
20 changes: 10 additions & 10 deletions onadata/apps/api/tests/viewsets/test_entity_list_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand Down Expand Up @@ -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",
)
Expand Down Expand Up @@ -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",
),
Expand All @@ -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",
)
Expand All @@ -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",
},
},
{
Expand All @@ -349,7 +349,7 @@ def setUp(self):
"species": "wallaba",
"circumference_cm": 100,
"intake_notes": "Looks malnourished",
"meta/entity/label": "100cm wallaba",
"label": "100cm wallaba",
},
},
]
Expand Down Expand Up @@ -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",
},
},
)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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
),
]
4 changes: 2 additions & 2 deletions onadata/apps/logger/tests/models/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions onadata/apps/logger/tests/models/test_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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",
)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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",
},
)

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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",
},
)
2 changes: 1 addition & 1 deletion onadata/libs/serializers/entity_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion onadata/libs/tests/utils/test_export_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand Down
2 changes: 1 addition & 1 deletion onadata/libs/tests/utils/test_logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion onadata/libs/utils/export_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
Expand Down
8 changes: 6 additions & 2 deletions onadata/libs/utils/logger_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d0e9a51

Please sign in to comment.