Skip to content

Commit

Permalink
Merge pull request #21 from rcpch:pdu-organisations
Browse files Browse the repository at this point in the history
Pdu-organisations
  • Loading branch information
eatyourpeas authored Jun 30, 2024
2 parents 2a8c362 + 56caa5e commit bb11801
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 26 deletions.
2 changes: 2 additions & 0 deletions rcpch_nhs_organisations/hospitals/constants/country_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
{"country_ons_code": "S92000003", "country_ons_name": "Scotland"},
{"country_ons_code": "E92000003", "country_ons_name": "Jersey"},
]

# jersey = Country(boundary_identifier="E92000003", name="Jersey", welsh_name="", bng_e=None, bng_n=None, long=2.1313, lat=49.2144, globalid="", geom=None)
2 changes: 2 additions & 0 deletions rcpch_nhs_organisations/hospitals/management/commands/seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
seed_pdus,
ods_codes_to_abstraction_levels,
load_jersey_boundaries,
create_jersey_country,
)

from .image import rcpch_ascii_art
Expand Down Expand Up @@ -55,6 +56,7 @@ def handle(self, *args, **options):
self.stdout.write(
B + "Adding all organisations and levels of abstraction..." + W
)
create_jersey_country()
ods_codes_to_abstraction_levels()
seed_trusts()
seed_organisations()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,24 @@ def load_jersey_boundaries():
encoding="iso-8859-1",
)
lm.save(strict=True, verbose=True)


def create_jersey_country():
"""
Create Jersey country object
Note that the geom field is not populated
"""
Country = django_apps.get_model("hospitals", "Country")
jersey = Country(
boundary_identifier="E92000003",
name="Jersey",
welsh_name="",
bng_e=None,
bng_n=None,
long=2.1313,
lat=49.2144,
globalid="",
geom=None,
)
jersey.save()
return jersey
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def seed_organisations():
Country = apps.get_model("hospitals", "Country")
england = Country.objects.get(boundary_identifier="E92000001")
wales = Country.objects.get(boundary_identifier="W92000004")
jersey = Country.objects.get(boundary_identifier="E92000003")

if Organisation.objects.all().count() >= 330:
logger.info(
Expand Down Expand Up @@ -101,8 +102,13 @@ def seed_organisations():
ods_code=rcpch_organisation["ParentODSCode"]
)
organisation.trust = trust

organisation.country = england

# Jersey is a special case
if rcpch_organisation["OrganisationCode"] == "RGT1W":
organisation.country = jersey

else:
raise Exception(
f"No Match! {rcpch_organisation['OrganisationName']} has no parent organisation."
Expand Down Expand Up @@ -130,20 +136,6 @@ def seed_organisations():

logger.info(f"{added+1} organisations added.")

# Add RCPCH
rcpch = Organisation.objects.create(
ods_code="8HV48",
name="Royal College of Paediatrics and Child Health",
website="https://www.rcpch.ac.uk/",
address1="5-11 Theobalds Road",
city="London",
postcode="WC1X 8SH",
latitude=51.521,
longitude=-0.1184349,
geocode_coordinates=Point(x=-0.1184349, y=51.521),
telephone="020 7092 6000",
)

logger.info(
"Updating RCPCH organisations with ICB, NHS England relationships...",
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Generated by Django 4.2.11 on 2024-06-30 12:45

import django.contrib.gis.db.models.fields
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("hospitals", "0004_jerseyboundaries"),
]

operations = [
migrations.AlterField(
model_name="country",
name="bng_e",
field=models.BigIntegerField(blank=True, null=True),
),
migrations.AlterField(
model_name="country",
name="bng_n",
field=models.BigIntegerField(blank=True, null=True),
),
migrations.AlterField(
model_name="country",
name="geom",
field=django.contrib.gis.db.models.fields.MultiPolygonField(
blank=True, null=True, srid=27700
),
),
migrations.AlterField(
model_name="country",
name="globalid",
field=models.CharField(blank=True, max_length=38, null=True),
),
migrations.AlterField(
model_name="country",
name="welsh_name",
field=models.CharField(blank=True, max_length=17, null=True),
),
]
10 changes: 5 additions & 5 deletions rcpch_nhs_organisations/hospitals/models/country.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
class CountryBoundaries(models.Model):
boundary_identifier = models.CharField(max_length=9)
name = models.CharField(max_length=16)
welsh_name = models.CharField(max_length=17)
bng_e = models.BigIntegerField()
bng_n = models.BigIntegerField()
welsh_name = models.CharField(max_length=17, blank=True, null=True)
bng_e = models.BigIntegerField(blank=True, null=True)
bng_n = models.BigIntegerField(blank=True, null=True)
long = models.FloatField()
lat = models.FloatField()
globalid = models.CharField(max_length=38)
geom = models.MultiPolygonField(srid=27700)
globalid = models.CharField(max_length=38, blank=True, null=True)
geom = models.MultiPolygonField(srid=27700, blank=True, null=True)

class Meta:
abstract = True
Expand Down
7 changes: 6 additions & 1 deletion rcpch_nhs_organisations/hospitals/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
LondonBoroughOrganisationViewSet,
NHSEnglandRegionViewSet,
NHSEnglandRegionOrganisationViewSet,
OrganisationsAssociatedWithPaediatricDiabetesUnitsList,
PaediatricDiabetesUnitViewSet,
PaediatricDiabetesUnitWithNestedOrganisationsViewSet,
PaediatricDiabetesUnitForOrganisationWithParentViewSet,
Expand Down Expand Up @@ -99,7 +100,6 @@
basename="paediatric_diabetes_unit",
)


drf_routes = [
# rest framework paths
path("", include(router.urls)),
Expand All @@ -109,6 +109,11 @@
PaediatricDiabetesUnitForOrganisationWithParentViewSet.as_view({"get": "list"}),
name="paediatric_diabetes_unit_organisation_with_parent",
),
path(
"organisations/paediatric-diabetes-units",
OrganisationsAssociatedWithPaediatricDiabetesUnitsList.as_view(),
name="organisations-associated-with-paediatric-diabetes-units",
),
path("schema/", SpectacularJSONAPIView.as_view(), name="schema"),
# Swagger UI
path("swagger-ui/", SpectacularSwaggerView.as_view(), name="swagger-ui"),
Expand Down
115 changes: 113 additions & 2 deletions rcpch_nhs_organisations/hospitals/views/organisation.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from rest_framework import (
viewsets,
serializers, # serializers here required for drf-spectacular @extend_schema
filters,
generics,
)
from rest_framework.decorators import api_view
from rest_framework.response import Response
from django_filters.rest_framework import DjangoFilterBackend

from drf_spectacular.utils import (
Expand All @@ -14,7 +16,10 @@
from drf_spectacular.types import OpenApiTypes

from ..models import Organisation
from ..serializers import OrganisationSerializer, OrganisationNoParentsSerializer
from ..serializers import (
OrganisationSerializer,
OrganisationNoParentsSerializer,
)


@extend_schema(
Expand Down Expand Up @@ -182,3 +187,109 @@ class OrganisationLimitedViewSet(viewsets.ReadOnlyModelViewSet):
]
filter_backends = (DjangoFilterBackend,)
pagination_class = None


@extend_schema(
request=None, # No request body expected
responses={
200: OpenApiResponse(
response=OpenApiTypes.OBJECT,
description="Valid Response",
examples=[
OpenApiExample(
"organisations/paediatric-diabetes-units/",
response_only="true",
value=[
{
"ods_code": "RGT01",
"name": "ADDENBROOKE'S HOSPITAL",
"website": "https://www.cuh.nhs.uk/",
"address1": "HILLS ROAD",
"address2": "",
"address3": "",
"telephone": "01223 245151",
"city": "CAMBRIDGE",
"county": "CAMBRIDGESHIRE",
"latitude": 52.17513275,
"longitude": 0.140753239,
"postcode": "CB2 0QQ",
"geocode_coordinates": "SRID=27700;POINT (0.140753239 52.17513275)",
"active": True,
"published_at": None,
"paediatric_diabetes_unit": {"pz_code": "PZ041"},
"trust": {
"ods_code": "RGT",
"name": "CAMBRIDGE UNIVERSITY HOSPITALS NHS FOUNDATION TRUST",
"address_line_1": "CAMBRIDGE BIOMEDICAL CAMPUS",
"address_line_2": "HILLS ROAD",
"town": "CAMBRIDGE",
"postcode": "CB2 0QQ",
"country": "ENGLAND",
"telephone": None,
"website": None,
"active": True,
"published_at": None,
},
"local_health_board": None,
"integrated_care_board": None,
"nhs_england_region": None,
"openuk_network": None,
"london_borough": None,
"country": {
"boundary_identifier": "E92000001",
"name": "England",
},
},
{
"ods_code": "RCF22",
"name": "AIREDALE GENERAL HOSPITAL",
"website": "https://www.airedaletrust.nhs.uk/",
"address1": "SKIPTON ROAD",
"address2": "STEETON",
"address3": "",
"telephone": "",
"city": "KEIGHLEY",
"county": "WEST YORKSHIRE",
"latitude": 53.8979454,
"longitude": -1.962710142,
"postcode": "BD20 6TD",
"geocode_coordinates": "SRID=27700;POINT (-1.962710142 53.8979454)",
"active": True,
"published_at": None,
"paediatric_diabetes_unit": {"pz_code": "PZ047"},
"trust": {
"ods_code": "RCF",
"name": "AIREDALE NHS FOUNDATION TRUST",
"address_line_1": "AIREDALE GENERAL HOSPITAL",
"address_line_2": "SKIPTON ROAD",
"town": "KEIGHLEY",
"postcode": "BD20 6TD",
"country": "ENGLAND",
"telephone": None,
"website": None,
"active": True,
"published_at": None,
},
"local_health_board": None,
"integrated_care_board": None,
"nhs_england_region": None,
"openuk_network": None,
"london_borough": None,
"country": {
"boundary_identifier": "E92000001",
"name": "England",
},
},
],
),
],
),
},
summary="This endpoint returns a list of all NHS Organisations (Acute or Community Hospitals) associated with a parent Paediatric Diabetes Unit.",
)
class OrganisationsAssociatedWithPaediatricDiabetesUnitsList(generics.ListAPIView):
queryset = Organisation.objects.filter(paediatric_diabetes_unit__isnull=False)
serializer_class = OrganisationSerializer
filter_backends = [DjangoFilterBackend, filters.OrderingFilter]
filterset_fields = ["ods_code", "name"]
ordering_fields = ["name", "ods_code"]
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from drf_spectacular.utils import extend_schema, OpenApiParameter, OpenApiTypes


from ..models import PaediatricDiabetesUnit
from ..models import PaediatricDiabetesUnit, Organisation
from ..serializers import (
PaediatricDiabetesUnitSerializer,
PaediatricDiabetesUnitWithNestedOrganisationSerializer,
Expand Down Expand Up @@ -142,7 +142,6 @@ class PaediatricDiabetesUnitWithNestedOrganisationsViewSet(
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
)
Expand Down
3 changes: 1 addition & 2 deletions rcpch_nhs_organisations/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

REST_FRAMEWORK = {
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
"PAGE_SIZE": 10,
"DEFAULT_PAGINATION_CLASS": None,
"DEFAULT_FILTER_BACKENDS": ("django_filters.rest_framework.DjangoFilterBackend",),
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
"DEFAULT_RENDERER_CLASSES": [
Expand Down

0 comments on commit bb11801

Please sign in to comment.