Coverage for backend/core/auxiliary/serializers/PropertyInfoSerializer.py: 99%
64 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 drf_spectacular.utils import extend_schema_field
3from core.auxiliary.models.PropertyInfo import PropertyInfo, HistoricalValue
4from core.auxiliary.models.PropertySet import PropertySet
5from .PropertyValueSerializer import PropertyValueSerializer
6from .RecycleDataSerializer import RecyclePropertySerializer
8class PropertyHistorySerializer(serializers.ModelSerializer):
9 class Meta:
10 model = HistoricalValue
11 fields = ['value']
12 read_only_fields = ['value']
15class PropertyInfoSerializer(serializers.ModelSerializer):
17 values = serializers.SerializerMethodField()
18 indexed_sets = serializers.SerializerMethodField()
20 recycleConnection = RecyclePropertySerializer(read_only=True)
22 class Meta:
23 model = PropertyInfo
24 fields = '__all__'
25 read_only_fields = ['id', 'key', 'set']
28 @extend_schema_field(PropertyValueSerializer)
29 def get_values(self, instance: PropertyInfo):
30 # for now, assume that we only have one property value
31 # and return { "id": property_value.id, "value": property_value.value }
33 # get the schema for this propertyInfo
35 #index_sets = instance.get_schema().indexSets
36 schema = instance.get_schema()
37 if schema != None:
38 index_sets = schema.indexSets
39 else:
40 index_sets = None
42 base = {}
43 for property_value in instance.values.all():
44 curr = base
45 parent = None
46 key = None
47 indexed_items = property_value.indexedItems.all()
48 if index_sets is not None:
49 for index_set in index_sets:
50 # Find the indexed item related to this
51 for indexed_item in indexed_items:
52 # move into this sub-dictionary
53 if indexed_item.type == index_set:
54 curr.setdefault(indexed_item.key, {})
55 parent = curr
56 key = indexed_item.key
57 curr = curr[indexed_item.key]
58 # TODO: This might break with multiple index sets of the same type, e.g "indexSets": ["phase","phase"],
59 # put property value in this location
60 property_value_data = PropertyValueSerializer(property_value).data
61 if key is not None:
62 parent[key] = property_value_data
63 else:
64 base = property_value_data
65 return base
68 def get_indexed_sets(self, instance: PropertyInfo):
69 """
70 Group every property values' indexed items by their indexed set
71 """
72 indexed_sets = {}
73 for property_value in instance.values.all():
74 for indexed_item in property_value.indexedItems.all():
75 indexed_sets.setdefault(indexed_item.type, {})[indexed_item.key] = {
76 "id": indexed_item.id,
77 "displayName": indexed_item.displayName,
78 }
79 return indexed_sets
82 def update(self, instance: PropertyInfo, validated_data: dict):
83 if "unit" in validated_data: 83 ↛ 85line 83 didn't jump to line 85 because the condition on line 83 was always true
84 instance.unit_conversion(validated_data.pop("unit"))
85 super().update(instance, validated_data)
87 return instance
90class PropertySetSerializer(serializers.ModelSerializer):
91 ContainedProperties = PropertyInfoSerializer(many=True)
92 unspecifiedProperties = serializers.SerializerMethodField()
94 class Meta:
95 model = PropertySet
96 read_only_fields = ['id']
97 fields = '__all__'
99 def get_unspecifiedProperties(self, instance: PropertySet):
100 return instance.get_unspecified_properties()