Skip to content
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
28 changes: 26 additions & 2 deletions src/coldfront_plugin_api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
from rest_framework import serializers

from coldfront.core.allocation.models import Allocation, AllocationAttribute
from coldfront.core.allocation.models import Project
from coldfront.core.project.models import Project, ProjectAttribute


class ProjectSerializer(serializers.ModelSerializer):
class Meta:
model = Project
fields = ["id", "title", "pi", "description", "field_of_science", "status"]
fields = [
"id",
"title",
"pi",
"description",
"field_of_science",
"status",
"attributes",
]

pi = serializers.SerializerMethodField()
field_of_science = serializers.SerializerMethodField()
status = serializers.SerializerMethodField()
# is_externally_funded = serializers.SerializerMethodField()
attributes = serializers.SerializerMethodField()

def get_pi(self, obj: Project) -> str:
return obj.pi.email
Expand All @@ -22,6 +32,20 @@ def get_field_of_science(self, obj: Project) -> str:
def get_status(self, obj: Project) -> str:
return obj.status.name

def get_attributes(self, obj: Project):
attrs = ProjectAttribute.objects.filter(project=obj)
return {a.proj_attr_type.name: a.value for a in attrs}

# def get_is_externally_funded(self, obj: Project) -> bool | None:
# is_externally_funded_attr = ProjectAttribute.objects.filter(
# project=obj, proj_attr_type__name="Is Externally Funded"
# ).first()
# return (
# is_externally_funded_attr.value.lower() == "yes"
# if is_externally_funded_attr
# else None
# )


class AllocationSerializer(serializers.ModelSerializer):
class Meta:
Expand Down
9 changes: 6 additions & 3 deletions src/coldfront_plugin_api/tests/unit/test_allocations.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from os import devnull
import logging
import sys

from coldfront.core.allocation import models as allocation_models
Expand All @@ -17,9 +18,11 @@ def setUp(self) -> None:
call_command("register_cloud_attributes")
sys.stdout = backup

self.resource = self.new_openstack_resource(
name="Devstack", auth_url="http://localhost"
)
self.resource = self.new_openshift_resource(name="Microshift")

logging.disable(logging.CRITICAL)
call_command("register_default_quotas", apply=True)
logging.disable(logging.NOTSET)

@staticmethod
def new_allocation_attribute(allocation, attribute, value):
Expand Down
Loading