Skip to content

Commit 9e3298d

Browse files
author
Michael Wood
committed
additional_data: codelist: Add geoCodeType support
Revert "additional_data: codelist: Remove changes needed to support geoCodeType" This reverts commit a948b84.
1 parent a948b84 commit 9e3298d

2 files changed

Lines changed: 59 additions & 7 deletions

File tree

datastore/additional_data/sources/codelist_code.py

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"https://raw.githubusercontent.com/ThreeSixtyGiving/standard/main/codelists/grantToIndividualsReason.csv",
1010
"https://raw.githubusercontent.com/ThreeSixtyGiving/standard/main/codelists/regrantType.csv",
1111
"https://raw.githubusercontent.com/ThreeSixtyGiving/standard/main/codelists/locationScope.csv",
12+
"https://raw.githubusercontent.com/ThreeSixtyGiving/standard/main/codelists/geoCodeType.csv",
1213
# These codelists aren't yet processed
13-
# "https://raw.githubusercontent.com/ThreeSixtyGiving/standard/main/codelists/geoCodeType.csv",
1414
# "https://raw.githubusercontent.com/ThreeSixtyGiving/standard/main/codelists/countryCode.csv",
1515
# "https://raw.githubusercontent.com/ThreeSixtyGiving/standard/main/codelists/currency.csv",
1616
]
@@ -35,12 +35,21 @@ def import_codelists(self):
3535
r.iter_lines(decode_unicode=True), delimiter=","
3636
)
3737
for value in file_data:
38-
CodelistCode.objects.create(
39-
code=value["Code"],
40-
title=value["Title"],
41-
description=value["Description"],
42-
list_name=list_name,
43-
)
38+
# In https://github.com/ThreeSixtyGiving/standard/blob/main/codelists/geoCodeType.csv
39+
# we have non unique codes with differing descriptions. We have to just take the first
40+
# one we come accross to avoid an integrity error on the unique constraints.
41+
# https://github.com/ThreeSixtyGiving/standard/issues/391
42+
try:
43+
CodelistCode.objects.get(
44+
code=value["Code"], list_name=list_name
45+
)
46+
except CodelistCode.DoesNotExist:
47+
CodelistCode.objects.create(
48+
code=value["Code"],
49+
title=value["Title"],
50+
description=value["Description"],
51+
list_name=list_name,
52+
)
4453

4554
def update_additional_data(self, grant, additional_data):
4655
# check All the fields in the grant data that use codelists and make
@@ -51,6 +60,9 @@ def update_additional_data(self, grant, additional_data):
5160
grantPurpose = []
5261
regrantType = ""
5362
locationScope = ""
63+
beneficiary_geoCodeTypes = []
64+
recipient_organization_geoCodeType = ""
65+
funding_organization_geoCodeType = ""
5466

5567
try:
5668
code = grant["toIndividualsDetails"]["primaryGrantReason"]
@@ -95,6 +107,33 @@ def update_additional_data(self, grant, additional_data):
95107
except (KeyError, CodelistCode.DoesNotExist):
96108
pass
97109

110+
for location in grant["beneficiaryLocation"]:
111+
try:
112+
code = location["geoCodeType"]
113+
if code_title := CodelistCode.objects.get(
114+
code=location["geoCodeType"], list_name="geoCodeType"
115+
).title:
116+
beneficiary_geoCodeTypes.append(code_title)
117+
118+
except (KeyError, IndexError, CodelistCode.DoesNotExist):
119+
continue
120+
121+
try:
122+
code = grant["fundingOrganization"][0]["location"][0]["geoCodeType"]
123+
funding_organization_geoCodeType = CodelistCode.objects.get(
124+
code=code, list_name="geoCodeType"
125+
).title
126+
except (KeyError, IndexError, CodelistCode.DoesNotExist):
127+
pass
128+
129+
try:
130+
code = grant["recipientOrganization"][0]["location"][0]["geoCodeType"]
131+
recipient_organization_geoCodeType = CodelistCode.objects.get(
132+
code=code, list_name="geoCodeType"
133+
).title
134+
except (KeyError, IndexError, CodelistCode.DoesNotExist):
135+
pass
136+
98137
additional_data["codeListLookup"] = {
99138
"toIndividualsDetails": {
100139
"primaryGrantReason": primaryGrantReason,
@@ -103,4 +142,9 @@ def update_additional_data(self, grant, additional_data):
103142
},
104143
"regrantType": regrantType,
105144
"locationScope": locationScope,
145+
"geoCodeType": {
146+
"beneficiaryLocations": beneficiary_geoCodeTypes,
147+
"recipientOrganization0": recipient_organization_geoCodeType,
148+
"fundingOrganization0": funding_organization_geoCodeType,
149+
},
106150
}

datastore/tests/test_additional_data_codelist_code.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ def test_code_list(self):
1616
},
1717
"regrantType": "FRG010",
1818
"locationScope": "GLS040",
19+
"fundingOrganization": [{"location": [{"geoCodeType": "CTY"}]}],
20+
"recipientOrganization": [{"location": [{"geoCodeType": "LONB"}]}],
21+
"beneficiaryLocation": [{"geoCodeType": "MD"}],
1922
}
2023

2124
additional_data_in = {}
@@ -29,6 +32,11 @@ def test_code_list(self):
2932
},
3033
"regrantType": "Common Regrant",
3134
"locationScope": "Subnational region",
35+
"geoCodeType": {
36+
"beneficiaryLocations": ["Metropolitan Districts"],
37+
"recipientOrganization0": "London Boroughs",
38+
"fundingOrganization0": "Counties",
39+
},
3240
}
3341
}
3442

0 commit comments

Comments
 (0)