Coverage for backend/django/flowsheetInternals/unitops/serializers/SimulationObjectSerializer.py: 98%
39 statements
« prev ^ index » next coverage.py v7.10.7, created at 2026-05-13 02:47 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2026-05-13 02:47 +0000
1from rest_framework import serializers
2from flowsheetInternals.unitops.models.property_package_propogation import propogate_property_package
3from flowsheetInternals.unitops.models import SimulationObject, SimulationObjectFactory
4from flowsheetInternals.propertyPackages.models.StreamFactory import StreamFactory
5from core.auxiliary.serializers.PropertyInfoSerializer import PropertySetSerializer
8class SimulationObjectSerializer(serializers.ModelSerializer):
9 componentName = serializers.CharField(required=False, allow_blank=True, default="")
10 # The x and y coordinates of the unit operation.
11 x = serializers.DecimalField(max_digits=10, decimal_places=5, write_only=True, default=0.0)
12 y = serializers.DecimalField(max_digits=10, decimal_places=5, write_only=True, default=0.0)
13 parentGroup = serializers.IntegerField(write_only=True, default=0)# initial group to add the simulation object to
14 propertySetId = serializers.PrimaryKeyRelatedField(source='properties.pk', read_only=True)
15 groupId = serializers.PrimaryKeyRelatedField(source='grouping.pk', read_only=True) # This group id is only present on Group unit operations. see groupingModel.py
16 unspecifiedProperties = serializers.SerializerMethodField()
19 class Meta:
20 model = SimulationObject
21 extra_kwargs = {
22 'componentName': {'required': False}, # auto-generated
23 }
24 fields = '__all__'
26 def create(self, validated_data):
27 x = float(validated_data.pop('x'))
28 y = float(validated_data.pop('y'))
29 coordinates = {"x": x, "y": y}
30 return SimulationObjectFactory.create_simulation_object(coordinates, **validated_data)
32 def save(self, **kwargs):
33 # Update property package
34 validated_data = self.validated_data
35 result = super().save(**kwargs)
36 # Then propagate it to all connected streams
37 if validated_data.get("propertyPackageType") is not None or validated_data.get("customPackage") is not None:
38 if self.instance.objectType == "stream" or self.instance.objectType == "humid_air_stream": 38 ↛ 41line 38 didn't jump to line 41 because the condition on line 38 was always true
39 factory = StreamFactory(self.instance)
40 factory.check_and_update_stream()
41 propogate_property_package(self.instance)
43 return result
45 def get_unspecifiedProperties(self, instance: SimulationObject) -> list:
46 return instance.get_unspecified_properties()
48class SimulationObjectRetrieveSerializer(serializers.ModelSerializer):
49 properties = PropertySetSerializer(read_only=True)
51 class Meta:
52 model = SimulationObject
53 fields = '__all__'
54 extra_kwargs = {
55 'componentName': {'required': False}, # auto-generated
56 }