Skip to content

Commit

Permalink
Add patient ordering by category (#1235)
Browse files Browse the repository at this point in the history
Add ordering by patient category
  • Loading branch information
Ashesh3 committed Apr 6, 2023
1 parent fe89f5c commit 3e7f57b
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions care/facility/api/viewsets/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from django.conf import settings
from django.contrib.postgres.search import TrigramSimilarity
from django.core.validators import validate_email
from django.db import models
from django.db.models import Case, When
from django.db.models.query_utils import Q
from django.utils.timezone import localtime, now
from django_filters import rest_framework as filters
Expand All @@ -14,6 +16,7 @@
from rest_framework import mixins, serializers, status, viewsets
from rest_framework.decorators import action
from rest_framework.exceptions import ValidationError
from rest_framework.filters import BaseFilterBackend
from rest_framework.generics import get_object_or_404
from rest_framework.mixins import CreateModelMixin, ListModelMixin, RetrieveModelMixin
from rest_framework.pagination import PageNumberPagination
Expand Down Expand Up @@ -203,6 +206,30 @@ def filter_list_queryset(self, request, queryset, view):
return queryset.filter(facility_id__isnull=show_without_facility)


class PatientCustomOrderingFilter(BaseFilterBackend):
def filter_queryset(self, request, queryset, view):
ordering = request.query_params.get("ordering", "")

if ordering == "category_severity" or ordering == "-category_severity":
category_ordering = {
category: index + 1
for index, (category, _) in enumerate(CATEGORY_CHOICES)
}
when_statements = [
When(last_consultation__category=cat, then=order)
for cat, order in category_ordering.items()
]
queryset = queryset.annotate(
category_severity=Case(
*when_statements,
default=(len(category_ordering) + 1),
output_field=models.IntegerField(),
)
).order_by(ordering)

return queryset


class PatientViewSet(
HistoryMixin,
mixins.CreateModelMixin,
Expand Down Expand Up @@ -254,6 +281,7 @@ class PatientViewSet(
PatientDRYFilter,
filters.DjangoFilterBackend,
rest_framework_filters.OrderingFilter,
PatientCustomOrderingFilter,
)
filterset_class = PatientFilterSet

Expand Down

0 comments on commit 3e7f57b

Please sign in to comment.