Skip to content

Commit

Permalink
Add a dedicated enpoint to retrieve academies
Browse files Browse the repository at this point in the history
  • Loading branch information
mlvernay committed Dec 24, 2024
1 parent f0bcb42 commit d11eda1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
3 changes: 2 additions & 1 deletion territories/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.urls import path

from .views import TerritoryCombinedListAPIView
from .views import AcademyListAPIView, TerritoryCombinedListAPIView

urlpatterns = [
path("academies/", AcademyListAPIView.as_view(), name="academies-list"),
path("", TerritoryCombinedListAPIView.as_view(), name="territory-combined-list"),
]
12 changes: 11 additions & 1 deletion territories/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from territories.models import Academy, City, Department

from .serializers import TerritoryCombinedSerializer
from .serializers import TerritoryCombinedSerializer, AcademySerializer


class Unaccent(Func):
Expand Down Expand Up @@ -53,3 +53,13 @@ def get(self, request, *args, **kwargs):

serializer = TerritoryCombinedSerializer(data)
return Response(serializer.data)


class AcademyListAPIView(APIView):
serializer_class = AcademySerializer

def get(self, request, *args, **kwargs):
academies = Academy.objects.all()

serializer = AcademySerializer(academies, many=True)
return Response(serializer.data)
42 changes: 40 additions & 2 deletions tests/territories/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@

class TerritoryCombinedListAPITests(APITestCase):
def setUp(self):
self.academy = AcademyFactory.create(name="Academie du Rhône")
multi_polygon = MultiPolygon(Polygon(((5, 5), (5, 10), (10, 10), (10, 5), (5, 5))))
self.academy = AcademyFactory.create(name="Academie du Rhône", boundary=multi_polygon)
self.academy_paris = AcademyFactory.create(
name="Académie de Paris", boundary=MultiPolygon(Polygon(((2, 48), (2, 49), (3, 49), (3, 48), (2, 48))))
)
self.department = DepartmentFactory.create(name="Rhône", academy=self.academy, boundary=multi_polygon)
self.city = CityFactory.create(
name="Lyon", postal_codes=["69001", "69002", "69003"], department=self.department
Expand All @@ -29,7 +32,12 @@ def test_get_territory_combined_list_filtered(self):
{
"id": mock.ANY,
"name": "Academie du Rhône",
"bbox": None,
"bbox": {
"xmin": 5.0,
"ymin": 5.0,
"xmax": 10.0,
"ymax": 10.0,
},
}
],
"departments": [
Expand All @@ -47,3 +55,33 @@ def test_get_territory_combined_list_filtered(self):
"cities": [],
},
)

def test_get_academies_list(self):
response = self.client.get(reverse("academies-list"))
self.assertEqual(response.status_code, status.HTTP_200_OK)

self.assertEqual(
response.json(),
[
{
"id": mock.ANY,
"name": "Academie du Rhône",
"bbox": {
"xmin": 5.0,
"ymin": 5.0,
"xmax": 10.0,
"ymax": 10.0,
},
},
{
"id": mock.ANY,
"name": "Académie de Paris",
"bbox": {
"xmin": 2.0,
"ymin": 48.0,
"xmax": 3.0,
"ymax": 49.0,
},
},
],
)

0 comments on commit d11eda1

Please sign in to comment.