-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4025 from unicef/api-endpoints-list-ba-and-program
[207085] New REST API endpoints - Program and BA list
- Loading branch information
Showing
10 changed files
with
321 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from hct_mis_api.api.endpoints.core import * # noqa: F401, F403 | ||
from hct_mis_api.api.endpoints.lookups import * # noqa: F401, F403 | ||
from hct_mis_api.api.endpoints.rdi import * # noqa: F401, F403 |
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 @@ | ||
from hct_mis_api.api.endpoints.core.views import BusinessAreaListView # noqa: F401 |
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,18 @@ | ||
from rest_framework import serializers | ||
|
||
from hct_mis_api.apps.core.models import BusinessArea | ||
|
||
|
||
class BusinessAreaSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = BusinessArea | ||
fields = ( | ||
"id", | ||
"name", | ||
"code", | ||
"long_name", | ||
"slug", | ||
"parent", | ||
"is_split", | ||
"active", | ||
) |
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,10 @@ | ||
from rest_framework.generics import ListAPIView | ||
|
||
from hct_mis_api.api.endpoints.base import HOPEAPIView | ||
from hct_mis_api.api.endpoints.core.serializers import BusinessAreaSerializer | ||
from hct_mis_api.apps.core.models import BusinessArea | ||
|
||
|
||
class BusinessAreaListView(HOPEAPIView, ListAPIView): | ||
serializer_class = BusinessAreaSerializer | ||
queryset = BusinessArea.objects.all() |
Empty file.
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,25 @@ | ||
from rest_framework import serializers | ||
|
||
from hct_mis_api.apps.program.models import Program | ||
|
||
|
||
class ProgramGlobalSerializer(serializers.ModelSerializer): | ||
business_area_code = serializers.CharField(source="business_area.code", read_only=True) | ||
|
||
class Meta: | ||
model = Program | ||
fields = ( | ||
"id", | ||
"name", | ||
"programme_code", | ||
"status", | ||
"start_date", | ||
"end_date", | ||
"budget", | ||
"frequency_of_payments", | ||
"sector", | ||
"scope", | ||
"cash_plus", | ||
"population_goal", | ||
"business_area_code", | ||
) |
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,10 @@ | ||
from rest_framework.generics import ListAPIView | ||
|
||
from hct_mis_api.api.endpoints.base import HOPEAPIView | ||
from hct_mis_api.api.endpoints.program.serializers import ProgramGlobalSerializer | ||
from hct_mis_api.apps.program.models import Program | ||
|
||
|
||
class ProgramGlobalListView(HOPEAPIView, ListAPIView): | ||
serializer_class = ProgramGlobalSerializer | ||
queryset = Program.objects.all() |
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,94 @@ | ||
import contextlib | ||
from typing import Iterator | ||
|
||
from rest_framework.reverse import reverse | ||
|
||
from hct_mis_api.api.models import APIToken, Grant | ||
from hct_mis_api.api.tests.base import HOPEApiTestCase | ||
from hct_mis_api.apps.account.fixtures import BusinessAreaFactory | ||
from hct_mis_api.apps.core.models import BusinessArea | ||
|
||
|
||
@contextlib.contextmanager | ||
def token_grant_permission(token: APIToken, grant: Grant) -> Iterator: | ||
old = token.grants | ||
token.grants += [grant.name] | ||
token.save() | ||
yield | ||
token.grants = old | ||
token.save() | ||
|
||
|
||
class APIBusinessAreaTests(HOPEApiTestCase): | ||
databases = {"default"} | ||
user_permissions = [] | ||
|
||
@classmethod | ||
def setUpTestData(cls) -> None: | ||
super().setUpTestData() | ||
cls.list_url = reverse("api:business-area-list") | ||
|
||
def test_list_business_area(self) -> None: | ||
business_area1: BusinessArea = BusinessAreaFactory( | ||
slug="ukraine11", | ||
code="1234", | ||
name="Ukraine", | ||
long_name="the long name of Ukraine", | ||
active=True, | ||
) | ||
business_area2: BusinessArea = BusinessAreaFactory( | ||
slug="BA 2", | ||
code="5678", | ||
name="Bus Area 2", | ||
long_name="Business Area 2", | ||
active=False, | ||
parent=self.business_area, | ||
) | ||
self.business_area.refresh_from_db() | ||
business_area1.refresh_from_db() | ||
business_area2.refresh_from_db() | ||
response = self.client.get(self.list_url) | ||
assert response.status_code == 403 | ||
with token_grant_permission(self.token, Grant.API_READ_ONLY): | ||
response = self.client.get(self.list_url) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(len(response.json()["results"]), 3) | ||
self.assertIn( | ||
{ | ||
"id": str(self.business_area.id), | ||
"name": self.business_area.name, | ||
"code": self.business_area.code, | ||
"long_name": self.business_area.long_name, | ||
"slug": self.business_area.slug, | ||
"parent": None, | ||
"is_split": self.business_area.is_split, | ||
"active": self.business_area.active, | ||
}, | ||
response.json()["results"], | ||
) | ||
self.assertIn( | ||
{ | ||
"id": str(business_area1.id), | ||
"name": business_area1.name, | ||
"code": business_area1.code, | ||
"long_name": business_area1.long_name, | ||
"slug": business_area1.slug, | ||
"parent": None, | ||
"is_split": business_area1.is_split, | ||
"active": business_area1.active, | ||
}, | ||
response.json()["results"], | ||
) | ||
self.assertIn( | ||
{ | ||
"id": str(business_area2.id), | ||
"name": business_area2.name, | ||
"code": business_area2.code, | ||
"long_name": business_area2.long_name, | ||
"slug": business_area2.slug, | ||
"parent": str(business_area2.parent.id), | ||
"is_split": business_area2.is_split, | ||
"active": business_area2.active, | ||
}, | ||
response.json()["results"], | ||
) |
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