diff --git a/src/coldfront_plugin_api/serializers.py b/src/coldfront_plugin_api/serializers.py index 8a29059..8ec3bbb 100644 --- a/src/coldfront_plugin_api/serializers.py +++ b/src/coldfront_plugin_api/serializers.py @@ -1,7 +1,12 @@ from rest_framework import serializers -from coldfront.core.allocation.models import Allocation, AllocationAttribute -from coldfront.core.allocation.models import Project +from coldfront.core.allocation.models import ( + Allocation, + AllocationAttribute, + AllocationStatusChoice, + AllocationAttributeType, +) +from coldfront.core.allocation.models import Project, Resource class ProjectSerializer(serializers.ModelSerializer): @@ -23,28 +28,72 @@ def get_status(self, obj: Project) -> str: return obj.status.name +class AllocationAttributeSerializer(serializers.ModelSerializer): + class Meta: + model = AllocationAttribute + fields = ["allocation_attribute_type", "value"] + + allocation_attribute_type = serializers.SlugRelatedField( + read_only=False, + slug_field="name", + queryset=AllocationAttributeType.objects.all(), + ) + value = serializers.CharField(read_only=False) + + class AllocationSerializer(serializers.ModelSerializer): class Meta: model = Allocation - fields = ["id", "project", "description", "resource", "status", "attributes"] + fields = [ + "id", + "project", + "project_id", + "description", + "resource", + "resources_id", + "status", + "allocationattribute_set", + ] + read_only_fields = ["status"] + # Seperate serializer fields for reading and writing projects and resources resource = serializers.SerializerMethodField() - project = ProjectSerializer() - attributes = serializers.SerializerMethodField() - status = serializers.SerializerMethodField() + resources_id = serializers.PrimaryKeyRelatedField( + write_only=True, queryset=Resource.objects.all(), source="resources", many=True + ) + + project = ProjectSerializer(read_only=True) + project_id = serializers.PrimaryKeyRelatedField( + write_only=True, + queryset=Project.objects.all(), + source="project", + ) + + allocationattribute_set = AllocationAttributeSerializer(many=True, required=False) def get_resource(self, obj: Allocation) -> dict: resource = obj.resources.first() return {"name": resource.name, "resource_type": resource.resource_type.name} - def get_attributes(self, obj: Allocation): - attrs = AllocationAttribute.objects.filter(allocation=obj) - return { - a.allocation_attribute_type.name: obj.get_attribute( - a.allocation_attribute_type.name - ) - for a in attrs - } - def get_status(self, obj: Allocation) -> str: return obj.status.name + + def create(self, validated_data): + allocation = Allocation.objects.create( + project=validated_data["project"], + status=AllocationStatusChoice.objects.get( + name="Active" + ), # TODO: What should be the default status choice + ) + allocation.resources.add(validated_data["resources"][0]) + allocation.save() + + allocation_attributes = validated_data.pop("allocationattribute_set", []) + for attribute in allocation_attributes: + AllocationAttribute.objects.create( + allocation=allocation, + allocation_attribute_type=attribute["allocation_attribute_type"], + value=attribute["value"], + ) + + return allocation diff --git a/src/coldfront_plugin_api/urls.py b/src/coldfront_plugin_api/urls.py index e5ac5a4..2e83fce 100644 --- a/src/coldfront_plugin_api/urls.py +++ b/src/coldfront_plugin_api/urls.py @@ -10,7 +10,7 @@ from coldfront_plugin_api import auth, serializers -class AllocationViewSet(viewsets.ReadOnlyModelViewSet): +class AllocationViewSet(viewsets.ModelViewSet): """ This viewset implements the API to Coldfront's allocation object The API allows filtering allocations by any of Coldfront's allocation model attributes,