Skip to content

Commit

Permalink
made neccessary changes for prefetching the skills
Browse files Browse the repository at this point in the history
  • Loading branch information
yaswanthsaivendra committed May 27, 2023
1 parent a75b98d commit f59ff6b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
17 changes: 12 additions & 5 deletions care/facility/api/viewsets/facility_users.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.core.exceptions import ObjectDoesNotExist
from django_filters import rest_framework as filters
from rest_framework import mixins
from rest_framework.exceptions import ValidationError
Expand All @@ -10,8 +11,10 @@


class UserFilter(filters.FilterSet):
user_type = filters.TypedChoiceFilter(choices=[(key, key) for key in User.TYPE_VALUE_MAP.keys()],
coerce=lambda role: User.TYPE_VALUE_MAP[role])
user_type = filters.TypedChoiceFilter(
choices=[(key, key) for key in User.TYPE_VALUE_MAP.keys()],
coerce=lambda role: User.TYPE_VALUE_MAP[role],
)

class Meta:
model = User
Expand All @@ -27,7 +30,11 @@ class FacilityUserViewSet(GenericViewSet, mixins.ListModelMixin):

def get_queryset(self):
try:
facility = Facility.objects.get(external_id=self.kwargs.get("facility_external_id"))
return facility.users.filter(deleted=False).order_by("-last_login")
except:
facility = Facility.objects.get(
external_id=self.kwargs.get("facility_external_id")
)
queryset = facility.users.filter(deleted=False).order_by("-last_login")
queryset = self.get_serializer_class().skills_eager_loading(queryset)
return queryset
except ObjectDoesNotExist:
raise ValidationError({"Facility": "Facility not found"})
11 changes: 10 additions & 1 deletion care/facility/api/viewsets/patient_consultation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.core.validators import validate_email
from django.db.models import Prefetch
from django.db.models.query_utils import Q
from django_filters import rest_framework as filters
from drf_yasg import openapi
Expand All @@ -25,7 +26,7 @@
email_discharge_summary,
generate_and_upload_discharge_summary_task,
)
from care.users.models import User
from care.users.models import Skill, User
from care.utils.cache.cache_allowed_facilities import get_accessible_facilities


Expand Down Expand Up @@ -68,6 +69,14 @@ def get_permissions(self):
return super().get_permissions()

def get_queryset(self):
if self.serializer_class == PatientConsultationSerializer:
self.queryset = self.queryset.prefetch_related(
"assigned_to",
Prefetch(
"assigned_to__skills",
queryset=Skill.objects.filter(userskill__deleted=False),
),
)
if self.request.user.is_superuser:
return self.queryset
elif self.request.user.user_type >= User.TYPE_VALUE_MAP["StateLabAdmin"]:
Expand Down
22 changes: 16 additions & 6 deletions care/users/api/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.contrib.auth import get_user_model
from django.contrib.auth.hashers import make_password
from django.db import transaction
from django.db.models import Prefetch
from rest_framework import exceptions, serializers

from care.facility.api.serializers.facility import FacilityBareMinimumSerializer
Expand All @@ -13,7 +14,7 @@
StateSerializer,
)
from care.users.api.serializers.skill import SkillSerializer
from care.users.models import GENDER_CHOICES
from care.users.models import GENDER_CHOICES, Skill
from care.utils.queryset.facility import get_home_facility_queryset
from care.utils.serializer.external_id_field import ExternalIdSerializerField
from care.utils.serializer.phonenumber_ispossible_field import (
Expand Down Expand Up @@ -358,11 +359,20 @@ class UserAssignedSerializer(serializers.ModelSerializer):
home_facility_object = FacilityBareMinimumSerializer(
source="home_facility", read_only=True
)
skills = serializers.SerializerMethodField()

def get_skills(self, obj):
qs = obj.skills.filter(userskill__deleted=False)
return SkillSerializer(qs, many=True).data
# skills = serializers.SerializerMethodField()
skills = SkillSerializer(many=True, read_only=True)

# def get_skills(self, obj):
# qs = obj.skills.filter(userskill__deleted=False)
# return SkillSerializer(qs, many=True).data

@staticmethod
def skills_eager_loading(queryset):
"""Perform necessary eager loading of data."""
queryset = queryset.prefetch_related(
Prefetch("skills", queryset=Skill.objects.filter(userskill__deleted=False))
)
return queryset

class Meta:
model = User
Expand Down

0 comments on commit f59ff6b

Please sign in to comment.