Skip to content

Commit

Permalink
Adds route to fetch facility hubs (#2520)
Browse files Browse the repository at this point in the history
* Sending hub and spoke data in facility serializer

* remove unwanted changes

* Added /hubs route to list hubs

* Update care/facility/api/viewsets/facility.py

Co-authored-by: Aakash Singh <mail@singhaakash.dev>

---------

Co-authored-by: Aakash Singh <mail@singhaakash.dev>
  • Loading branch information
shivankacker and sainak authored Oct 15, 2024
1 parent 02c9056 commit e440d46
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
16 changes: 16 additions & 0 deletions care/facility/api/viewsets/facility.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,19 @@ def get_serializer_context(self):
context = super().get_serializer_context()
context["facility"] = facility
return context


class FacilityHubsViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
queryset = FacilityHubSpoke.objects.all().select_related("spoke", "hub")
serializer_class = FacilitySpokeSerializer
permission_classes = (IsAuthenticated,)
lookup_field = "external_id"

def get_queryset(self):
return self.queryset.filter(spoke=self.get_facility())

def get_facility(self):
facilities = get_facility_queryset(self.request.user)
return get_object_or_404(
facilities.filter(external_id=self.kwargs["facility_external_id"])
)
19 changes: 19 additions & 0 deletions care/facility/tests/test_facility_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,25 @@ def test_spoke_is_not_ancestor(self):
)
self.assertIs(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_hubs_list(self):
facility_a = self.create_facility(
self.super_user, self.district, self.local_body
)
facility_b = self.create_facility(
self.super_user, self.district, self.local_body
)

FacilityHubSpoke.objects.create(hub=facility_a, spoke=facility_b)

self.client.force_authenticate(user=self.super_user)
response = self.client.get(f"/api/v1/facility/{facility_b.external_id}/hubs/")
self.assertIs(response.status_code, status.HTTP_200_OK)
data = response.json()
self.assertEqual(data["count"], 1)
self.assertEqual(
data["results"][0]["hub_object"]["id"], str(facility_a.external_id)
)


class FacilityCoverImageTests(TestUtils, APITestCase):
@classmethod
Expand Down
2 changes: 2 additions & 0 deletions config/api_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
)
from care.facility.api.viewsets.facility import (
AllFacilityViewSet,
FacilityHubsViewSet,
FacilitySpokesViewSet,
FacilityViewSet,
)
Expand Down Expand Up @@ -218,6 +219,7 @@
facility_nested_router.register(
r"spokes", FacilitySpokesViewSet, basename="facility-spokes"
)
facility_nested_router.register(r"hubs", FacilityHubsViewSet, basename="facility-hubs")

router.register("asset", AssetViewSet, basename="asset")
asset_nested_router = NestedSimpleRouter(router, r"asset", lookup="asset")
Expand Down

0 comments on commit e440d46

Please sign in to comment.