diff --git a/rcpch_nhs_organisations/hospitals/constants/country_codes.py b/rcpch_nhs_organisations/hospitals/constants/country_codes.py index 8b829af..cdcaea7 100644 --- a/rcpch_nhs_organisations/hospitals/constants/country_codes.py +++ b/rcpch_nhs_organisations/hospitals/constants/country_codes.py @@ -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) diff --git a/rcpch_nhs_organisations/hospitals/management/commands/seed.py b/rcpch_nhs_organisations/hospitals/management/commands/seed.py index 0b68d0d..488cb6b 100644 --- a/rcpch_nhs_organisations/hospitals/management/commands/seed.py +++ b/rcpch_nhs_organisations/hospitals/management/commands/seed.py @@ -10,6 +10,7 @@ seed_pdus, ods_codes_to_abstraction_levels, load_jersey_boundaries, + create_jersey_country, ) from .image import rcpch_ascii_art @@ -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() diff --git a/rcpch_nhs_organisations/hospitals/management/commands/seed_functions/jersey.py b/rcpch_nhs_organisations/hospitals/management/commands/seed_functions/jersey.py index c910579..8900bfb 100644 --- a/rcpch_nhs_organisations/hospitals/management/commands/seed_functions/jersey.py +++ b/rcpch_nhs_organisations/hospitals/management/commands/seed_functions/jersey.py @@ -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 diff --git a/rcpch_nhs_organisations/hospitals/management/commands/seed_functions/organisations.py b/rcpch_nhs_organisations/hospitals/management/commands/seed_functions/organisations.py index 1dc843e..c3babbb 100644 --- a/rcpch_nhs_organisations/hospitals/management/commands/seed_functions/organisations.py +++ b/rcpch_nhs_organisations/hospitals/management/commands/seed_functions/organisations.py @@ -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( @@ -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." @@ -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...", ) diff --git a/rcpch_nhs_organisations/hospitals/migrations/0005_alter_country_bng_e_alter_country_bng_n_and_more.py b/rcpch_nhs_organisations/hospitals/migrations/0005_alter_country_bng_e_alter_country_bng_n_and_more.py new file mode 100644 index 0000000..7bbbe8b --- /dev/null +++ b/rcpch_nhs_organisations/hospitals/migrations/0005_alter_country_bng_e_alter_country_bng_n_and_more.py @@ -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), + ), + ] diff --git a/rcpch_nhs_organisations/hospitals/models/country.py b/rcpch_nhs_organisations/hospitals/models/country.py index 917b737..133f38a 100644 --- a/rcpch_nhs_organisations/hospitals/models/country.py +++ b/rcpch_nhs_organisations/hospitals/models/country.py @@ -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 diff --git a/rcpch_nhs_organisations/hospitals/urls.py b/rcpch_nhs_organisations/hospitals/urls.py index 0128178..e3f6fb4 100644 --- a/rcpch_nhs_organisations/hospitals/urls.py +++ b/rcpch_nhs_organisations/hospitals/urls.py @@ -11,6 +11,7 @@ LondonBoroughOrganisationViewSet, NHSEnglandRegionViewSet, NHSEnglandRegionOrganisationViewSet, + OrganisationsAssociatedWithPaediatricDiabetesUnitsList, PaediatricDiabetesUnitViewSet, PaediatricDiabetesUnitWithNestedOrganisationsViewSet, PaediatricDiabetesUnitForOrganisationWithParentViewSet, @@ -99,7 +100,6 @@ basename="paediatric_diabetes_unit", ) - drf_routes = [ # rest framework paths path("", include(router.urls)), @@ -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"), diff --git a/rcpch_nhs_organisations/hospitals/views/organisation.py b/rcpch_nhs_organisations/hospitals/views/organisation.py index b406322..5b35537 100644 --- a/rcpch_nhs_organisations/hospitals/views/organisation.py +++ b/rcpch_nhs_organisations/hospitals/views/organisation.py @@ -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 ( @@ -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( @@ -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"] diff --git a/rcpch_nhs_organisations/hospitals/views/paediatric_diabetes_unit.py b/rcpch_nhs_organisations/hospitals/views/paediatric_diabetes_unit.py index 7584a24..a1cc8dd 100644 --- a/rcpch_nhs_organisations/hospitals/views/paediatric_diabetes_unit.py +++ b/rcpch_nhs_organisations/hospitals/views/paediatric_diabetes_unit.py @@ -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, @@ -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 ) diff --git a/rcpch_nhs_organisations/settings.py b/rcpch_nhs_organisations/settings.py index 463b4cf..4cad667 100644 --- a/rcpch_nhs_organisations/settings.py +++ b/rcpch_nhs_organisations/settings.py @@ -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": [