diff --git a/care/users/api/viewsets/users.py b/care/users/api/viewsets/users.py index bbf587dec7..d581b170c0 100644 --- a/care/users/api/viewsets/users.py +++ b/care/users/api/viewsets/users.py @@ -1,6 +1,9 @@ +from datetime import timedelta + from django.core.cache import cache from django.db.models import F, Q, Subquery from django.http import Http404 +from django.utils import timezone from django_filters import rest_framework as filters from drf_spectacular.utils import extend_schema from dry_rest_permissions.generics import DRYPermissions @@ -57,6 +60,7 @@ class UserFilterSet(filters.FilterSet): home_facility = filters.UUIDFilter( field_name="home_facility__external_id", lookup_expr="exact" ) + last_active_days = filters.CharFilter(method="last_active_after") def get_user_type( self, @@ -71,6 +75,14 @@ def get_user_type( user_type = filters.CharFilter(method="get_user_type", field_name="user_type") + def last_active_after(self, queryset, name, value): + if value == "never": + return queryset.filter(last_login__isnull=True) + # convert days to date + date = timezone.now() - timedelta(days=int(value)) + + return queryset.filter(last_login__gte=date) + class UserViewSet( mixins.RetrieveModelMixin, diff --git a/care/users/tests/test_api.py b/care/users/tests/test_api.py index 6ad1342f93..bcd7216e0d 100644 --- a/care/users/tests/test_api.py +++ b/care/users/tests/test_api.py @@ -1,5 +1,6 @@ -from datetime import date +from datetime import date, timedelta +from django.utils import timezone from rest_framework import status from rest_framework.test import APITestCase @@ -200,3 +201,55 @@ def test_user_cannot_delete_others(self): self.data_2[field], User.objects.get(username=self.data_2[field]).username, ) + + +class TestUserFilter(TestUtils, APITestCase): + @classmethod + def setUpTestData(cls) -> None: + cls.state = cls.create_state() + cls.district = cls.create_district(cls.state) + 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.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) + + def setUp(self): + self.client.force_authenticate(self.super_user) + self.user_1.last_login = timezone.now() - timedelta(hours=1) + self.user_1.save() + self.user_2.last_login = timezone.now() - timedelta(days=5) + self.user_2.save() + self.user_3.last_login = None + self.user_3.save() + + def test_last_active_filter(self): + """Test last active filter""" + response = self.client.get("/api/v1/users/?last_active_days=1") + self.assertEqual(response.status_code, status.HTTP_200_OK) + res_data_json = response.json() + self.assertEqual(res_data_json["count"], 1) + self.assertIn( + self.user_1.username, {r["username"] for r in res_data_json["results"]} + ) + + response = self.client.get("/api/v1/users/?last_active_days=10") + self.assertEqual(response.status_code, status.HTTP_200_OK) + res_data_json = response.json() + print(res_data_json) + self.assertEqual(res_data_json["count"], 2) + self.assertIn( + self.user_2.username, {r["username"] for r in res_data_json["results"]} + ) + + 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.assertIn( + self.user_3.username, {r["username"] for r in res_data_json["results"]} + )