diff --git a/rcpch_nhs_organisations/hospitals/admin.py b/rcpch_nhs_organisations/hospitals/admin.py index 8c38f3f..0d7f999 100644 --- a/rcpch_nhs_organisations/hospitals/admin.py +++ b/rcpch_nhs_organisations/hospitals/admin.py @@ -1,3 +1,15 @@ from django.contrib import admin # Register your models here. +from .models import ( + LocalHealthBoard, + Organisation, + PaediatricDiabetesUnit, + Trust, +) + +# Register your models here. +admin.site.register(LocalHealthBoard) +admin.site.register(Organisation) +admin.site.register(PaediatricDiabetesUnit) +admin.site.register(Trust) diff --git a/rcpch_nhs_organisations/hospitals/constants/paediatric_diabetes_units.py b/rcpch_nhs_organisations/hospitals/constants/paediatric_diabetes_units.py index 5b31768..6b677b0 100644 --- a/rcpch_nhs_organisations/hospitals/constants/paediatric_diabetes_units.py +++ b/rcpch_nhs_organisations/hospitals/constants/paediatric_diabetes_units.py @@ -170,8 +170,8 @@ {"ods_code": "RWFTW", "npda_code": "PZ216"}, {"ods_code": "RDU01", "npda_code": "PZ218"}, {"ods_code": "RCUEF", "npda_code": "PZ219"}, - {"ods_code": "RC979", "npda_code": "PZ220"}, # RC112 Bedford Hospital - {"ods_code": "RGT2X", "npda_code": "PZ220"}, # RC112 Luton and Dunstable Hospital + {"ods_code": "RC979", "npda_code": "PZ220"}, # Bedford Hospital + {"ods_code": "RGT2X", "npda_code": "PZ010"}, # Luton and Dunstable Hospital {"ods_code": "RN325", "npda_code": "PZ221"}, {"ods_code": "RL403", "npda_code": "PZ222"}, {"ods_code": "RXK01", "npda_code": "PZ223"}, @@ -187,4 +187,5 @@ "ods_code": "RGT1W", "npda_code": "PZ248", }, # Jersey General Hospital added 22/6/24 + {"ods_code": "8HV48", "npda_code": "PZ999"}, # RCPCH ] diff --git a/rcpch_nhs_organisations/hospitals/serializers/__init__.py b/rcpch_nhs_organisations/hospitals/serializers/__init__.py index 0e4baf8..15f52dc 100644 --- a/rcpch_nhs_organisations/hospitals/serializers/__init__.py +++ b/rcpch_nhs_organisations/hospitals/serializers/__init__.py @@ -25,4 +25,4 @@ PaediatricDiabetesUnitWithNestedOrganisationAndParentSerializer, ) from .paediatric_diabetes_unit import PaediatricDiabetesUnitSerializer -from .trust import TrustSerializer +from .trust import TrustSerializer, PaediatricDiabetesUnitWithNestedTrustSerializer diff --git a/rcpch_nhs_organisations/hospitals/serializers/trust.py b/rcpch_nhs_organisations/hospitals/serializers/trust.py index ccfb64c..e3c007c 100644 --- a/rcpch_nhs_organisations/hospitals/serializers/trust.py +++ b/rcpch_nhs_organisations/hospitals/serializers/trust.py @@ -1,8 +1,13 @@ from rest_framework import serializers +from django.apps import apps from drf_spectacular.utils import extend_schema_serializer, OpenApiExample -from ..models import Trust +from .local_health_board import LocalHealthBoardLimitedSerializer + +Organisation = apps.get_model("hospitals", "Organisation") +PaediatricDiabetesUnit = apps.get_model("hospitals", "PaediatricDiabetesUnit") +Trust = apps.get_model("hospitals", "Trust") @extend_schema_serializer( @@ -43,3 +48,88 @@ class Meta: "active", "published_at", ] + + +@extend_schema_serializer( + examples=[ + OpenApiExample( + "paediatric_diabetes_units/trust/", + value={"pz_code": "", "trust": []}, + response_only=True, + ) + ] +) +class PaediatricDiabetesUnitWithNestedTrustSerializer(serializers.ModelSerializer): + parent = serializers.SerializerMethodField() + primary_organisation = serializers.SerializerMethodField() + + class Meta: + model = PaediatricDiabetesUnit + fields = ["pz_code", "parent", "primary_organisation"] + + def get_parent(self, obj): + try: + # all related organisations for that PaediatricDiabetesUnit should have the same parent + # so we can just get the first one + organisation = Organisation.objects.filter( + paediatric_diabetes_unit=obj + ).first() + except Organisation.DoesNotExist: + return None + + if organisation.trust: + return TrustSerializer(organisation.trust).data + elif organisation.local_health_board: + return LocalHealthBoardLimitedSerializer( + organisation.local_health_board + ).data + return None + + def get_primary_organisation(self, obj): + # prevents circular import + from rcpch_nhs_organisations.hospitals.serializers.organisation import ( + OrganisationNoParentsSerializer, + ) + + try: + organisations = Organisation.objects.filter( + paediatric_diabetes_unit=obj + ).all() + except Organisation.DoesNotExist: + return None + + if organisations.count() > 1: + if obj.pz_code == "PZ024": + # RPF01 is the parent organisation for PZ024 (William Harvey Hospital, Ashford) + return OrganisationNoParentsSerializer( + organisations.filter(ods_code="RVV01").get() + ).data + elif obj.pz_code == "PZ050": + # RPF01 is the parent organisation for PZ024 (Queen Mary's Hospital for Children, Carshalton) + return OrganisationNoParentsSerializer( + organisations.filter(ods_code="RVR07").get() + ).data + elif obj.pz_code == "PZ099": + # RPF01 is the parent organisation for PZ099 (Lister Hospital, Stevenage) + return OrganisationNoParentsSerializer( + organisations.filter(ods_code="RWH01").get() + ).data + elif obj.pz_code == "PZ136": + # RPF01 is the parent organisation for PZ099 (Manchester Children's Hospital) + return OrganisationNoParentsSerializer( + organisations.filter(ods_code="R0A03").get() + ).data + elif obj.pz_code == "PZ206": + # RM401 is the parent organisation for PZ206 (Trafford General Hospital) + return OrganisationNoParentsSerializer( + organisations.filter(ods_code="RM321").get() + ).data + elif obj.pz_code == "PZ230": + # RM230 is the parent organisation for PZ230 (Conquest Hospital, Hastings) + return OrganisationNoParentsSerializer( + organisations.filter(ods_code="RXC01").get() + ).data + else: + return OrganisationNoParentsSerializer(organisations.first()).data + else: + return OrganisationNoParentsSerializer(organisations.get()).data diff --git a/rcpch_nhs_organisations/hospitals/urls.py b/rcpch_nhs_organisations/hospitals/urls.py index e3f6fb4..bb6a154 100644 --- a/rcpch_nhs_organisations/hospitals/urls.py +++ b/rcpch_nhs_organisations/hospitals/urls.py @@ -15,6 +15,7 @@ PaediatricDiabetesUnitViewSet, PaediatricDiabetesUnitWithNestedOrganisationsViewSet, PaediatricDiabetesUnitForOrganisationWithParentViewSet, + PaediatricDiabetesUnitForTrustViewSet, TrustViewSet, ) @@ -22,6 +23,8 @@ router = routers.DefaultRouter() +from django.contrib import admin + # returns a limited list of organisations by name and ods code router.register( r"organisations/limited", @@ -99,6 +102,12 @@ viewset=PaediatricDiabetesUnitWithNestedOrganisationsViewSet, basename="paediatric_diabetes_unit", ) +# returns a list of Paediatric Diabetes Units with nested trusts +router.register( + r"paediatric_diabetes_units/trust", + viewset=PaediatricDiabetesUnitForTrustViewSet, + basename="paediatric_diabetes_unit", +) drf_routes = [ # rest framework paths @@ -121,5 +130,7 @@ urlpatterns = [] +urlpatterns += (path("admin/", admin.site.urls),) + urlpatterns += drf_routes diff --git a/rcpch_nhs_organisations/hospitals/views/paediatric_diabetes_unit.py b/rcpch_nhs_organisations/hospitals/views/paediatric_diabetes_unit.py index a1cc8dd..d740d37 100644 --- a/rcpch_nhs_organisations/hospitals/views/paediatric_diabetes_unit.py +++ b/rcpch_nhs_organisations/hospitals/views/paediatric_diabetes_unit.py @@ -20,6 +20,7 @@ PaediatricDiabetesUnitSerializer, PaediatricDiabetesUnitWithNestedOrganisationSerializer, PaediatricDiabetesUnitWithNestedOrganisationAndParentSerializer, + PaediatricDiabetesUnitWithNestedTrustSerializer, ) @@ -149,3 +150,44 @@ def list(self, request, ods_code=None): queryset, many=True ) return Response(serializer.data) + + +@extend_schema( + request=PaediatricDiabetesUnitWithNestedTrustSerializer, + responses={ + 200: OpenApiResponse( + response=OpenApiTypes.OBJECT, + description="Valid Response", + examples=[ + OpenApiExample( + "paediatric_diabetes_units/trust/RGT/", + external_value="external value", + value={ + "pz_code": "PZ215", + }, + response_only="true", + ), + ], + ), + }, + parameters=[ + OpenApiParameter( + name="pz_code", + description="PZ Code of the Paediatric Diabetes Unit", + required=False, + type=OpenApiTypes.STR, + location=OpenApiParameter.QUERY, + ), + ], + summary="This endpoint returns the parent NHS Trust for a given Paediatric Diabetes Unit (with their primary organisation), against a PZ code. If no code is provide, a list is returned.", +) +class PaediatricDiabetesUnitForTrustViewSet(viewsets.ReadOnlyModelViewSet): + queryset = PaediatricDiabetesUnit.objects.all() + serializer_class = PaediatricDiabetesUnitWithNestedTrustSerializer + + def get_queryset(self): + queryset = super().get_queryset() + pz_code = self.request.query_params.get("pz_code", None) + if pz_code is not None: + queryset = queryset.filter(pz_code=pz_code) + return queryset