11from rest_framework import serializers
22
3- from coldfront .core .allocation .models import Allocation , AllocationAttribute
4- from coldfront .core .allocation .models import Project
3+ from coldfront .core .allocation .models import (
4+ Allocation ,
5+ AllocationAttribute ,
6+ AllocationStatusChoice ,
7+ AllocationAttributeType ,
8+ )
9+ from coldfront .core .allocation .models import Project , Resource
510
611
712class ProjectSerializer (serializers .ModelSerializer ):
@@ -23,28 +28,72 @@ def get_status(self, obj: Project) -> str:
2328 return obj .status .name
2429
2530
31+ class AllocationAttributeSerializer (serializers .ModelSerializer ):
32+ class Meta :
33+ model = AllocationAttribute
34+ fields = ["allocation_attribute_type" , "value" ]
35+
36+ allocation_attribute_type = serializers .SlugRelatedField (
37+ read_only = False ,
38+ slug_field = "name" ,
39+ queryset = AllocationAttributeType .objects .all (),
40+ )
41+ value = serializers .CharField (read_only = False )
42+
43+
2644class AllocationSerializer (serializers .ModelSerializer ):
2745 class Meta :
2846 model = Allocation
29- fields = ["id" , "project" , "description" , "resource" , "status" , "attributes" ]
47+ fields = [
48+ "id" ,
49+ "project" ,
50+ "project_id" ,
51+ "description" ,
52+ "resource" ,
53+ "resources_id" ,
54+ "status" ,
55+ "allocationattribute_set" ,
56+ ]
57+ read_only_fields = ["status" ]
3058
59+ # Seperate serializer fields for reading and writing projects and resources
3160 resource = serializers .SerializerMethodField ()
32- project = ProjectSerializer ()
33- attributes = serializers .SerializerMethodField ()
34- status = serializers .SerializerMethodField ()
61+ resources_id = serializers .PrimaryKeyRelatedField (
62+ write_only = True , queryset = Resource .objects .all (), source = "resources" , many = True
63+ )
64+
65+ project = ProjectSerializer (read_only = True )
66+ project_id = serializers .PrimaryKeyRelatedField (
67+ write_only = True ,
68+ queryset = Project .objects .all (),
69+ source = "project" ,
70+ )
71+
72+ allocationattribute_set = AllocationAttributeSerializer (many = True , required = False )
3573
3674 def get_resource (self , obj : Allocation ) -> dict :
3775 resource = obj .resources .first ()
3876 return {"name" : resource .name , "resource_type" : resource .resource_type .name }
3977
40- def get_attributes (self , obj : Allocation ):
41- attrs = AllocationAttribute .objects .filter (allocation = obj )
42- return {
43- a .allocation_attribute_type .name : obj .get_attribute (
44- a .allocation_attribute_type .name
45- )
46- for a in attrs
47- }
48-
4978 def get_status (self , obj : Allocation ) -> str :
5079 return obj .status .name
80+
81+ def create (self , validated_data ):
82+ allocation = Allocation .objects .create (
83+ project = validated_data ["project" ],
84+ status = AllocationStatusChoice .objects .get (
85+ name = "Active"
86+ ), # TODO: What should be the default status choice
87+ )
88+ allocation .resources .add (validated_data ["resources" ][0 ])
89+ allocation .save ()
90+
91+ allocation_attributes = validated_data .pop ("allocationattribute_set" , [])
92+ for attribute in allocation_attributes :
93+ AllocationAttribute .objects .create (
94+ allocation = allocation ,
95+ allocation_attribute_type = attribute ["allocation_attribute_type" ],
96+ value = attribute ["value" ],
97+ )
98+
99+ return allocation
0 commit comments