Coverage for backend/flowsheetInternals/unitops/serializers/SimulationObjectSerializer.py: 100%
28 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-11-06 23:27 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2025-11-06 23:27 +0000
1from rest_framework import serializers
2from flowsheetInternals.unitops.models import SimulationObject, SimulationObjectFactory
3from core.auxiliary.serializers.PropertyInfoSerializer import PropertySetSerializer
6class SimulationObjectSerializer(serializers.ModelSerializer):
7 componentName = serializers.CharField(required=False, allow_blank=True, default="")
8 # The x and y coordinates of the unit operation.
9 x = serializers.DecimalField(max_digits=10, decimal_places=5, write_only=True, default=0.0)
10 y = serializers.DecimalField(max_digits=10, decimal_places=5, write_only=True, default=0.0)
11 parentGroup = serializers.IntegerField(write_only=True, default=0)# initial group to add the simulation object to
12 propertySetId = serializers.PrimaryKeyRelatedField(source='properties.pk', read_only=True)
13 groupId = serializers.PrimaryKeyRelatedField(source='grouping.pk', read_only=True) # This group id is only present on Group unit operations. see groupingModel.py
14 unspecifiedProperties = serializers.SerializerMethodField()
17 class Meta:
18 model = SimulationObject
19 extra_kwargs = {
20 'componentName': {'required': False}, # auto-generated
21 }
22 fields = '__all__'
24 def create(self, validated_data):
25 x = float(validated_data.pop('x'))
26 y = float(validated_data.pop('y'))
27 coordinates = {"x": x, "y": y}
28 return SimulationObjectFactory.create_simulation_object(coordinates, **validated_data)
31 def get_unspecifiedProperties(self, instance: SimulationObject) -> list:
32 return instance.get_unspecified_properties()
34class SimulationObjectRetrieveSerializer(serializers.ModelSerializer):
35 properties = PropertySetSerializer(read_only=True)
37 class Meta:
38 model = SimulationObject
39 fields = '__all__'
40 extra_kwargs = {
41 'componentName': {'required': False}, # auto-generated
42 }