Skip to content

Commit

Permalink
Merge pull request #47 from rcpch:eatyourpeas/issue46
Browse files Browse the repository at this point in the history
reinstate-postcodes-io
  • Loading branch information
eatyourpeas authored Dec 8, 2024
2 parents 1c81ce4 + 194fab0 commit c85002b
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 34 deletions.
6 changes: 3 additions & 3 deletions documentation/docs/developer/seeding.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Seeding the database happens on initial migration (`002_seed_abstraction_levels`
- NHS England Regions
- Countries

Each one of these is associated with GIS shapes data loaded in from .csv in the `shape_files` folder. This goes through a LayerMapping step beforehand.
Each one of these is associated with GIS shapes data loaded in from .csv in the `shape_files` folder. This goes through a LayerMapping step beforehand. Countries included are England, Wales, Scotland, Northern Ireland and Jersey.

Subsequent seeding happens then from the command line and adds:

Expand All @@ -23,12 +23,12 @@ Subsequent seeding happens then from the command line and adds:
To run this after initial migration therefore from the command line within the docker instance it is necessary to:

```console
python manage.py seed --model all
python manage.py seed --level all
```

If only individual models need seeding the `--model` attribute accepts these parameters:
`abstraction_levels` (this adds ODS codes to the existing ICBs, London Boroughs, NHS England regions, as well as ONS GSS codes to the Countries)
`trusts`
`organisations`
`pdus`
`all`
`all` - Adds all the above as well as Jersey General Hosptial
2 changes: 1 addition & 1 deletion envs/.example.env
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ ENABLE_PDF_EXPORT=1 # Disables automatic PDF generation in local dev
NHS_ODS_API_URL="https://directory.spineservices.nhs.uk/ORD/2-0-0"

# POSTCODES API
POSTCODE_API_BASE_URL="https://findthatpostcode.uk"
POSTCODES_IO_API_URL="https://postcodes.io" # NOTE THAT POSTCODE_API_BASE_URL IS NOW DEPRECATED

# TIMEZONE
TZ="Europe/London"
Expand Down
16 changes: 16 additions & 0 deletions rcpch_nhs_organisations/hospitals/general_functions/ods_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,19 @@ def update_organisation_model_with_ORD_changes():
logger.info(
"No updates have been made to existing records in the RCPCH database."
)


def check_organisation_has_location_data():
"""
Checks if the organisation has location data
"""
index = 0
Organisation = apps.get_model("hospitals", "Organisation")
for organisation in Organisation.objects.all():
if organisation.postcode is None:
index += 1
if organisation.longitude is None:
print(f"{organisation.name} has not location data")
print(
f"{index} organisations have no postcode of a total {Organisation.objects.all().count()} organisations."
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,31 @@

def fetch_by_postcode(postcode: str):
"""
Returns GP Practice/hospital for ODS Code
Returns data object from the Postcode API for a given postcode
Includes:
- longitude, latitude
- northings, eastings
- country, nhs_ha, european_electoral_region, primary_care_trust
- region, lsoa, msoa, incode, outcode
- parliamentary_constituency, admin_district, parish, admin_county, admin_ward
codes for all of the above
"""

url = os.getenv("POSTCODE_API_BASE_URL")
url = os.getenv("POSTCODES_IO_API_URL")
api_key = os.getenv("POSTCODES_IO_API_KEY")

request_url = f"{url}/postcodes/{postcode}.json"
request_url = f"{url}/postcodes/{postcode}"

try:
response = requests.get(
url=request_url,
timeout=10, # times out after 10 seconds
headers={"Ocp-Apim-Subscription-Key": api_key},
)
response.raise_for_status()
except HTTPError as e:
print(e.response.text)
print(f"{postcode} not found")
return None

return response.json()["data"]["attributes"]
return response.json()["result"]
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ def create_organisations(self, organisations):

# fetch the county, postcode and retrieve the longitude and latitude
try:
longitude = float(postcode_object["location"]["lon"])
longitude = float(postcode_object]["longitude"])
except:
longitude = None

try:
latitude = float(postcode_object["location"]["lat"])
latitude = float(postcode_object["latitude"])
except:
latitude = None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ def ods_codes_to_abstraction_levels():

for open_uk_network in OPEN_UK_NETWORKS:
# iterates through all 42 ICBs and populates table
if OPENUKNetwork.objects.all().count() == 30:
if OPENUKNetwork.objects.filter(
boundary_identifier=open_uk_network["OPEN_UK_Network_Code"]
).exists():
# should NOT already exist in the database
logger.info(
f"OPEN UK Networks {open_uk_network['name']} have already been added to the database."
f"OPEN UK Networks {open_uk_network['OPEN_UK_Network_Name']} has already been added to the database."
)
pass
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def seed_organisations():
openuk_network = OPENUKNetwork.objects.get(
boundary_identifier=trust_openuk_network["OPEN UK Network Code"]
)
# upoate the OPENUK netowork for all the Organisations in this trust
# upoate the OPENUK network for all the Organisations in this trust
Organisation.objects.filter(query_term).update(openuk_network=openuk_network)
info_text = (
f"Updated {added+1} RCPCH organisations with OPENUK relationships..."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ def seed_pdus():
ORD_organisation["GeoLoc"]["Location"], "county", None
)
try:
longitude = float(postcode_object["location"]["lon"])
longitude = float(postcode_object["longitude"])
except:
longitude = None

try:
latitude = float(postcode_object["location"]["lat"])
latitude = float(postcode_object["latitude"])
except:
latitude = None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,35 @@ def seed_trusts():
# Get models
Trust = apps.get_model("hospitals", "Trust")

if Trust.objects.all().count() == 242:
logging_message = "242 Trusts already seeded. Skipping..."
if Trust.objects.all().count() == 243:
logging_message = "243 Trusts already seeded (including Jersey). Skipping..."
logger.info(logging_message)
else:
logger.info("Adding new Trusts...")

for added, trust in enumerate(TRUSTS):
try:
Trust.objects.create(
ods_code=trust["ods_code"],
name=trust["trust_name"],
address_line_1=trust["address_line_1"],
address_line_2=trust.get("address_line_2"),
town=trust["town"],
postcode=trust["postcode"],
country=trust["country"],
).save()
logger.info(f"{added+1}: {trust['trust_name']}")
except Exception as error:
error_message = f"Unable to save {trust['trust_name']}: {error}"
logger.error(error_message)

logging_message = f"{added+1} trusts added."
counter = 0
for _, trust in enumerate(TRUSTS):
if Trust.objects.filter(ods_code=trust["ods_code"]).exists():
logger.info(
f"{trust['trust_name']} already exists in the database. Skipping..."
)
continue
else:
try:
Trust.objects.create(
ods_code=trust["ods_code"],
name=trust["trust_name"],
address_line_1=trust["address_line_1"],
address_line_2=trust.get("address_line_2"),
town=trust["town"],
postcode=trust["postcode"],
country=trust["country"],
).save()
counter += 1
logger.info(f"Added: {trust['trust_name']}")
except Exception as error:
error_message = f"Unable to save {trust['trust_name']}: {error}"
logger.error(error_message)

logging_message = f"{counter} trusts added."
logger.info(logging_message)

0 comments on commit c85002b

Please sign in to comment.