Skip to content

Commit 84b2260

Browse files
committed
Implemented a first version of a surgeon dashboard landing page
1 parent e892866 commit 84b2260

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

tmh_registry/registry/api/serializers.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from drf_yasg.utils import swagger_serializer_method
22
from rest_framework.exceptions import ValidationError
3-
from rest_framework.fields import BooleanField, CharField, IntegerField
3+
from rest_framework.fields import BooleanField, CharField, IntegerField, DateField
44
from rest_framework.relations import PrimaryKeyRelatedField
5-
from rest_framework.serializers import ModelSerializer, SerializerMethodField
5+
from rest_framework.serializers import ModelSerializer, SerializerMethodField, Serializer
66

77
from ...common.utils.functions import get_text_choice_value_from_label
88
from ...users.api.serializers import MedicalPersonnelSerializer
@@ -645,3 +645,28 @@ def create(self, validated_data):
645645
follow_up.attendees.set(attendees)
646646

647647
return follow_up
648+
649+
class SurgeonEpisodeSummarySerializer(Serializer):
650+
episode_count = IntegerField()
651+
last_episode_date = DateField(allow_null=True)
652+
653+
654+
class OwnedEpisodeSerializer(ModelSerializer):
655+
patient_name = CharField(source='patient_hospital_mapping.patient.full_name', read_only=True)
656+
patient_id = CharField(source='patient_hospital_mapping.patient.id', read_only=True)
657+
hospital_id = CharField(source='patient_hospital_mapping.hospital.id', read_only=True)
658+
follow_up_dates = SerializerMethodField()
659+
discharge = SerializerMethodField()
660+
661+
class Meta:
662+
model = Episode
663+
fields = ['id', 'surgery_date', 'patient_name', 'discharge', 'follow_up_dates', 'patient_id', 'hospital_id']
664+
665+
def get_follow_up_dates(self, obj):
666+
# Return only the dates of follow-up objects as a list of strings
667+
return [follow_up.date for follow_up in obj.followup_set.all()]
668+
669+
def get_discharge(self, obj):
670+
# Return the discharge date if available, otherwise None
671+
discharge = getattr(obj, 'discharge', None)
672+
return discharge.date if discharge else None

tmh_registry/registry/api/viewsets.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.core.exceptions import ObjectDoesNotExist
2-
from django.db.models import CharField, Q
2+
from django.db.models import CharField, Q, Count, Max
33
from django.db.models.functions import Cast
44
from django.utils.decorators import method_decorator
55
from django_filters import ( # pylint: disable=E0401
@@ -41,6 +41,7 @@
4141
PatientHospitalMappingWriteSerializer,
4242
PreferredHospitalReadSerializer,
4343
ReadPatientSerializer,
44+
SurgeonEpisodeSummarySerializer, OwnedEpisodeSerializer,
4445
)
4546
from ...users.models import MedicalPersonnel
4647

@@ -67,6 +68,50 @@ def retrieve_for_current_user(self, request, *args, **kwargs):
6768
serializer = self.get_serializer(preferred_hospital)
6869
return Response(serializer.data)
6970

71+
class SurgeonEpisodeSummaryViewSet(viewsets.ReadOnlyModelViewSet):
72+
serializer_class = SurgeonEpisodeSummarySerializer
73+
permission_classes = [IsAuthenticated]
74+
75+
def get_queryset(self):
76+
user = self.request.user
77+
try:
78+
medical_personnel = MedicalPersonnel.objects.get(user=user)
79+
except MedicalPersonnel.DoesNotExist:
80+
return Episode.objects.none()
81+
82+
return Episode.objects.filter(surgeons=medical_personnel)
83+
84+
def list(self, request, *args, **kwargs):
85+
queryset = self.get_queryset()
86+
87+
stats = queryset.aggregate(
88+
episode_count=Count('id'),
89+
last_episode_date=Max('surgery_date')
90+
)
91+
92+
serializer = self.get_serializer(stats)
93+
return Response(serializer.data)
94+
95+
class OwnedEpisodesViewSet(viewsets.ReadOnlyModelViewSet):
96+
pagination_class = None
97+
serializer_class = OwnedEpisodeSerializer
98+
permission_classes = [IsAuthenticated]
99+
100+
def get_queryset(self):
101+
user = self.request.user
102+
try:
103+
medical_personnel = MedicalPersonnel.objects.get(user=user)
104+
except MedicalPersonnel.DoesNotExist:
105+
return Episode.objects.none()
106+
107+
episodes = (
108+
Episode.objects
109+
.filter(surgeons=medical_personnel)
110+
.select_related("patient_hospital_mapping__patient")
111+
.prefetch_related( "discharge", "followup_set")
112+
)
113+
return episodes
114+
70115
class PatientFilterSet(FilterSet):
71116
hospital_id = NumberFilter(
72117
method="filter_hospital",

tmh_registry/registry/urls.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
PatientHospitalMappingViewset,
1010
PatientViewSet,
1111
PreferredHospitalViewSet,
12+
SurgeonEpisodeSummaryViewSet,
13+
OwnedEpisodesViewSet,
1214
)
1315

1416
router = DefaultRouter()
@@ -19,6 +21,9 @@
1921
router.register(r"discharges", DischargeViewset)
2022
router.register(r"follow-ups", FollowUpViewset)
2123
router.register(r"preferred-hospital", PreferredHospitalViewSet, basename='preferred-hospital')
24+
router.register(r'surgeon-episode-summary', SurgeonEpisodeSummaryViewSet, basename='surgeon-episode-summary')
25+
router.register(r"owned-episodes", OwnedEpisodesViewSet, basename='owned-episodes')
26+
2227

2328
urlpatterns = [
2429
path("", include(router.urls)),

0 commit comments

Comments
 (0)