|
15 | 15 | from rest_framework.decorators import action |
16 | 16 | from rest_framework.exceptions import NotFound |
17 | 17 | from rest_framework.mixins import CreateModelMixin, RetrieveModelMixin |
| 18 | +from rest_framework.permissions import IsAuthenticated |
18 | 19 | from rest_framework.response import Response |
19 | 20 | from rest_framework.viewsets import GenericViewSet |
20 | 21 |
|
|
25 | 26 | Hospital, |
26 | 27 | Patient, |
27 | 28 | PatientHospitalMapping, |
| 29 | + PreferredHospital, |
28 | 30 | ) |
29 | 31 | from .serializers import ( |
30 | 32 | CreatePatientSerializer, |
|
37 | 39 | HospitalSerializer, |
38 | 40 | PatientHospitalMappingReadSerializer, |
39 | 41 | PatientHospitalMappingWriteSerializer, |
| 42 | + PreferredHospitalReadSerializer, |
40 | 43 | ReadPatientSerializer, |
41 | 44 | ) |
| 45 | +from ...users.models import MedicalPersonnel |
42 | 46 |
|
43 | 47 |
|
44 | 48 | class HospitalViewSet(viewsets.ReadOnlyModelViewSet): |
45 | 49 | queryset = Hospital.objects.all() |
46 | 50 | serializer_class = HospitalSerializer |
47 | 51 |
|
| 52 | +class PreferredHospitalViewSet(viewsets.GenericViewSet): |
| 53 | + serializer_class = PreferredHospitalReadSerializer |
| 54 | + permission_classes = [IsAuthenticated] |
| 55 | + |
| 56 | + @action(detail=False, methods=['get']) |
| 57 | + def retrieve_for_current_user(self, request, *args, **kwargs): |
| 58 | + user = request.user |
| 59 | + try: |
| 60 | + medical_personnel = MedicalPersonnel.objects.get(user=user) |
| 61 | + except MedicalPersonnel.DoesNotExist: |
| 62 | + return Response({}, status=200) |
| 63 | + try: |
| 64 | + preferred_hospital = PreferredHospital.objects.get(medical_personnel=medical_personnel) |
| 65 | + except PreferredHospital.DoesNotExist: |
| 66 | + return Response({}, status=200) |
| 67 | + serializer = self.get_serializer(preferred_hospital) |
| 68 | + return Response(serializer.data) |
48 | 69 |
|
49 | 70 | class PatientFilterSet(FilterSet): |
50 | 71 | hospital_id = NumberFilter( |
|
0 commit comments