Skip to content

Commit

Permalink
Merge pull request #24 from rcpch:rcpch-pdu
Browse files Browse the repository at this point in the history
Rcpch-pdu
  • Loading branch information
eatyourpeas authored Aug 16, 2024
2 parents dc35007 + 82ba9f7 commit db3f243
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 4 deletions.
12 changes: 12 additions & 0 deletions rcpch_nhs_organisations/hospitals/admin.py
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand All @@ -187,4 +187,5 @@
"ods_code": "RGT1W",
"npda_code": "PZ248",
}, # Jersey General Hospital added 22/6/24
{"ods_code": "8HV48", "npda_code": "PZ999"}, # RCPCH
]
2 changes: 1 addition & 1 deletion rcpch_nhs_organisations/hospitals/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
PaediatricDiabetesUnitWithNestedOrganisationAndParentSerializer,
)
from .paediatric_diabetes_unit import PaediatricDiabetesUnitSerializer
from .trust import TrustSerializer
from .trust import TrustSerializer, PaediatricDiabetesUnitWithNestedTrustSerializer
92 changes: 91 additions & 1 deletion rcpch_nhs_organisations/hospitals/serializers/trust.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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
11 changes: 11 additions & 0 deletions rcpch_nhs_organisations/hospitals/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
PaediatricDiabetesUnitViewSet,
PaediatricDiabetesUnitWithNestedOrganisationsViewSet,
PaediatricDiabetesUnitForOrganisationWithParentViewSet,
PaediatricDiabetesUnitForTrustViewSet,
TrustViewSet,
)

from drf_spectacular.views import SpectacularJSONAPIView, SpectacularSwaggerView

router = routers.DefaultRouter()

from django.contrib import admin

# returns a limited list of organisations by name and ods code
router.register(
r"organisations/limited",
Expand Down Expand Up @@ -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
Expand All @@ -121,5 +130,7 @@

urlpatterns = []

urlpatterns += (path("admin/", admin.site.urls),)


urlpatterns += drf_routes
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
PaediatricDiabetesUnitSerializer,
PaediatricDiabetesUnitWithNestedOrganisationSerializer,
PaediatricDiabetesUnitWithNestedOrganisationAndParentSerializer,
PaediatricDiabetesUnitWithNestedTrustSerializer,
)


Expand Down Expand Up @@ -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

0 comments on commit db3f243

Please sign in to comment.