Skip to content

Commit

Permalink
Merge pull request #4416 from unicef/221081-geo-api-additional-filters
Browse files Browse the repository at this point in the history
[221081] Geo API - add filters
  • Loading branch information
domdinicola authored Nov 7, 2024
2 parents 6ef7a33 + c0f4938 commit 9148ee1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/hct_mis_api/api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,6 +30,8 @@ class Meta:
"area_type_area_level",
"valid_from",
"valid_until",
"parent_id",
"parent_p_code",
)


Expand Down
7 changes: 6 additions & 1 deletion tests/unit/api/test_lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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),
}

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

0 comments on commit 9148ee1

Please sign in to comment.