From 81823a90bd6827ad762c1194c8f4955108b0ec6e Mon Sep 17 00:00:00 2001 From: Aakash Singh Date: Sat, 3 Sep 2022 00:19:13 +0530 Subject: [PATCH] add rounds_type filter for daily rounds (#996) --- care/facility/api/viewsets/daily_round.py | 18 ++++++++++++++++++ care/facility/models/daily_round.py | 2 ++ 2 files changed, 20 insertions(+) diff --git a/care/facility/api/viewsets/daily_round.py b/care/facility/api/viewsets/daily_round.py index f856c77a3c..271cecedd8 100644 --- a/care/facility/api/viewsets/daily_round.py +++ b/care/facility/api/viewsets/daily_round.py @@ -1,3 +1,4 @@ +from django_filters import rest_framework as filters from dry_rest_permissions.generics import DRYPermissions from rest_framework import mixins from rest_framework.decorators import action @@ -16,6 +17,20 @@ DailyRoundAttributes = [f.name for f in DailyRound._meta.get_fields()] +class DailyRoundFilterSet(filters.FilterSet): + rounds_type = filters.CharFilter(method="filter_rounds_type") + + def filter_rounds_type(self, queryset, name, value): + rounds_type = set() + values = value.split(",") + for v in values: + try: + rounds_type.add(DailyRound.RoundsTypeDict[v]) + except KeyError: + pass + return queryset.filter(rounds_type__in=list(rounds_type)) + + class DailyRoundsViewSet( AssetUserAccessMixin, mixins.CreateModelMixin, @@ -31,6 +46,9 @@ class DailyRoundsViewSet( ) queryset = DailyRound.objects.all().order_by("-id") lookup_field = "external_id" + filterset_class = DailyRoundFilterSet + + filter_backends = (filters.DjangoFilterBackend,) FIELDS_KEY = "fields" MAX_FIELDS = 20 diff --git a/care/facility/models/daily_round.py b/care/facility/models/daily_round.py index 20f319a652..e0fe835728 100644 --- a/care/facility/models/daily_round.py +++ b/care/facility/models/daily_round.py @@ -6,6 +6,7 @@ from multiselectfield import MultiSelectField from care.facility.models import CATEGORY_CHOICES, PatientBaseModel +from care.facility.models.base import covert_choice_dict from care.facility.models.bed import AssetBed from care.facility.models.json_schema.daily_round import ( BLOOD_PRESSURE, @@ -31,6 +32,7 @@ class RoundsType(enum.Enum): AUTOMATED = 300 RoundsTypeChoice = [(e.value, e.name) for e in RoundsType] + RoundsTypeDict = covert_choice_dict(RoundsTypeChoice) class ConsciousnessType(enum.Enum): UNKNOWN = 0