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 }
0 commit comments