Skip to content

Commit

Permalink
Merge pull request #598 from praekeltfoundation/clinic-list-update
Browse files Browse the repository at this point in the history
Add clinic details endpoint
  • Loading branch information
erikh360 authored May 30, 2024
2 parents be3a067 + f66d850 commit 7bee8ce
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
38 changes: 38 additions & 0 deletions registrations/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,44 @@ def setUp(self):
self.normalclient.credentials(HTTP_AUTHORIZATION="Token " + self.normaltoken)


class FacilityDetailsViewTests(APITestCase):
def test_get_facility_details(self):
ClinicCode.objects.create(
code="123456",
value="123456",
uid="cc1",
name="test1",
province="ZA-EC",
location="(location)",
area_type="rural",
unit_type="test unit type",
district="test district",
municipality="test munnicipality",
)

user = User.objects.create_user("test", "test")
self.client.force_authenticate(user)

url = reverse("facility-detail")
response = self.client.get(url, {"facility_code": "123456"})

self.assertEqual(
response.json(),
{
"area_type": "rural",
"code": "123456",
"district": "test district",
"location": "(location)",
"municipality": "test munnicipality",
"name": "test1",
"province": "ZA-EC",
"uid": "cc1",
"unit_type": "test unit type",
"value": "123456",
},
)


class FacilityCheckViewTests(APITestCase):
def test_filter_by_code(self):
ClinicCode.objects.create(
Expand Down
5 changes: 5 additions & 0 deletions registrations/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@
path(
"api/v1/facilityCheck", views.FacilityCheckView.as_view(), name="facility-check"
),
path(
"api/v1/facilityDetails",
views.FacilityDetailsView.as_view(),
name="facility-detail",
),
re_path(r"^api/v1/", include(router.urls)),
]
23 changes: 23 additions & 0 deletions registrations/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from functools import partial

from django.forms.models import model_to_dict
from rest_framework import generics, mixins, status, viewsets
from rest_framework.authentication import (
BasicAuthentication,
Expand All @@ -20,6 +21,28 @@ class BearerTokenAuthentication(TokenAuthentication):
keyword = "Bearer"


class FacilityDetailsView(generics.RetrieveAPIView):

def get(self, request):
try:
facility_code = request.query_params["facility_code"]
except KeyError:
return Response(
{"error": "Must supply 'criteria' query parameter"},
status.HTTP_400_BAD_REQUEST,
)

try:
clinic = ClinicCode.objects.get(value=facility_code)
except ClinicCode.DoesNotExist:
return Response(
{"error": "Clinic not found"},
status.HTTP_404_NOT_FOUND,
)

return Response(model_to_dict(clinic))


class FacilityCheckView(generics.RetrieveAPIView):
queryset = ClinicCode.objects.all()
permission_classes = (DjangoModelPermissions,)
Expand Down

0 comments on commit 7bee8ce

Please sign in to comment.