Skip to content

Commit

Permalink
Merge pull request #9 from rcpch:pdu-from-organisation
Browse files Browse the repository at this point in the history
Pdu-from-organisation
  • Loading branch information
eatyourpeas authored Jun 1, 2024
2 parents 0732543 + 9ca65b1 commit b4badb3
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 8 deletions.
1 change: 1 addition & 0 deletions rcpch_nhs_organisations/hospitals/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
LondonBoroughWithNestedOrganisationsSerializer,
NHSEnglandRegionWithNestedOrganisationsSerializer,
PaediatricDiabetesUnitWithNestedOrganisationSerializer,
PaediatricDiabetesUnitWithNestedOrganisationAndParentSerializer,
)
from .paediatric_diabetes_unit import PaediatricDiabetesUnitSerializer
from .trust import TrustSerializer
56 changes: 56 additions & 0 deletions rcpch_nhs_organisations/hospitals/serializers/organisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4606,6 +4606,15 @@ class Meta:
fields = ["ods_code", "name"]


class OrganisationTrustLHBParentSerializer(serializers.ModelSerializer):
# used to serialize all child organisations and associated parent details
# returns only ods_code and name
class Meta:
model = Organisation
fields = ["ods_code", "name", "trust", "local_health_board"]
depth = 1


class TrustWithNestedOrganisationsSerializer(serializers.ModelSerializer):
# used to return all Trust fields as well as all related child organisations
# nested in
Expand Down Expand Up @@ -4741,3 +4750,50 @@ class PaediatricDiabetesUnitWithNestedOrganisationSerializer(
class Meta:
model = PaediatricDiabetesUnit
fields = ["pz_code", "organisations"]


# Returns an organisation with its parent LHB or Trust details
@extend_schema_serializer(
examples=[
OpenApiExample(
"/paediatric_diabetes_unit/1/organisations",
value={"ods_code": "", "parent": "Trust/Local Health Board"},
response_only=True,
)
]
)
class OrganisationWithParentSerializer(serializers.ModelSerializer):
parent = serializers.SerializerMethodField()

class Meta:
model = Organisation
fields = ["ods_code", "name", "parent"]

def get_parent(self, obj):
if obj.trust is not None:
return TrustSerializer(obj.trust).data
elif obj.local_health_board is not None:
return LocalHealthBoardSerializer(obj.local_health_board).data
else:
return None


@extend_schema_serializer(
examples=[
OpenApiExample(
"paediatric_diabetes_units/sibling-organisations/RGT01/",
value={"pz_code": "", "organisations": []},
response_only=True,
)
]
)
class PaediatricDiabetesUnitWithNestedOrganisationAndParentSerializer(
serializers.ModelSerializer
):
organisations = OrganisationWithParentSerializer(
many=True, read_only=True, source="paediatric_diabetes_unit_organisations"
)

class Meta:
model = PaediatricDiabetesUnit
fields = ["pz_code", "organisations"]
15 changes: 8 additions & 7 deletions rcpch_nhs_organisations/hospitals/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
NHSEnglandRegionOrganisationViewSet,
PaediatricDiabetesUnitViewSet,
PaediatricDiabetesUnitWithNestedOrganisationsViewSet,
PaediatricDiabetesUnitForOrganisationWithParentViewSet,
TrustViewSet,
)

Expand All @@ -27,10 +28,9 @@
basename="organisation-limited",
)

# returns a list of organisations and their nested parent details (without boundary data)
# returns a list of organisations and their nested parent details (without boundary data) - this runs to 18,000 records so is possibly going to need pagination
router.register(r"organisations", viewset=OrganisationViewSet, basename="organisation")


# returns a list of trusts and their details with their nested child organisations (ods_code and name only)
router.register(
r"trusts",
Expand Down Expand Up @@ -92,22 +92,23 @@
viewset=PaediatricDiabetesUnitViewSet,
basename="paediatric_diabetes_unit",
)
# returns a list of Paediatric Diabetes Units with nested child organisations
router.register(
r"paediatric_diabetes_units/organisations",
viewset=PaediatricDiabetesUnitWithNestedOrganisationsViewSet,
basename="paediatric_diabetes_unit",
)
# router.register(
# r"paediatric_diabetes_units/trusts",
# viewset=TrustWithNestedPaediatricDiabetesUnitsViewSet,
# basename="paediatric_diabetes_unit",
# )


drf_routes = [
# rest framework paths
path("", include(router.urls)),
# JSON Schema
path(
"paediatric_diabetes_units/sibling-organisations/<str:ods_code>/",
PaediatricDiabetesUnitForOrganisationWithParentViewSet.as_view({"get": "list"}),
name="paediatric_diabetes_unit_organisation_with_parent",
),
path("schema/", SpectacularJSONAPIView.as_view(), name="schema"),
# Swagger UI
path("swagger-ui/", SpectacularSwaggerView.as_view(), name="swagger-ui"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@
viewsets,
serializers, # serializers here required for drf-spectacular @extend_schema
)
from rest_framework.response import Response
from django_filters.rest_framework import DjangoFilterBackend

from drf_spectacular.utils import (
extend_schema,
OpenApiExample,
OpenApiResponse,
)
from drf_spectacular.types import OpenApiTypes

# from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import extend_schema, OpenApiParameter, OpenApiTypes


from ..models import PaediatricDiabetesUnit
from ..serializers import (
PaediatricDiabetesUnitSerializer,
PaediatricDiabetesUnitWithNestedOrganisationSerializer,
PaediatricDiabetesUnitWithNestedOrganisationAndParentSerializer,
)


Expand Down Expand Up @@ -103,3 +108,45 @@ class PaediatricDiabetesUnitWithNestedOrganisationsViewSet(
]
filter_backends = (DjangoFilterBackend,)
pagination_class = None


@extend_schema(
request=PaediatricDiabetesUnitWithNestedOrganisationAndParentSerializer,
responses={
200: OpenApiResponse(
response=OpenApiTypes.OBJECT,
description="Valid Response",
examples=[
OpenApiExample(
"paediatric_diabetes_units/sibling-organisations/RGT01/",
external_value="external value",
value={
"ods_code": "RGT01",
},
response_only="true",
),
],
),
},
parameters=[
OpenApiParameter(
name="ods_code",
description="ODS Code of the Organisation",
required=True,
type=OpenApiTypes.STR,
location=OpenApiParameter.PATH,
),
],
summary="This endpoint returns a list of sibling NHS Organisations (Acute or Community Hospitals) within a Paediatric Diabetes Unit (with their parent), against an ODS code.",
)
class PaediatricDiabetesUnitForOrganisationWithParentViewSet(viewsets.ViewSet):

def list(self, request, ods_code=None):
print(f"Hello {ods_code}")
queryset = PaediatricDiabetesUnit.objects.filter(
paediatric_diabetes_unit_organisations__ods_code=ods_code
)
serializer = PaediatricDiabetesUnitWithNestedOrganisationAndParentSerializer(
queryset, many=True
)
return Response(serializer.data)

0 comments on commit b4badb3

Please sign in to comment.