diff --git a/src/hct_mis_api/api/filters.py b/src/hct_mis_api/api/filters.py index 5d2c1d3de8..cca9a6cbee 100644 --- a/src/hct_mis_api/api/filters.py +++ b/src/hct_mis_api/api/filters.py @@ -19,6 +19,8 @@ class AreaFilter(UpdatedAtFilter): country_iso_code2 = CharFilter(field_name="area_type__country__iso_code2") country_iso_code3 = CharFilter(field_name="area_type__country__iso_code3") area_type_area_level = NumberFilter(field_name="area_type__area_level") + parent_id = CharFilter(field_name="parent__id") + parent_p_code = CharFilter(field_name="parent__p_code") class Meta: model = Area @@ -28,6 +30,8 @@ class Meta: "area_type_area_level", "valid_from", "valid_until", + "parent_id", + "parent_p_code", ) diff --git a/tests/unit/api/test_lookups.py b/tests/unit/api/test_lookups.py index 0bc338f9e1..752dfc5911 100644 --- a/tests/unit/api/test_lookups.py +++ b/tests/unit/api/test_lookups.py @@ -140,6 +140,7 @@ def setUpTestData(cls) -> None: cls.area2 = AreaFactory( name="area2", area_type=cls.area_type2, + parent=cls.area1, ) cls.area2.valid_from = datetime(2020, 1, 1, tzinfo=pytz.UTC) cls.area2.valid_until = datetime(2020, 12, 31, tzinfo=pytz.UTC) @@ -162,7 +163,7 @@ def get_result(self, area: Area) -> dict: "rght": area.rght, "tree_id": area.tree_id, "level": area.level, - "parent": area.parent, + "parent": str(area.parent.id) if area.parent else None, "area_type": str(area.area_type.id), } @@ -189,6 +190,10 @@ def test_get_area_list_filter(self) -> None: ({"valid_until_after": "2021-01-01"}, []), ({"area_type_area_level": 1}, [self.area1]), ({"area_type_area_level": 2}, [self.area2]), + ({"parent_id": str(self.area1.id)}, [self.area2]), + ({"parent_p_code": self.area1.p_code}, [self.area2]), + ({"parent_id": str(self.area2.id)}, []), + ({"parent_p_code": self.area2.p_code}, []), ): with token_grant_permission(self.token, Grant.API_READ_ONLY): response = self.client.get(self.url, filter_data) # type: ignore