Skip to content

Commit

Permalink
Merge pull request #131 from ThreeSixtyGiving/mw/best_grant_impact_lo…
Browse files Browse the repository at this point in the history
…cation

Refactor locations in additional data
  • Loading branch information
michaelwood committed Dec 13, 2022
2 parents 8ba70f3 + ecb1c85 commit 95a913d
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 9,767 deletions.
9 changes: 8 additions & 1 deletion datastore/additional_data/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from additional_data.sources.local_files import LocalFilesSource
from additional_data.sources.nspl import NSPLSource
from additional_data.sources.tsg_org_types import TSGOrgTypesSource
from additional_data.sources.additional_data_recipient_location import (
AdditionalDataRecipientLocation,
)


class AdditionalDataGenerator(object):
Expand All @@ -14,7 +17,8 @@ def __init__(self):
self.nspl_source = NSPLSource()
self.geo_lookup = GeoLookupSource()
self.tsg_org_types = TSGOrgTypesSource()
# Initialise Other Sources heres
self.additional_data_recipient_location = AdditionalDataRecipientLocation()
# Initialise Other Sources here

def create(self, grant):
"""Takes a grant's data and returns a dict of additional data"""
Expand All @@ -28,5 +32,8 @@ def create(self, grant):
self.nspl_source.update_additional_data(grant, additional_data)
self.geo_lookup.update_additional_data(grant, additional_data)
self.tsg_org_types.update_additional_data(grant, additional_data)
self.additional_data_recipient_location.update_additional_data(
grant, additional_data
)

return additional_data
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ def handle(self, *args, **options):
for grant in grants:
additional_data = generator.create(grant.data)
grant.additional_data = additional_data
grant.save()

Grant.objects.bulk_update(grants, ["additional_data"], batch_size=10000)
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
class AdditionalDataRecipientLocation(object):
"""Adds recipient* additional data to grant data
These fields are used as the best available Region and District backward compatibility
with grantnav and are sourced from locationLookup.
https://github.com/ThreeSixtyGiving/grantnav/issues/867#issuecomment-1332505360
Fields updated by this source.
"recipientWardNameGeoCode",
"recipientWardName",
"recipientDistrictName",
"recipientDistrictGeoCode",
"recipientRegionName",
"recipientCountryName",
"recipientLocation", # string of all the above
"""

def update_additional_data(self, grant, additional_data):
# If we have locationLookup from beneficiaryLocation or we don't have any
# recipientRegionName take whatever data we can from locationLookup instead
try:
additional_data["recipientRegionName"] = additional_data["locationLookup"][
0
]["rgnnm"]
except (IndexError, KeyError):
pass

try:
additional_data["recipientDistrictName"] = additional_data[
"locationLookup"
][0]["ladnm"]
additional_data["recipientDistrictGeoCode"] = additional_data[
"locationLookup"
][0]["ladcd"]
except (IndexError, KeyError):
pass

# Oddity that GrantNav expects a country name as a region name if a region hasn't been found
if not additional_data.get("recipientRegionName"):
try:
additional_data["recipientRegionName"] = additional_data[
"locationLookup"
][0]["ctrynm"]
except (IndexError, KeyError):
pass

if not additional_data.get("recipientCountryName"):
try:
additional_data["recipientCountryName"] = additional_data[
"locationLookup"
][0]["ctrynm"]
except (IndexError, KeyError):
pass

try:
# We can only get ward for recipientOrganization not beneficiary due to the granularity of needing a postcode
additional_data["recipientWardName"] = additional_data[
"recipientOrganizationLocation"
]["ward_name"]
additional_data["recipientWardNameGeoCode"] = additional_data[
"recipientOrganizationLocation"
]["ward"]
except (KeyError):
pass

# This is used for text searching in GrantNav
additional_data["recipientLocation"] = " ".join(
[
additional_data.get("recipientDistrictName", ""),
additional_data.get("recipientDistrictGeoCode", ""),
additional_data.get("recipientDistrictWardName", ""),
additional_data.get("recipientDistrictWardNameGeoCode", ""),
additional_data.get("recipientRegionName", ""),
additional_data.get("recipientCountryName", ""),
]
)
Loading

0 comments on commit 95a913d

Please sign in to comment.