Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add list and detail serializer with tests for shifting module #1549 #2392

Merged
merged 13 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 122 additions & 2 deletions care/facility/api/serializers/shifting.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@
)
from care.facility.models.bed import ConsultationBed
from care.facility.models.notification import Notification
from care.facility.models.patient_base import NewDischargeReasonEnum
from care.facility.models.patient_base import (
DISEASE_STATUS_CHOICES,
DiseaseStatusEnum,
NewDischargeReasonEnum,
)
from care.facility.models.patient_consultation import PatientConsultation
from care.users.api.serializers.lsg import StateSerializer
from care.users.api.serializers.user import UserBaseMinimumSerializer
from care.utils.notification_handler import NotificationGenerator
from care.utils.serializer.external_id_field import ExternalIdSerializerField
Expand Down Expand Up @@ -436,7 +441,122 @@ class Meta:
read_only_fields = TIMESTAMP_FIELDS


class ShiftingRequestCommentSerializer(serializers.ModelSerializer):
class FacilityShiftingBareMinimumSerializer(serializers.ModelSerializer):
class Meta:
model = Facility
fields = ["id", "name"]


class PatientShiftingBareMinimumSerializer(serializers.ModelSerializer):
id = serializers.CharField(source="external_id", read_only=True)
facility = serializers.UUIDField(
source="facility.external_id", allow_null=True, read_only=True
)
facility_object = FacilityShiftingBareMinimumSerializer(
source="facility", read_only=True
)
state_object = StateSerializer(source="state", read_only=True)
disease_status = ChoiceField(
choices=DISEASE_STATUS_CHOICES, default=DiseaseStatusEnum.SUSPECTED.value
)
age = serializers.SerializerMethodField()

def get_age(self, obj):
return obj.get_age()

class Meta:
model = PatientRegistration
fields = [
"id",
"name",
"allow_transfer",
"age",
"phone_number",
"address",
"disease_status",
"facility",
"facility_object",
"state_object",
]
read_only = TIMESTAMP_FIELDS


class ShiftingListSerializer(serializers.ModelSerializer):
id = serializers.UUIDField(source="external_id", read_only=True)

patient = ExternalIdSerializerField(
queryset=PatientRegistration.objects.all(),
allow_null=False,
required=True,
)
patient_object = PatientShiftingBareMinimumSerializer(
source="patient", read_only=True
)

status = ChoiceField(choices=SHIFTING_STATUS_CHOICES)
origin_facility_object = FacilityShiftingBareMinimumSerializer(
source="origin_facility", read_only=True
)
shifting_approving_facility_object = FacilityShiftingBareMinimumSerializer(
source="shifting_approving_facility", read_only=True
)

assigned_facility = ExternalIdSerializerField(
queryset=Facility.objects.all(), allow_null=True, required=False
)
assigned_facility_external = serializers.CharField(
required=False, allow_null=True, allow_blank=True
)
assigned_facility_object = FacilityShiftingBareMinimumSerializer(
source="assigned_facility", read_only=True
)

class Meta:
model = ShiftingRequest
exclude = [
"created_by",
"last_edited_by",
"assigned_to",
"shifting_approving_facility",
"origin_facility",
"ambulance_number",
"ambulance_phone_number",
"ambulance_driver_name",
"is_assigned_to_user",
"is_kasp",
"refering_facility_contact_number",
"refering_facility_contact_name",
"comments",
"preferred_vehicle_choice",
"vehicle_preference",
"reason",
"is_up_shift",
"assigned_facility_type",
"deleted",
"breathlessness_level",
]
read_only_fields = TIMESTAMP_FIELDS


class ShiftingUserBareMinimumSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ["id", "first_name", "last_name"]


class ShiftingRequestCommentListSerializer(serializers.ModelSerializer):
id = serializers.UUIDField(source="external_id", read_only=True)
comment = serializers.CharField(required=True)
created_by_object = ShiftingUserBareMinimumSerializer(
source="created_by", read_only=True
)

class Meta:
model = ShiftingRequestComment
fields = ["id", "comment", "modified_date", "created_by_object"]


class ShiftingRequestCommentDetailSerializer(ShiftingRequestCommentListSerializer):
id = serializers.UUIDField(source="external_id", read_only=True)

created_by_object = UserBaseMinimumSerializer(source="created_by", read_only=True)
Expand Down
90 changes: 57 additions & 33 deletions care/facility/api/viewsets/shifting.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.conf import settings
from django.db.models.query import QuerySet
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 @@ -15,7 +16,9 @@

from care.facility.api.serializers.shifting import (
ShiftingDetailSerializer,
ShiftingRequestCommentSerializer,
ShiftingListSerializer,
ShiftingRequestCommentDetailSerializer,
ShiftingRequestCommentListSerializer,
ShiftingSerializer,
has_facility_permission,
)
Expand Down Expand Up @@ -93,36 +96,7 @@ class ShiftingViewSet(
):
serializer_class = ShiftingSerializer
lookup_field = "external_id"
queryset = ShiftingRequest.objects.all().select_related(
"origin_facility",
"origin_facility__ward",
"origin_facility__local_body",
"origin_facility__district",
"origin_facility__state",
"shifting_approving_facility",
"shifting_approving_facility__ward",
"shifting_approving_facility__local_body",
"shifting_approving_facility__district",
"shifting_approving_facility__state",
"assigned_facility",
"assigned_facility__ward",
"assigned_facility__local_body",
"assigned_facility__district",
"assigned_facility__state",
"patient",
"patient__ward",
"patient__local_body",
"patient__district",
"patient__state",
"patient__facility",
"patient__facility__ward",
"patient__facility__local_body",
"patient__facility__district",
"patient__facility__state",
"assigned_to",
"created_by",
"last_edited_by",
)
queryset = ShiftingRequest.objects.all()
ordering_fields = ["id", "created_date", "modified_date", "emergency"]

permission_classes = (IsAuthenticated, DRYPermissions)
Expand All @@ -133,8 +107,52 @@ class ShiftingViewSet(
)
filterset_class = ShiftingFilterSet

def get_queryset(self) -> QuerySet:
if self.action == "list":
self.queryset = self.queryset.select_related(
"origin_facility",
"shifting_approving_facility",
"assigned_facility",
"patient",
)

else:
self.queryset = self.queryset.select_related(
"origin_facility",
"origin_facility__ward",
"origin_facility__local_body",
"origin_facility__district",
"origin_facility__state",
"shifting_approving_facility",
"shifting_approving_facility__ward",
"shifting_approving_facility__local_body",
"shifting_approving_facility__district",
"shifting_approving_facility__state",
"assigned_facility",
"assigned_facility__ward",
"assigned_facility__local_body",
"assigned_facility__district",
"assigned_facility__state",
"patient",
"patient__ward",
"patient__local_body",
"patient__district",
"patient__state",
"patient__facility",
"patient__facility__ward",
"patient__facility__local_body",
"patient__facility__district",
"patient__facility__state",
"assigned_to",
"created_by",
"last_edited_by",
)
return self.queryset

def get_serializer_class(self):
serializer_class = self.serializer_class
if self.action == "list":
return ShiftingListSerializer
if self.action == "retrieve":
serializer_class = ShiftingDetailSerializer
return serializer_class
Expand Down Expand Up @@ -186,7 +204,8 @@ def list(self, request, *args, **kwargs):
field_header_map=ShiftingRequest.CSV_MAPPING,
field_serializer_map=ShiftingRequest.CSV_MAKE_PRETTY,
)
return super(ShiftingViewSet, self).list(request, *args, **kwargs)
response = super().list(request, *args, **kwargs)
return response


class ShifitngRequestCommentViewSet(
Expand All @@ -195,7 +214,7 @@ class ShifitngRequestCommentViewSet(
mixins.RetrieveModelMixin,
GenericViewSet,
):
serializer_class = ShiftingRequestCommentSerializer
serializer_class = ShiftingRequestCommentDetailSerializer
lookup_field = "external_id"
queryset = ShiftingRequestComment.objects.all().order_by("-created_date")

Expand Down Expand Up @@ -243,3 +262,8 @@ def get_request(self):

def perform_create(self, serializer):
serializer.save(request=self.get_request())

def get_serializer_class(self):
if self.action == "list":
return ShiftingRequestCommentListSerializer
return ShiftingRequestCommentDetailSerializer
Loading