Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
282 additions
and
30 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
11 changes: 11 additions & 0 deletions
11
datastore/additional_data/management/commands/load_codelist_codes.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,11 @@ | ||
from django.core.management.base import BaseCommand | ||
|
||
from additional_data.sources.codelist_code import CodeListSource | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Imports 360Giving standard codelist data" | ||
|
||
def handle(self, *args, **options): | ||
source = CodeListSource() | ||
source.import_codelists() |
4 changes: 2 additions & 2 deletions
4
...ta/management/commands/load_code_names.py → ...management/commands/load_geocode_names.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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
from django.core.management.base import BaseCommand | ||
|
||
from additional_data.sources.code_names import CodeNamesSource | ||
from additional_data.sources.code_names import GeoCodeNamesSource | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Imports location code names" | ||
|
||
def handle(self, *args, **options): | ||
source = CodeNamesSource() | ||
source = GeoCodeNamesSource() | ||
source.import_code_names() |
47 changes: 47 additions & 0 deletions
47
datastore/additional_data/migrations/0008_auto_20230123_1727.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,47 @@ | ||
# Generated by Django 3.2.16 on 2023-01-23 17:27 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("additional_data", "0007_auto_20200918_0954"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="GeoCodeName", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("code", models.CharField(db_index=True, max_length=9)), | ||
("data", models.JSONField()), | ||
], | ||
), | ||
migrations.DeleteModel( | ||
name="CodeName", | ||
), | ||
migrations.AlterField( | ||
model_name="geolookup", | ||
name="data", | ||
field=models.JSONField(), | ||
), | ||
migrations.AlterField( | ||
model_name="nspl", | ||
name="data", | ||
field=models.JSONField(), | ||
), | ||
migrations.AlterField( | ||
model_name="orginfocache", | ||
name="data", | ||
field=models.JSONField(), | ||
), | ||
] |
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,46 @@ | ||
# Generated by Django 3.2.16 on 2023-01-24 13:08 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("additional_data", "0008_auto_20230123_1727"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="CodelistCode", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
( | ||
"list_name", | ||
models.CharField( | ||
help_text="The name of the CodeList the code belongs to", | ||
max_length=200, | ||
), | ||
), | ||
("code", models.CharField(help_text="The code", max_length=200)), | ||
( | ||
"title", | ||
models.CharField(help_text="The title of the code", max_length=200), | ||
), | ||
( | ||
"description", | ||
models.TextField(help_text="The long description of the code"), | ||
), | ||
], | ||
options={ | ||
"unique_together": {("list_name", "code")}, | ||
}, | ||
), | ||
] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import csv | ||
import requests | ||
|
||
from additional_data.models import CodelistCode | ||
|
||
code_lists_urls = [ | ||
"https://raw.githubusercontent.com/ThreeSixtyGiving/standard/master/codelists/grantToIndividualsPurpose.csv", | ||
"https://raw.githubusercontent.com/ThreeSixtyGiving/standard/master/codelists/grantToIndividualsReason.csv", | ||
"https://raw.githubusercontent.com/ThreeSixtyGiving/standard/master/codelists/regrantType.csv", | ||
# These lists aren't yet ready for use in the datastore | ||
# https://github.com/ThreeSixtyGiving/standard/issues/348 | ||
# https://github.com/ThreeSixtyGiving/standard/issues/349 | ||
# "https://raw.githubusercontent.com/ThreeSixtyGiving/standard/master/codelists/countryCode.csv", | ||
# "https://raw.githubusercontent.com/ThreeSixtyGiving/standard/master/codelists/currency.csv", | ||
# "https://raw.githubusercontent.com/ThreeSixtyGiving/standard/master/codelists/geoCodeType.csv", | ||
] | ||
|
||
|
||
class CodeListSource(object): | ||
"""Looks up codes from 360Giving codelists and gets the title value of the code | ||
responsible for field: codeListLookup | ||
""" | ||
|
||
def import_codelists(self): | ||
CodelistCode.objects.all().delete() | ||
|
||
for code_list_url in code_lists_urls: | ||
# list name = last item in split -4 to remove extension .csv | ||
list_name = code_list_url.split("/")[-1:][0][:-4] | ||
with requests.get(code_list_url, stream=True) as r: | ||
r.raise_for_status() | ||
file_data = csv.DictReader( | ||
r.iter_lines(decode_unicode=True), delimiter="," | ||
) | ||
for value in file_data: | ||
CodelistCode.objects.create( | ||
code=value["Code"], | ||
title=value["Title"], | ||
description=value["Description"], | ||
list_name=list_name, | ||
) | ||
|
||
def update_additional_data(self, grant, additional_data): | ||
# check All the fields in the grant data that use codelists and make additional data field versions of them | ||
|
||
primaryGrantReason = "" | ||
secondaryGrantReason = "" | ||
grantPurpose = "" | ||
regrantType = "" | ||
|
||
try: | ||
code = grant["toIndividualDetails"]["primaryGrantReason"] | ||
primaryGrantReason = CodelistCode.objects.get( | ||
code=code, list_name="grantToIndividualsReason" | ||
).title | ||
except (KeyError, CodelistCode.DoesNotExist): | ||
pass | ||
|
||
try: | ||
code = grant["toIndividualDetails"]["secondaryGrantReason"] | ||
secondaryGrantReason = CodelistCode.objects.get( | ||
code=code, list_name="grantToIndividualsReason" | ||
).title | ||
except (KeyError, CodelistCode.DoesNotExist): | ||
pass | ||
|
||
try: | ||
code = grant["toIndividualDetails"]["grantPurpose"] | ||
grantPurpose = CodelistCode.objects.get( | ||
code=code, list_name="grantToIndividualsPurpose" | ||
).title | ||
except (KeyError, CodelistCode.DoesNotExist): | ||
pass | ||
|
||
try: | ||
code = grant["regrantType"] | ||
regrantType = CodelistCode.objects.get( | ||
code=code, list_name="regrantType" | ||
).title | ||
except (KeyError, CodelistCode.DoesNotExist): | ||
pass | ||
|
||
additional_data["codeListLookup"] = { | ||
"toIndividualDetails": { | ||
"primaryGrantReason": primaryGrantReason, | ||
"secondaryGrantReason": secondaryGrantReason, | ||
"grantPurpose": grantPurpose, | ||
}, | ||
"regrantType": regrantType, | ||
} |
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
Oops, something went wrong.