-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get gc organisations data from S3 (#2056)
We now have a cached version of the gc-organisations datafile in S3 (both in Prod and Staging). This PR updates admin to use the file in S3.
- Loading branch information
Showing
6 changed files
with
77 additions
and
99 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
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,43 @@ | ||
import json | ||
|
||
import botocore | ||
from unidecode import unidecode | ||
|
||
from app.s3_client.s3_logo_client import get_s3_object | ||
|
||
|
||
def get_gc_organisations_from_s3(current_app): | ||
bucket = current_app.config["GC_ORGANISATIONS_BUCKET_NAME"] | ||
filename = current_app.config["GC_ORGANISATIONS_FILENAME"] | ||
try: | ||
key = get_s3_object(bucket, filename) | ||
data_str = key.get()["Body"].read().decode("utf-8") | ||
org_data = json.loads(data_str) | ||
return org_data | ||
except (botocore.exceptions.ClientError, botocore.exceptions.ParamValidationError, ValueError): | ||
current_app.logger.error("Unable to download s3 file {}/{}".format(bucket, filename)) | ||
return [] | ||
|
||
|
||
def parse_gc_organisations_data(org_data): | ||
if not org_data: | ||
return {"all": [], "names": {}} | ||
|
||
org_name_data = { | ||
"en": [item["name_eng"] for item in org_data], | ||
"fr": [item["name_fra"] for item in org_data], | ||
} | ||
# unidecode is needed so that names starting with accents like é | ||
# are sorted correctly | ||
sorted_org_name_data = { | ||
"en": sorted(org_name_data["en"], key=unidecode), | ||
"fr": sorted(org_name_data["fr"], key=unidecode), | ||
} | ||
parsed_org_data = {"all": org_data, "names": sorted_org_name_data} | ||
return parsed_org_data | ||
|
||
|
||
def get_gc_organisations(current_app): | ||
org_data = get_gc_organisations_from_s3(current_app) | ||
parsed_org_data = parse_gc_organisations_data(org_data) | ||
return parsed_org_data |
This file was deleted.
Oops, something went wrong.
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,27 @@ | ||
from app.s3_client.s3_gc_organisations_client import parse_gc_organisations_data | ||
|
||
|
||
def test_parse_gc_organisations_data_sorts_alphabetically(mocker, app_): | ||
test_data = [ | ||
{ | ||
"id": "1234", | ||
"name_eng": "CDS", | ||
"name_fra": "SNC", | ||
}, | ||
{ | ||
"id": "2234", | ||
"name_eng": "TBS", | ||
"name_fra": "SCT", | ||
}, | ||
{"id": "3456", "name_eng": "ABC", "name_fra": "ÉASDF"}, | ||
] | ||
parsed_data = parse_gc_organisations_data(test_data) | ||
assert parsed_data["all"] == test_data | ||
assert parsed_data["names"] == {"en": ["ABC", "CDS", "TBS"], "fr": ["ÉASDF", "SCT", "SNC"]} | ||
|
||
|
||
def test_parse_gc_organisations_data_returns_empty_dict(mocker, app_): | ||
test_data = None | ||
parsed_data = parse_gc_organisations_data(test_data) | ||
assert parsed_data["all"] == [] | ||
assert parsed_data["names"] == {} |
This file was deleted.
Oops, something went wrong.