Coverage for backend/django/Economics/costing/line_properties/references.py: 93%
25 statements
« prev ^ index » next coverage.py v7.10.7, created at 2026-06-23 21:51 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2026-06-23 21:51 +0000
1from __future__ import annotations
3from core.auxiliary.models.PropertyValue import PropertyValue
4from Economics.formulas.native_properties.specs import EconomicsNativePropertySpec, native_property_specs
5from Economics.studies.models import EconomicsStudy
8CAPITAL_LINE_KIND = "capital"
9OPERATING_LINE_KIND = "operating"
12def native_property_reference(study: EconomicsStudy, field_key: str) -> str:
13 value = native_property_value(study, field_key)
14 return property_mention(value) if value is not None else ""
17def native_property_value(study: EconomicsStudy, field_key: str) -> PropertyValue | None:
18 spec = _native_property_spec(study, field_key)
19 if spec is None: 19 ↛ 20line 19 didn't jump to line 20 because the condition on line 19 was never true
20 return None
21 return (
22 PropertyValue.objects.select_related("property", "property__set")
23 .filter(
24 flowsheet=study.flowsheet,
25 property__formula_incomplete=False,
26 economics_metric_formulas__flowsheet=study.flowsheet,
27 economics_metric_formulas__study=study,
28 economics_metric_formulas__metric_key=_native_metric_key(spec),
29 )
30 .order_by("pk")
31 .first()
32 )
35def line_property_reference(study: EconomicsStudy, *, line_kind: str, line_id: int) -> str:
36 value = (
37 PropertyValue.objects.select_related("property", "property__set")
38 .filter(
39 flowsheet=study.flowsheet,
40 property__formula_incomplete=False,
41 economics_line_formulas__flowsheet=study.flowsheet,
42 economics_line_formulas__study=study,
43 economics_line_formulas__line_key=_line_field_key(line_kind, line_id),
44 )
45 .order_by("pk")
46 .first()
47 )
48 return property_mention(value) if value is not None else ""
51def property_mention(value: PropertyValue) -> str:
52 return f"@[{value.property.displayName}](prop{value.pk})"
55def _native_property_spec(study: EconomicsStudy, field_key: str) -> EconomicsNativePropertySpec | None:
56 return next((spec for spec in native_property_specs(study) if spec.field_key == field_key), None)
59def _native_metric_key(spec: EconomicsNativePropertySpec) -> str:
60 return spec.result_metric_key or spec.field_key
63def _line_field_key(line_kind: str, line_id: int) -> str:
64 return f"{line_kind}_line:{line_id}"