From 7175b64e5ae8928ce7e03b60179b8a347b38c192 Mon Sep 17 00:00:00 2001 From: James B Date: Wed, 26 Mar 2025 10:40:00 +0000 Subject: [PATCH] Data Package should contain a publishers.jl file too https://github.com/ThreeSixtyGiving/grantnav/issues/862 --- .../commands/create_data_package.py | 4 ++ .../commands/manage_entities_data.py | 40 ++++++++++++------- datastore/tests/test_commands.py | 4 ++ 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/datastore/db/management/commands/create_data_package.py b/datastore/db/management/commands/create_data_package.py index 57833eae..f9e9ffbc 100644 --- a/datastore/db/management/commands/create_data_package.py +++ b/datastore/db/management/commands/create_data_package.py @@ -55,6 +55,7 @@ def handle(self, *args, **options): data_all_file = "%s/data_all.json" % options["dir"] recipients_file = "%s/recipients.jl" % options["dir"] funders_file = "%s/funders.jl" % options["dir"] + publishers_file = "%s/publishers.jl" % options["dir"] with open(funders_file, "w") as funders_fp: create_orgs_list("funder", funders_fp) @@ -62,6 +63,9 @@ def handle(self, *args, **options): with open(recipients_file, "w") as recipients_fp: create_orgs_list("recipient", recipients_fp) + with open(publishers_file, "w") as recipients_fp: + create_orgs_list("publisher", recipients_fp) + def flatten_grant(in_grant): """Add the additional_data inside grant object""" out_grant = {} diff --git a/datastore/db/management/commands/manage_entities_data.py b/datastore/db/management/commands/manage_entities_data.py index 3ab0c654..91d3050f 100644 --- a/datastore/db/management/commands/manage_entities_data.py +++ b/datastore/db/management/commands/manage_entities_data.py @@ -87,20 +87,32 @@ def create_orgs_list(entity_type, output=sys.stdout): entity_type: publisher, recipient, funder output: io """ - query = f""" - SELECT DISTINCT - db_{entity_type}.org_id as "id", - db_{entity_type}.non_primary_org_ids as "non_primary_org_ids", - db_{entity_type}.name as name, - db_{entity_type}."aggregate" as "aggregate", - db_{entity_type}.additional_data as "additionalData", - additional_data_orginfocache.data as "ftcData", - db_publisher.name as "publisherName", - db_publisher.prefix as "publisherPrefix" - FROM db_{entity_type} - LEFT OUTER JOIN additional_data_orginfocache on db_{entity_type}.org_id = additional_data_orginfocache.org_id - LEFT OUTER JOIN db_publisher on db_{entity_type}.org_id = db_publisher.org_id OR db_publisher.org_id = ANY(db_{entity_type}.non_primary_org_ids) - """ + if entity_type == "publisher": + query = f""" + SELECT DISTINCT + db_{entity_type}.org_id as "id", + db_{entity_type}.name as name, + db_{entity_type}."aggregate" as "aggregate", + db_{entity_type}.additional_data as "additionalData", + additional_data_orginfocache.data as "ftcData" + FROM db_{entity_type} + LEFT OUTER JOIN additional_data_orginfocache on db_{entity_type}.org_id = additional_data_orginfocache.org_id + """ + else: + query = f""" + SELECT DISTINCT + db_{entity_type}.org_id as "id", + db_{entity_type}.non_primary_org_ids as "non_primary_org_ids", + db_{entity_type}.name as name, + db_{entity_type}."aggregate" as "aggregate", + db_{entity_type}.additional_data as "additionalData", + additional_data_orginfocache.data as "ftcData", + db_publisher.name as "publisherName", + db_publisher.prefix as "publisherPrefix" + FROM db_{entity_type} + LEFT OUTER JOIN additional_data_orginfocache on db_{entity_type}.org_id = additional_data_orginfocache.org_id + LEFT OUTER JOIN db_publisher on db_{entity_type}.org_id = db_publisher.org_id OR db_publisher.org_id = ANY(db_{entity_type}.non_primary_org_ids) + """ def parse_data_in_result(result, col_types): # work around for https://github.com/ThreeSixtyGiving/datastore/issues/125 diff --git a/datastore/tests/test_commands.py b/datastore/tests/test_commands.py index d42e63e6..e00e3550 100644 --- a/datastore/tests/test_commands.py +++ b/datastore/tests/test_commands.py @@ -34,6 +34,10 @@ def test_create_and_load_data_package(self): with open(os.path.join(tmpdir, "recipients.jl")) as recipients_fp: json.loads(recipients_fp.readline().strip()) + # Check the output json lines file by parsing the first line + with open(os.path.join(tmpdir, "publishers.jl")) as recipients_fp: + json.loads(recipients_fp.readline().strip()) + call_command("load_data_package", tmpdir, stderr=err_out) self.assertEqual( len(err_out.getvalue()), 0, "Errors output by load command"