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 and tests for prescription module #1555

Closed
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
101 changes: 98 additions & 3 deletions care/facility/api/serializers/prescription.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from care.facility.models import MedibaseMedicine, MedicineAdministration, Prescription
from care.users.api.serializers.user import UserBaseMinimumSerializer
from care.users.models import User


class MedibaseMedicineSerializer(serializers.ModelSerializer):
Expand All @@ -19,7 +20,8 @@ class Meta:
)


class PrescriptionSerializer(serializers.ModelSerializer):
class PrescriptionDetailSerializer(serializers.ModelSerializer):
# TODO: Remove when #5492 is merged
id = serializers.UUIDField(source="external_id", read_only=True)
prescribed_by = UserBaseMinimumSerializer(read_only=True)
last_administered_on = serializers.SerializerMethodField()
Expand Down Expand Up @@ -89,11 +91,12 @@ def validate(self, attrs):
# TODO: Ensure that this medicine is not already prescribed to the same patient and is currently active.


class MedicineAdministrationSerializer(serializers.ModelSerializer):
class MedicineAdministrationDetailSerializer(serializers.ModelSerializer):
# TODO: Remove when #5492 is merged
id = serializers.UUIDField(source="external_id", read_only=True)

administered_by = UserBaseMinimumSerializer(read_only=True)
prescription = PrescriptionSerializer(read_only=True)
prescription = PrescriptionDetailSerializer(read_only=True)

def validate_administered_date(self, value):
if value > timezone.now():
Expand All @@ -116,3 +119,95 @@ class Meta:
"modified_date",
"prescription",
)


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

class Meta:
model = MedibaseMedicine
fields = ["id", "name", "created_date", "modified_date"]
read_only_fields = (
"created_date",
"modified_date",
)


class PrescriptionBareMinimumSerializer(serializers.ModelSerializer):
id = serializers.UUIDField(source="external_id", read_only=True)
medicine_object = MedibaseMedicineBareMinimumSerializer(
read_only=True, source="medicine"
)

class Meta:
model = Prescription
fields = (
"id",
"medicine_object",
"medicine_old",
"created_date",
)


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


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

administered_by = MedicineAdministrationUserBareMinimumSerializer(read_only=True)
prescription = PrescriptionBareMinimumSerializer(read_only=True)

class Meta:
model = MedicineAdministration
fields = [
"id",
"prescription",
"created_date",
"administered_by",
"notes",
"modified_date",
]
read_only_fields = (
"id",
"administered_by",
"created_date",
"prescription",
"notes",
"modified_date",
)


class PrescriptionListSerializer(serializers.ModelSerializer):
id = serializers.UUIDField(source="external_id", read_only=True)
last_administered_on = serializers.SerializerMethodField()
medicine_object = MedibaseMedicineBareMinimumSerializer(
read_only=True, source="medicine"
)

class Meta:
model = Prescription
exclude = [
"created_date",
"external_id",
"deleted",
"meta",
"discontinued_date",
"is_migrated",
"consultation",
"medicine",
"prescribed_by",
]

def get_last_administered_on(self, obj):
last_administration = (
MedicineAdministration.objects.filter(prescription=obj)
.order_by("-created_date")
.first()
)
if last_administration:
return last_administration.created_date
return None
22 changes: 17 additions & 5 deletions care/facility/api/viewsets/prescription.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
from rest_framework.viewsets import GenericViewSet, ViewSet

from care.facility.api.serializers.prescription import (
MedicineAdministrationSerializer,
PrescriptionSerializer,
MedicineAdministrationDetailSerializer,
MedicineAdministrationListSerializer,
PrescriptionDetailSerializer,
PrescriptionListSerializer,
)
from care.facility.models import (
MedicineAdministration,
Expand Down Expand Up @@ -39,7 +41,7 @@ class MedicineAdminstrationFilter(filters.FilterSet):
class MedicineAdministrationViewSet(
mixins.ListModelMixin, mixins.RetrieveModelMixin, GenericViewSet
):
serializer_class = MedicineAdministrationSerializer
serializer_class = MedicineAdministrationDetailSerializer
permission_classes = (IsAuthenticated,)
queryset = MedicineAdministration.objects.all().order_by("-created_date")
lookup_field = "external_id"
Expand All @@ -57,6 +59,11 @@ def get_queryset(self):
consultation_obj = self.get_consultation_obj()
return self.queryset.filter(prescription__consultation_id=consultation_obj.id)

def get_serializer_class(self):
if self.action == "list":
return MedicineAdministrationListSerializer
return self.serializer_class


class ConsultationPrescriptionFilter(filters.FilterSet):
is_prn = filters.BooleanFilter()
Expand All @@ -69,13 +76,18 @@ class ConsultationPrescriptionViewSet(
mixins.RetrieveModelMixin,
GenericViewSet,
):
serializer_class = PrescriptionSerializer
serializer_class = PrescriptionDetailSerializer
permission_classes = (IsAuthenticated,)
queryset = Prescription.objects.all().order_by("-created_date")
lookup_field = "external_id"
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = ConsultationPrescriptionFilter

def get_serializer_class(self):
if self.action == "list":
return PrescriptionListSerializer
return self.serializer_class

def get_consultation_obj(self):
return get_object_or_404(
get_consultation_queryset(self.request.user).filter(
Expand Down Expand Up @@ -109,7 +121,7 @@ def discontinue(self, request, *args, **kwargs):
@action(
methods=["POST"],
detail=True,
serializer_class=MedicineAdministrationSerializer,
serializer_class=MedicineAdministrationDetailSerializer,
)
def administer(self, request, *args, **kwargs):
prescription_obj = self.get_object()
Expand Down
Loading