From 6b56f4f594b2a4738c9da2c0fc8440411abe8091 Mon Sep 17 00:00:00 2001 From: eatyourpeas Date: Thu, 30 May 2024 19:56:38 +0100 Subject: [PATCH] remove wythenshawe from northern care alliance add to manchester university, fix pdu seeding to update --- .../constants/paediatric_diabetes_units.py | 10 +- .../constants/rcpch_organisations.py | 50 ++++---- .../commands/seed_functions/pdus.py | 115 +++++++++++++----- .../hospitals/models/organisation.py | 2 +- .../models/paediatric_diabetes_unit.py | 2 +- 5 files changed, 118 insertions(+), 61 deletions(-) diff --git a/rcpch_nhs_organisations/hospitals/constants/paediatric_diabetes_units.py b/rcpch_nhs_organisations/hospitals/constants/paediatric_diabetes_units.py index a26a281..e1c765d 100644 --- a/rcpch_nhs_organisations/hospitals/constants/paediatric_diabetes_units.py +++ b/rcpch_nhs_organisations/hospitals/constants/paediatric_diabetes_units.py @@ -9,7 +9,10 @@ {"ods_code": "RC971", "npda_code": "PZ010"}, {"ods_code": "RAL26", "npda_code": "PZ012"}, {"ods_code": "RALC7", "npda_code": "PZ014"}, - {"ods_code": "R0A07", "npda_code": "PZ015"}, + { + "ods_code": "R0A07", + "npda_code": "PZ015", + }, # WYTHENSHAWE HOSPITAL (R0A07 - parent MANCHESTER UNIVERSITY NHS FOUNDATION TRUST) - has two ODS codes, the other is RM325 parent Northern Care Alliance {"ods_code": "RP5BA", "npda_code": "PZ016"}, {"ods_code": "RBD01", "npda_code": "PZ017"}, {"ods_code": "RYR18", "npda_code": "PZ018"}, @@ -150,7 +153,10 @@ {"ods_code": "RQWG0", "npda_code": "PZ200"}, {"ods_code": "RYJ01", "npda_code": "PZ202"}, {"ods_code": "RRV03", "npda_code": "PZ203"}, - {"ods_code": "RW6", "npda_code": "PZ206"}, + { + "ods_code": "RM3", + "npda_code": "PZ206", + }, # Pennine Acute Hospitals NHS Trust was RW6 and merged with Salford Royal NHS Foundation Trust, RM3 {"ods_code": "RTP04", "npda_code": "PZ213"}, {"ods_code": "RJZ01", "npda_code": "PZ215"}, {"ods_code": "RWFTW", "npda_code": "PZ216"}, diff --git a/rcpch_nhs_organisations/hospitals/constants/rcpch_organisations.py b/rcpch_nhs_organisations/hospitals/constants/rcpch_organisations.py index 86a79c6..ae4da60 100644 --- a/rcpch_nhs_organisations/hospitals/constants/rcpch_organisations.py +++ b/rcpch_nhs_organisations/hospitals/constants/rcpch_organisations.py @@ -8203,31 +8203,31 @@ "Fax": "", "LocalAuthority": "E06000060", }, - { - "OrganisationID": "", - "OrganisationCode": "RM325", - "OrganisationType": "", - "SubType": "", - "Sector": "", - "OrganisationStatus": "", - "IsPimsManaged": "", - "OrganisationName": "WYTHENSHAWE HOSPITAL", - "Address1": "SOUTHMOOR ROAD", - "Address2": "", - "Address3": "", - "City": "MANCHESTER", - "County": "GREATER MANCHESTER", - "Postcode": "M23 9LT", - "Latitude": "53.38911906358711", - "Longitude": "-2.2919031193396626", - "ParentODSCode": "RM3", - "ParentName": "NORTHERN CARE ALLIANCE NHS FOUNDATION TRUST", - "Phone": "", - "Email": "", - "Website": "", - "Fax": "", - "LocalAuthority": "E08000003", - }, + # { + # "OrganisationID": "", + # "OrganisationCode": "RM325", + # "OrganisationType": "", + # "SubType": "", + # "Sector": "", + # "OrganisationStatus": "", + # "IsPimsManaged": "", + # "OrganisationName": "WYTHENSHAWE HOSPITAL", + # "Address1": "SOUTHMOOR ROAD", + # "Address2": "", + # "Address3": "", + # "City": "MANCHESTER", + # "County": "GREATER MANCHESTER", + # "Postcode": "M23 9LT", + # "Latitude": "53.38911906358711", + # "Longitude": "-2.2919031193396626", + # "ParentODSCode": "RM3", + # "ParentName": "NORTHERN CARE ALLIANCE NHS FOUNDATION TRUST", + # "Phone": "", + # "Email": "", + # "Website": "", + # "Fax": "", + # "LocalAuthority": "E08000003", + # }, # Duplicate but note different ParentODSCode and ParentName. Wythenshawe Hospital is part of Manchester University NHS Foundation Trust and has never been part of Northern Care Alliance NHS Foundation Trust. { "OrganisationID": "39985", "OrganisationCode": "RA430", diff --git a/rcpch_nhs_organisations/hospitals/management/commands/seed_functions/pdus.py b/rcpch_nhs_organisations/hospitals/management/commands/seed_functions/pdus.py index 53d32ed..934ed51 100644 --- a/rcpch_nhs_organisations/hospitals/management/commands/seed_functions/pdus.py +++ b/rcpch_nhs_organisations/hospitals/management/commands/seed_functions/pdus.py @@ -20,10 +20,13 @@ def seed_pdus(): """ - Seed function which populates the Organisation table from JSON. - This instead uses a list provided by RCPCH E12 team of all organisations in England - and Wales that care for children with Epilepsy - community paediatrics and hospital paediatrics - in the same trust are counted as one organisation. + Seed function which populates the PaediatricDiabetesUnit table from constants.py + PDU codes are associated with ODS codes, which are used to identify the organisations or trusts that have a PDU + Some trusts have multiple PDUs + This function will update the existing organisations with the PDU code, where the ODS code matches the Organisation ODS code + If the ODS code is not found in the Organisation table, the function will search the Trust table for the ODS code. The assumption is made here + that where a PZ code is associated with a Trust, all the organisations under that Trust will be updated with the PDU code. + """ # Get models @@ -32,47 +35,69 @@ def seed_pdus(): LocalHealthBoard = apps.get_model("hospitals", "LocalHealthBoard") PaediatricDiabetesUnit = apps.get_model("hospitals", "PaediatricDiabetesUnit") + if PaediatricDiabetesUnit.objects.exists(): + logger.info( + "Paediatric Diabetes Units already exist in the database. Updating..." + ) + logger.info("Paediatric Diabetes Units being seeded...") for pdu in PZ_CODES: if Organisation.objects.filter(ods_code=pdu["ods_code"]).exists(): # the ods_code provided is for an existing organisation, update to include PDU - paediatric_diabetes_unit = PaediatricDiabetesUnit.objects.create( - pz_code=pdu["npda_code"] + paediatric_diabetes_unit, created = ( + PaediatricDiabetesUnit.objects.update_or_create( + pz_code=pdu["npda_code"] + ) ) Organisation.objects.filter(ods_code=pdu["ods_code"]).update( paediatric_diabetes_unit=paediatric_diabetes_unit ) + logger.info( + f"Updated Organisation {Organisation.objects.get(ods_code=pdu['ods_code'])} with PDU {pdu['npda_code']}" + ) else: if Trust.objects.filter(ods_code=pdu["ods_code"]).exists(): # the ods_code provided is for a Trust, update all the related organisations # create the PDU - paediatric_diabetes_unit = PaediatricDiabetesUnit.objects.create( - pz_code=pdu["npda_code"] + paediatric_diabetes_unit, created = ( + PaediatricDiabetesUnit.objects.update_or_create( + pz_code=pdu["npda_code"] + ) ) # get the trust trust = Trust.objects.filter(ods_code=pdu["ods_code"]).get() - # Update trust's child organisations and update their affiliation with the new PDU - Organisation.objects.filter(trust=trust).update( - paediatric_diabetes_unit=paediatric_diabetes_unit + # Update trust's child organisations and update their affiliation with the new PDU - exclude any organisations that already have a PDU + Organisation.objects.filter(trust=trust).exclude( + paediatric_diabetes_unit__isnull=False + ).update(paediatric_diabetes_unit=paediatric_diabetes_unit) + logger.info( + f"Updated Trust {trust} and all child organisations({Organisation.objects.filter(trust=trust)}) with PDU {pdu['npda_code']}" ) + elif LocalHealthBoard.objects.filter(ods_code=pdu["ods_code"]).exists(): # the ods_code provided is for a Local Health Board, update all the related organisations # create the PDU - paediatric_diabetes_unit = PaediatricDiabetesUnit.objects.create( - pz_code=pdu["npda_code"] + paediatric_diabetes_unit, created = ( + PaediatricDiabetesUnit.objects.update_or_create( + pz_code=pdu["npda_code"] + ) ) lhb = LocalHealthBoard.objects.get( ods_code=ORD_organisation["Rels"]["Rel"][0]["Target"]["OrgId"][ "extension" ] ) - Organisation.objects.filter(local_health_board=lhb).update( - paediatric_diabetes_unit=paediatric_diabetes_unit - ) + # update all child organisations in Local Health Board - exclude any organisations that already have a PDU + Organisation.objects.filter(local_health_board=lhb).exclude( + pz_code__isnull=False + ).update(paediatric_diabetes_unit=paediatric_diabetes_unit) else: # this organisation is associated with a pz code but does not exist in the organisation list we have # Fetch therefore from the Spine + logger.info( + f"Organisation with ODS code {pdu['ods_code']} not found in the database. Fetching from Spine..." + ) ORD_organisation = fetch_organisation_by_ods_code(pdu["ods_code"]) if ORD_organisation is not None: # use the retrieved postcode to get the longitude and latitude @@ -90,6 +115,9 @@ def seed_pdus(): "OrgId" ]["extension"] ) + child_organisations = Organisation.objects.filter( + trust=parent_trust, active=True + ) elif LocalHealthBoard.objects.filter( ods_code=ORD_organisation["Rels"]["Rel"][0]["Target"]["OrgId"][ "extension" @@ -100,20 +128,22 @@ def seed_pdus(): "OrgId" ]["extension"] ) + child_organisations = Organisation.objects.filter( + local_health_board=parent_trust, active=True + ) else: - print( - "There is no parent trust for this new organisation matching our database" + logger.warning( + f'There is no parent trust/local health board for this new organisation {pdu["ods_code"]} in the Spine. Skipping...' ) parent_trust = None if parent_trust is not None: - paediatric_diabetes_unit = ( - PaediatricDiabetesUnit.objects.create( + paediatric_diabetes_unit, created = ( + PaediatricDiabetesUnit.objects.update_or_create( pz_code=pdu["npda_code"] ) ) - organisations = parent_trust.trust_organisations.all() county = getattr( ORD_organisation["GeoLoc"]["Location"], "county", None ) @@ -132,7 +162,10 @@ def seed_pdus(): else: new_point = None try: - Organisation.objects.create( + logger.info( + f"Saving new organisation {ORD_organisation['Name']} from Spine with PDU {pdu['npda_code']}" + ) + new_organisation = Organisation.objects.create( ods_code=pdu["ods_code"], name=ORD_organisation["Name"], address1=ORD_organisation["GeoLoc"]["Location"][ @@ -147,20 +180,38 @@ def seed_pdus(): latitude=latitude, geocode_coordinates=new_point, published_at=ORD_organisation["Date"][0]["Start"], - trust=parent_trust, - local_health_board=None, - integrated_care_board=organisations[ - 0 - ].integrated_care_board, - nhs_england_region=organisations[0].nhs_england_region, - openuk_network=organisations[0].openuk_network, + openuk_network=child_organisations.first().openuk_network, paediatric_diabetes_unit=paediatric_diabetes_unit, - london_borough=organisations[0].london_borough, - country=organisations[0].country, + london_borough=child_organisations.first().london_borough, + country=child_organisations.first().country, ) + if child_organisations.first().country.name == "England": + new_organisation.trust = parent_trust + new_organisation.integrated_care_board = ( + child_organisations.first().integrated_care_board + ) + new_organisation.nhs_england_region = ( + child_organisations.first().nhs_england_region + ) + new_organisation.save( + update_fields=[ + "trust", + "integrated_care_board", + "nhs_england_region", + ] + ) + elif child_organisations.first().country.name == "Wales": + new_organisation.local_health_board = parent_trust + new_organisation.save( + update_fields=["local_health_board"] + ) + else: + logger.warning( + f"Country {child_organisations.first().country.name} not recognised for organisation {new_organisation.name}. Created with no relationship to Trust or Local Health Board or Integrated Care Board or NHS England Region" + ) except Exception as error: logger.exception( - f"{ORD_organisation['Name']} {pdu['ods_code']} not saved due to {error} {parent_trust.name} has count: {organisations.count()} organisations" + f"{ORD_organisation['Name']} {pdu['ods_code']} not saved due to {error} {parent_trust.name} has count: {child_organisations.count()} organisations" ) else: logger.exception( diff --git a/rcpch_nhs_organisations/hospitals/models/organisation.py b/rcpch_nhs_organisations/hospitals/models/organisation.py index cb20cc8..f9eea4b 100644 --- a/rcpch_nhs_organisations/hospitals/models/organisation.py +++ b/rcpch_nhs_organisations/hospitals/models/organisation.py @@ -25,7 +25,7 @@ class Organisation(TimeStampAbstractBaseClass): It represents a list of organisations that can be looked up """ - ods_code = CharField(max_length=100, null=True, blank=True, default=None) + ods_code = CharField(max_length=100, unique=True, null=False, blank=False) name = CharField(max_length=100, null=True, blank=True, default=None) website = CharField(max_length=100, null=True, blank=True, default=None) address1 = CharField(max_length=100, null=True, blank=True, default=None) diff --git a/rcpch_nhs_organisations/hospitals/models/paediatric_diabetes_unit.py b/rcpch_nhs_organisations/hospitals/models/paediatric_diabetes_unit.py index 2d36e00..6c2c827 100644 --- a/rcpch_nhs_organisations/hospitals/models/paediatric_diabetes_unit.py +++ b/rcpch_nhs_organisations/hospitals/models/paediatric_diabetes_unit.py @@ -6,7 +6,7 @@ class PaediatricDiabetesUnit(models.Model): - pz_code = CharField("Paediatric Diabetes Unit PZ Number", max_length=5) + pz_code = CharField("Paediatric Diabetes Unit PZ Number", max_length=5, unique=True) class Meta: verbose_name = "Paediatric Diabetes Unit"