Skip to content

Commit

Permalink
Filter out those facilities that are already linked to user (#1588)
Browse files Browse the repository at this point in the history
* template methos

* filter unlinked facilities

* rename params

* add tests

* fix import

* use facilityfilter

* refactor

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

Co-authored-by: Aakash Singh <[email protected]>

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

Co-authored-by: Aakash Singh <[email protected]>

* fix errors

* fix tests

* fix lint ci

* fix test cases

* fix filter

---------

Co-authored-by: Aakash Singh <[email protected]>
  • Loading branch information
Pranshu1902 and sainak authored Oct 30, 2023
1 parent 0daef7f commit 5428d66
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
6 changes: 6 additions & 0 deletions care/facility/api/viewsets/facility.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ class FacilityFilter(filters.FilterSet):
state = filters.NumberFilter(field_name="state__id")
state_name = filters.CharFilter(field_name="state__name", lookup_expr="icontains")
kasp_empanelled = filters.BooleanFilter(field_name="kasp_empanelled")
exclude_user = filters.CharFilter(method="filter_exclude_user")

def filter_exclude_user(self, queryset, name, value):
if value:
queryset = queryset.exclude(facilityuser__user__username=value)
return queryset


class FacilityQSPermissions(DRYPermissionFiltersBase):
Expand Down
28 changes: 24 additions & 4 deletions care/facility/tests/test_facilityuser_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from rest_framework import status
from rest_framework.test import APITestCase

from care.users.models import Skill
from care.utils.tests.test_utils import TestUtils


Expand All @@ -14,9 +13,16 @@ def setUpTestData(cls) -> None:
cls.super_user = cls.create_super_user("su", cls.district)
cls.facility = cls.create_facility(cls.super_user, cls.district, cls.local_body)
cls.user = cls.create_user("staff", cls.district, home_facility=cls.facility)
cls.skill1 = Skill.objects.create(name="Skill 1")
cls.skill2 = Skill.objects.create(name="Skill 2")
cls.user.skills.add(cls.skill1, cls.skill2)

cls.facility1 = cls.create_facility(
cls.super_user, cls.district, cls.local_body
)
cls.facility2 = cls.create_facility(
cls.super_user, cls.district, cls.local_body
)

def setUp(self) -> None:
self.client.force_authenticate(self.super_user)

def test_get_queryset_with_prefetching(self):
response = self.client.get(
Expand All @@ -25,3 +31,17 @@ def test_get_queryset_with_prefetching(self):

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertNumQueries(2)

def test_link_new_facility(self):
response = self.client.get("/api/v1/facility/")

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data["results"]), 3)

def test_link_existing_facility(self):
response = self.client.get(
f"/api/v1/facility/?exclude_user={self.user.username}"
)

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data["results"]), 2)

0 comments on commit 5428d66

Please sign in to comment.