Skip to content

Commit

Permalink
Merge pull request #2310 from coronasafe/last-active-filter
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshhari authored Jul 24, 2024
2 parents 14f1fe3 + 7e6e862 commit 32bf01b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
12 changes: 12 additions & 0 deletions care/users/api/viewsets/users.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
55 changes: 54 additions & 1 deletion care/users/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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"]}
)

0 comments on commit 32bf01b

Please sign in to comment.