Skip to content

Data Package should contain a publishers.jl file too #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: live
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions datastore/db/management/commands/create_data_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@ 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)

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 = {}
Expand Down
40 changes: 26 additions & 14 deletions datastore/db/management/commands/manage_entities_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,32 @@ def create_orgs_list(entity_type, output=sys.stdout):
entity_type: publisher, recipient, funder
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting comment, I wasn't sure if it ever worked in the past or was just planned to work at some point in the future

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":
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did try one query var and several ("" if x else "") in the middle but it seemed like this way was way more readable.

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
Expand Down
4 changes: 4 additions & 0 deletions datastore/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down