11from django .core .exceptions import ObjectDoesNotExist
2- from django .db .models import CharField , Q
2+ from django .db .models import CharField , Q , Count , Max
33from django .db .models .functions import Cast
44from django .utils .decorators import method_decorator
55from django_filters import ( # pylint: disable=E0401
4141 PatientHospitalMappingWriteSerializer ,
4242 PreferredHospitalReadSerializer ,
4343 ReadPatientSerializer ,
44+ SurgeonEpisodeSummarySerializer , OwnedEpisodeSerializer ,
4445)
4546from ...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+
70115class PatientFilterSet (FilterSet ):
71116 hospital_id = NumberFilter (
72117 method = "filter_hospital" ,
0 commit comments