Skip to content
Merged
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
1 change: 1 addition & 0 deletions Containerfile.debugpy
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ EXPOSE 5678

RUN echo "yes" | uv run manage.py initial_setup
RUN uv run manage.py load_test_data
RUN uv run manage.py drf_create_token admin

CMD ["uv", "run", "python3", "-m", "debugpy", "--listen", "0.0.0.0:5678", "-m", "gunicorn", "-w", "1", "--capture-output","--enable-stdio-inheritance", "coldfront.config.wsgi","--bind", "0.0.0.0:8000", "--timeout", "0"]
20 changes: 20 additions & 0 deletions coldfront/plugins/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ paths:
Show related user data.
- project_attributes (default false)
Show related attribute data.
- grants (default false)
Show related grant data.
tags:
- projects
security:
Expand All @@ -281,6 +283,8 @@ paths:
Show related user data.
- project_attributes (default false)
Show related attribute data.
- grants (default false)
Show related grant data.
parameters:
- in: path
name: id
Expand All @@ -302,6 +306,10 @@ paths:
/api/resources/:
get:
operationId: resources_list
description: |-
Query parameters:
- resource_attributes (default false)
Show related attributes.
tags:
- resources
security:
Expand All @@ -319,6 +327,10 @@ paths:
/api/resources/{id}/:
get:
operationId: resources_retrieve
description: |-
Query parameters:
- resource_attributes (default false)
Show related attributes.
parameters:
- in: path
name: id
Expand Down Expand Up @@ -551,8 +563,12 @@ components:
project_attributes:
type: string
readOnly: true
grants:
type: string
readOnly: true
required:
- allocations
- grants
- id
- pi
- project_attributes
Expand All @@ -576,10 +592,14 @@ components:
type: string
is_allocatable:
type: boolean
resource_attributes:
type: string
readOnly: true
required:
- description
- id
- name
- resource_attributes
- resource_type
User:
type: object
Expand Down
29 changes: 28 additions & 1 deletion coldfront/plugins/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from rest_framework import serializers

from coldfront.core.allocation.models import Allocation, AllocationAttribute, AllocationChangeRequest, AllocationUser
from coldfront.core.grant.models import Grant
from coldfront.core.project.models import Project, ProjectAttribute, ProjectUser
from coldfront.core.resource.models import Resource, ResourceAttribute

Expand Down Expand Up @@ -201,17 +202,37 @@ class Meta:
fields = ("proj_attr_type", "value")


class GrantSerializer(serializers.ModelSerializer):
funding_agency = serializers.SlugRelatedField(slug_field="name", read_only=True)
status = serializers.SlugRelatedField(slug_field="name", read_only=True)

class Meta:
model = Grant
fields = ("title", "funding_agency", "total_amount_awarded", "status")


class ProjectSerializer(serializers.ModelSerializer):
pi = serializers.SlugRelatedField(slug_field="username", read_only=True)
status = serializers.SlugRelatedField(slug_field="name", read_only=True)
school = serializers.SlugRelatedField(slug_field="description", read_only=True)
project_users = serializers.SerializerMethodField()
allocations = serializers.SerializerMethodField()
project_attributes = serializers.SerializerMethodField()
grants = serializers.SerializerMethodField()

class Meta:
model = Project
fields = ("id", "title", "school", "pi", "status", "project_users", "allocations", "project_attributes")
fields = (
"id",
"title",
"school",
"pi",
"status",
"project_users",
"allocations",
"project_attributes",
"grants",
)

def get_project_users(self, obj):
request = self.context.get("request", None)
Expand All @@ -230,3 +251,9 @@ def get_project_attributes(self, obj):
if request and request.query_params.get("project_attributes") in ["true", "True"]:
return ProjectAttributeSerializer(obj.projectattribute_set, many=True, read_only=True).data
return None

def get_grants(self, obj):
request = self.context.get("request", None)
if request and request.query_params.get("grants") in ["true", "True"]:
return GrantSerializer(obj.grant_set, many=True, read_only=True).data
return None
33 changes: 33 additions & 0 deletions coldfront/plugins/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
# SPDX-License-Identifier: AGPL-3.0-or-later

import unittest
from datetime import date

from rest_framework import status
from rest_framework.test import APITestCase

from coldfront.config.env import ENV
from coldfront.core.allocation.models import Allocation
from coldfront.core.grant.models import Grant, GrantFundingAgency, GrantStatusChoice
from coldfront.core.project.models import Project
from coldfront.core.test_helpers.factories import (
AllocationAttributeFactory,
Expand Down Expand Up @@ -46,6 +48,21 @@ def setUpTestData(self):
AllocationAttributeFactory(allocation=allocation)
self.pi_user = project.pi

Grant.objects.create(
project=project,
title="Test grant",
grant_number="TEST-123",
role="PI",
grant_pi_full_name="",
funding_agency=GrantFundingAgency.objects.create(name="Test agency"),
grant_start=date(2026, 1, 1),
grant_end=date(2027, 1, 1),
percent_credit=100,
direct_funding=1000,
total_amount_awarded=2000,
status=GrantStatusChoice.objects.create(name="Active"),
)

def test_requires_login(self):
"""Test that the API requires authentication"""
response = self.client.get("/api/")
Expand Down Expand Up @@ -131,6 +148,22 @@ def test_project_query_params(self):
for proj in response.json():
self.assertEqual(len(proj["allocations"]), 1)

response = self.client.get("/api/projects/?grants=true", format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
projects_with_grants = [proj for proj in response.json() if proj["grants"]]
self.assertEqual(len(projects_with_grants), 1)
self.assertEqual(
projects_with_grants[0]["grants"],
[
{
"title": "Test grant",
"funding_agency": "Test agency",
"total_amount_awarded": 2000,
"status": "Active",
}
],
)

def test_user_api_permissions(self):
"""Test that accessing the user API view as an admin returns all
allocations, and that accessing it as a user is forbidden"""
Expand Down
8 changes: 8 additions & 0 deletions coldfront/plugins/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from simple_history.utils import get_history_model_for_model

from coldfront.core.allocation.models import Allocation, AllocationChangeRequest
from coldfront.core.grant.models import Grant
from coldfront.core.project.models import Project, ProjectUser
from coldfront.core.resource.models import Resource
from coldfront.plugins.api import serializers
Expand Down Expand Up @@ -276,6 +277,8 @@ class ProjectViewSet(viewsets.ReadOnlyModelViewSet):
Show related user data.
- project_attributes (default false)
Show related attribute data.
- grants (default false)
Show related grant data.
"""

serializer_class = serializers.ProjectSerializer
Expand Down Expand Up @@ -316,6 +319,11 @@ def get_queryset(self):
if self.request.query_params.get("project_attributes") in ["True", "true"]:
projects = projects.prefetch_related("projectattribute_set")

if self.request.query_params.get("grants") in ["True", "true"]:
projects = projects.prefetch_related(
Prefetch("grant_set", queryset=Grant.objects.select_related("funding_agency", "status"))
)

return projects.order_by("pi")


Expand Down
Loading