-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from ThreeSixtyGiving/mw/best_grant_impact_lo…
…cation Refactor locations in additional data
- Loading branch information
Showing
11 changed files
with
96 additions
and
9,767 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
datastore/additional_data/sources/additional_data_recipient_location.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", ""), | ||
] | ||
) |
Oops, something went wrong.