Skip to content

Commit

Permalink
api: Enable fetching Grants by non-primay OrgIDs
Browse files Browse the repository at this point in the history
This commit enables fetching Grant data by the non-primary OrgID of a
Recipient of Funding Organisation.
  • Loading branch information
R2ZER0 committed Sep 30, 2024
1 parent 33d6750 commit 1c787ea
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
16 changes: 12 additions & 4 deletions datastore/api/org/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,16 @@ def get_queryset(self):
org_id = self.kwargs.get("org_id")

# Raise 404 if the Org doesn't exist
if not models.Organisation.exists(org_id):
try:
org = models.Organisation.get(org_id)
except models.Organisation.DoesNotExist:
raise rest_framework.exceptions.NotFound()

org_ids = [org.org_id] + [lo.org_id for lo in org.linked_orgs]

return (
db.Grant.objects.filter(source_file__latest__series=db.Latest.CURRENT)
.filter(funding_org_ids__contains=[org_id])
.filter(funding_org_ids__overlap=org_ids)
.select_related("source_file")
)

Expand All @@ -135,11 +139,15 @@ def get_queryset(self):
org_id = self.kwargs.get("org_id")

# Raise 404 if the Org doesn't exist
if not models.Organisation.exists(org_id):
try:
org = models.Organisation.get(org_id)
except models.Organisation.DoesNotExist:
raise rest_framework.exceptions.NotFound()

org_ids = [org.org_id] + [lo.org_id for lo in org.linked_orgs]

return (
db.Grant.objects.filter(source_file__latest__series=db.Latest.CURRENT)
.filter(recipient_org_ids__contains=[org_id])
.filter(recipient_org_ids__overlap=org_ids)
.select_related("source_file")
)
2 changes: 1 addition & 1 deletion datastore/db/fixtures/test_data.json

Large diffs are not rendered by default.

26 changes: 22 additions & 4 deletions datastore/tests/test_org_api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import datetime

from django.urls import reverse_lazy
from django.test import TestCase
from django.core.management import call_command

from django.test import TestCase
from django.urls import reverse_lazy

current_year = datetime.date.today().year

Expand All @@ -21,6 +20,7 @@ def setUpTestData(cls):
funder_grant_id = "360G-kahM5Ooc2u"

recipient_org_id = "360G-example-a"
recipient_nonprimary_org_id = "360G-example-nonprimary"
recipient_org_name = "Receive an example grant"
recipient_grant_id = "360G-Eiz4Veij8o"

Expand Down Expand Up @@ -202,7 +202,7 @@ def test_recipient_org_detail(self):
)

expected_org_data = {
"linked_orgs": [],
"linked_orgs": [{"org_id": "360G-example-nonprimary"}],
"self": "http://testserver" + expected_self_url,
"grants_made": "http://testserver" + expected_grants_made_url,
"grants_received": "http://testserver" + expected_received_made_url,
Expand All @@ -226,13 +226,22 @@ def test_recipient_org_detail(self):
"name": self.recipient_org_name,
}

# Lookup by primary org ID
data = self.client.get(
f"/api/v1/org/{self.recipient_org_id}/",
headers={"accept": "application/json"},
).json()

self.assertEqual(data, expected_org_data)

# Lookup by non-primary org ID
data = self.client.get(
f"/api/v1/org/{self.recipient_nonprimary_org_id}/",
headers={"accept": "application/json"},
).json()

self.assertEqual(data, expected_org_data)

def test_recipient_grants_received(self):
"""Assert that the Recipient gets the expected grant."""

Expand Down Expand Up @@ -318,6 +327,15 @@ def test_recipient_grants_received(self):
self.assertIn(self.recipient_grant_id, grants)
self.assertEqual(expected_grant_data, grants[self.recipient_grant_id])

def test_recipient_nonprimary_grants_received(self):
# Check that searching by a non-primary OrgID successfully returns results
data = self.client.get(
f"/api/v1/org/{self.recipient_nonprimary_org_id}/grants_received/",
headers={"accept": "application/json"},
).json()

self.assertEqual(data["count"], 50)

def test_recipient_grants_made(self):
"""A Recipient-only Org should not make any grants."""
data = self.client.get(
Expand Down
2 changes: 1 addition & 1 deletion datastore/tests/test_quality_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_create_sourcefile_publisher_quality_data(self):

expected_sourcefile_aggregate = {
"count": 5,
"recipient_organisations": ["360G-example-a"],
"recipient_organisations": ["360G-example-a", "360G-example-nonprimary"],
"recipient_individuals": 0,
"funders": ["GB-example-b"],
"max_award_date": "2019-10-03",
Expand Down

0 comments on commit 1c787ea

Please sign in to comment.