Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to filter users with no home facility #2332

Merged
merged 4 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions care/users/api/viewsets/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ class UserFilterSet(filters.FilterSet):
)
last_login = filters.DateFromToRangeFilter(field_name="last_login")
district_id = filters.NumberFilter(field_name="district_id", lookup_expr="exact")
home_facility = filters.UUIDFilter(
field_name="home_facility__external_id", lookup_expr="exact"
)
home_facility = filters.CharFilter(method="filter_home_facility")

def filter_home_facility(self, queryset, name, value):
if value == "NONE":
return queryset.filter(home_facility__isnull=True)
return queryset.filter(home_facility__external_id=value)

last_active_days = filters.CharFilter(method="last_active_after")

def get_user_type(
Expand Down
41 changes: 40 additions & 1 deletion care/users/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,22 @@ def setUpTestData(cls) -> None:
cls.local_body = cls.create_local_body(cls.district)
cls.super_user = cls.create_super_user("su", cls.district)
cls.facility = cls.create_facility(cls.super_user, cls.district, cls.local_body)
cls.facility_2 = cls.create_facility(
cls.super_user, cls.district, cls.local_body
)

cls.user_1 = cls.create_user("staff1", cls.district, home_facility=cls.facility)

cls.user_2 = cls.create_user("staff2", cls.district, home_facility=cls.facility)

cls.user_3 = cls.create_user("staff3", cls.district, home_facility=cls.facility)

cls.user_4 = cls.create_user(
"staff4", cls.district, home_facility=cls.facility_2
)

cls.user_5 = cls.create_user("doctor", cls.district)

def setUp(self):
self.client.force_authenticate(self.super_user)
self.user_1.last_login = timezone.now() - timedelta(hours=1)
Expand Down Expand Up @@ -248,7 +257,37 @@ def test_last_active_filter(self):
response = self.client.get("/api/v1/users/?last_active_days=never")
self.assertEqual(response.status_code, status.HTTP_200_OK)
res_data_json = response.json()
self.assertEqual(res_data_json["count"], 1)
self.assertEqual(res_data_json["count"], 3)
self.assertIn(
self.user_3.username, {r["username"] for r in res_data_json["results"]}
)

def test_home_facility_filter(self):
"""Test home facility filter"""
response = self.client.get("/api/v1/users/?home_facility=NOT_A_VALID_UUID")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
response = self.client.get(
f"/api/v1/users/?home_facility={self.facility.external_id}"
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
res_data_json = response.json()
self.assertEqual(res_data_json["count"], 3)
self.assertIn(
self.user_1.username, {r["username"] for r in res_data_json["results"]}
)

response = self.client.get(
f"/api/v1/users/?home_facility={self.facility_2.external_id}"
)
res_data_json = response.json()
self.assertEqual(res_data_json["count"], 1)
self.assertIn(
self.user_4.username, {r["username"] for r in res_data_json["results"]}
)

response = self.client.get("/api/v1/users/?home_facility=NONE")
res_data_json = response.json()
self.assertEqual(res_data_json["count"], 1)
self.assertIn(
self.user_5.username, {r["username"] for r in res_data_json["results"]}
)