Skip to content

Commit

Permalink
Change country endpoint to use standard pagination without count
Browse files Browse the repository at this point in the history
  • Loading branch information
pkujawa committed Oct 30, 2024
1 parent a08563b commit acc8ba8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/hct_mis_api/api/endpoints/lookups/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from hct_mis_api.api.endpoints.base import HOPEAPIView
from hct_mis_api.api.endpoints.serializers import CountrySerializer
from hct_mis_api.api.pagination import LimitOffsetPaginationWithoutCount
from hct_mis_api.apps.geo.models import Country
from hct_mis_api.apps.household.models import (
COLLECT_TYPES,
Expand All @@ -30,7 +31,7 @@ def get(self, request: "Request", format: Optional[Any] = None) -> Response:
class CountryAPIView(HOPEAPIView, ListAPIView):
queryset = Country.objects.all()
serializer_class = CountrySerializer
pagination_class = None
pagination_class = LimitOffsetPaginationWithoutCount


class ResidenceStatus(HOPEAPIView):
Expand Down
17 changes: 17 additions & 0 deletions src/hct_mis_api/api/pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from collections import OrderedDict
from typing import Any

from rest_framework.pagination import LimitOffsetPagination
from rest_framework.response import Response


class LimitOffsetPaginationWithoutCount(LimitOffsetPagination):
def get_paginated_response(self, data: dict) -> Response:
return Response(
OrderedDict([("next", self.get_next_link()), ("previous", self.get_previous_link()), ("results", data)])
)

def get_paginated_response_schema(self, schema: Any) -> dict:
response_schema = super().get_paginated_response_schema(schema)
del response_schema["properties"]["count"]
return response_schema
40 changes: 23 additions & 17 deletions tests/unit/api/test_lookups.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,26 @@ def test_get_countries(self) -> None:
url = reverse("api:country-list")
with token_grant_permission(self.token, Grant.API_READ_ONLY):
response = self.client.get(url)
assert response.status_code == status.HTTP_200_OK
assert response.json() == [
{
"name": country_afghanistan.name,
"short_name": country_afghanistan.short_name,
"iso_code2": country_afghanistan.iso_code2,
"iso_code3": country_afghanistan.iso_code3,
"iso_num": country_afghanistan.iso_num,
},
{
"name": country_poland.name,
"short_name": country_poland.short_name,
"iso_code2": country_poland.iso_code2,
"iso_code3": country_poland.iso_code3,
"iso_num": country_poland.iso_num,
},
]
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.json()["results"],
[
{
"name": country_afghanistan.name,
"short_name": country_afghanistan.short_name,
"iso_code2": country_afghanistan.iso_code2,
"iso_code3": country_afghanistan.iso_code3,
"iso_num": country_afghanistan.iso_num,
},
{
"name": country_poland.name,
"short_name": country_poland.short_name,
"iso_code2": country_poland.iso_code2,
"iso_code3": country_poland.iso_code3,
"iso_num": country_poland.iso_num,
},
],
)
self.assertNotIn("count", response.json())
self.assertIn("next", response.json())
self.assertIn("previous", response.json())

0 comments on commit acc8ba8

Please sign in to comment.