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 Aug 1, 2024
1 parent 09fb9c5 commit 5dd7d74
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 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.

10 changes: 5 additions & 5 deletions datastore/db/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import datetime
from typing import Dict, Any
from django.db.models import JSONField, Index
from django.db import connection, models
from django.db.utils import DataError

from django.contrib.postgres.fields import ArrayField
from django.contrib.postgres.indexes import GinIndex, BTreeIndex
from django.db import connection, models
from django.db.models import JSONField, Index
from django.db.utils import DataError
from django.utils import timezone

import datetime


class Latest(models.Model):
"""Latest best data we have"""
Expand Down
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

0 comments on commit 5dd7d74

Please sign in to comment.