Skip to content

Commit e892866

Browse files
committed
Added preferred hospital functionality
1 parent f0c3734 commit e892866

File tree

6 files changed

+89
-1
lines changed

6 files changed

+89
-1
lines changed

tmh_registry/registry/admin.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Hospital,
1212
Patient,
1313
PatientHospitalMapping,
14+
PreferredHospital,
1415
)
1516

1617

@@ -38,6 +39,9 @@ class PatientAdmin(ExportMixin, admin.ModelAdmin):
3839
class PatientHospitalMappingAdmin(ExportMixin, admin.ModelAdmin):
3940
model = PatientHospitalMapping
4041

42+
@admin.register(PreferredHospital)
43+
class PreferredHospitalAdmin(ExportMixin, admin.ModelAdmin):
44+
model = PreferredHospital
4145

4246
@admin.register(Episode)
4347
class EpisodeAdmin(ExportMixin, admin.ModelAdmin):

tmh_registry/registry/api/serializers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
Hospital,
1515
Patient,
1616
PatientHospitalMapping,
17+
PreferredHospital,
1718
)
1819

1920

@@ -239,6 +240,23 @@ def to_representation(self, instance):
239240

240241
return data
241242

243+
class PreferredHospitalReadSerializer(ModelSerializer):
244+
hospital = SerializerMethodField()
245+
246+
class Meta:
247+
model = PreferredHospital
248+
fields = ["hospital"]
249+
250+
def get_hospital(self, obj):
251+
request = self.context.get("request")
252+
if request and request.user:
253+
try:
254+
medical_personnel = MedicalPersonnel.objects.get(user=request.user)
255+
if obj.medical_personnel == medical_personnel:
256+
return {"id": obj.hospital.id}
257+
except MedicalPersonnel.DoesNotExist:
258+
pass
259+
return None
242260

243261
class PatientHospitalMappingWriteSerializer(ModelSerializer):
244262
patient_id = PrimaryKeyRelatedField(queryset=Patient.objects.all())

tmh_registry/registry/api/viewsets.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from rest_framework.decorators import action
1616
from rest_framework.exceptions import NotFound
1717
from rest_framework.mixins import CreateModelMixin, RetrieveModelMixin
18+
from rest_framework.permissions import IsAuthenticated
1819
from rest_framework.response import Response
1920
from rest_framework.viewsets import GenericViewSet
2021

@@ -25,6 +26,7 @@
2526
Hospital,
2627
Patient,
2728
PatientHospitalMapping,
29+
PreferredHospital,
2830
)
2931
from .serializers import (
3032
CreatePatientSerializer,
@@ -37,14 +39,33 @@
3739
HospitalSerializer,
3840
PatientHospitalMappingReadSerializer,
3941
PatientHospitalMappingWriteSerializer,
42+
PreferredHospitalReadSerializer,
4043
ReadPatientSerializer,
4144
)
45+
from ...users.models import MedicalPersonnel
4246

4347

4448
class HospitalViewSet(viewsets.ReadOnlyModelViewSet):
4549
queryset = Hospital.objects.all()
4650
serializer_class = HospitalSerializer
4751

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)
4869

4970
class PatientFilterSet(FilterSet):
5071
hospital_id = NumberFilter(
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Generated by Django 3.1.3 on 2024-07-29 13:58
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('users', '0008_auto_20210630_1407'),
11+
('registry', '0036_followup_recurrence'),
12+
]
13+
14+
operations = [
15+
migrations.CreateModel(
16+
name='PreferredHospital',
17+
fields=[
18+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
19+
('hospital', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='preferred_by_medical_personnel', to='registry.hospital')),
20+
('medical_personnel', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='preferred_hospital', to='users.medicalpersonnel')),
21+
],
22+
options={
23+
'verbose_name_plural': 'Medical Personnel Preferred Hospitals',
24+
'unique_together': {('medical_personnel', 'hospital')},
25+
},
26+
),
27+
]

tmh_registry/registry/models.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,27 @@ class Meta:
7373
("patient", "hospital"),
7474
("hospital", "patient_hospital_id"),
7575
)
76-
verbose_name_plural = "Patient-Hospital mappings"
76+
verbose_name_plural = "Patient-Hospital Mappings"
7777

7878
def __str__(self):
7979
return f"Patient {self.patient.full_name} ({self.patient_hospital_id}) - Hospital {self.hospital.name}"
8080

81+
class PreferredHospital(Model):
82+
medical_personnel = OneToOneField(
83+
MedicalPersonnel, on_delete=CASCADE, related_name="preferred_hospital"
84+
)
85+
hospital = ForeignKey(
86+
Hospital, on_delete=CASCADE, related_name="preferred_by_medical_personnel"
87+
)
88+
89+
class Meta:
90+
unique_together = (
91+
("medical_personnel", "hospital"),
92+
)
93+
verbose_name_plural = "Medical Personnel Preferred Hospitals"
94+
95+
def __str__(self):
96+
return f"{self.medical_personnel.user.first_name} {self.medical_personnel.user.last_name} ({self.medical_personnel.user.username}) - Hospital {self.hospital.name}"
8197

8298
class Episode(Model):
8399
class EpisodeChoices(TextChoices):

tmh_registry/registry/urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
HospitalViewSet,
99
PatientHospitalMappingViewset,
1010
PatientViewSet,
11+
PreferredHospitalViewSet,
1112
)
1213

1314
router = DefaultRouter()
@@ -17,6 +18,7 @@
1718
router.register(r"episodes", EpisodeViewset)
1819
router.register(r"discharges", DischargeViewset)
1920
router.register(r"follow-ups", FollowUpViewset)
21+
router.register(r"preferred-hospital", PreferredHospitalViewSet, basename='preferred-hospital')
2022

2123
urlpatterns = [
2224
path("", include(router.urls)),

0 commit comments

Comments
 (0)