Coverage for backend/django/core/auxiliary/methods/copy_object/formula_remapping.py: 89%
20 statements
« prev ^ index » next coverage.py v7.10.7, created at 2026-07-22 05:22 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2026-07-22 05:22 +0000
1"""Formula remapping for selected-object duplication."""
3from core.auxiliary.formula_limits import validate_formula_length
4from core.auxiliary.methods.replace_expression_ids import (
5 extract_id_from_formula_key,
6 get_formula_keys,
7 replace_props,
8)
9from core.auxiliary.models.PropertyValue import PropertyValue
10from flowsheetInternals.unitops.services.edit_operations.recorder import (
11 tracked_bulk_update,
12)
14from .model_lookup import ModelLookupDict
17def update_formulas(
18 model_lookups: ModelLookupDict,
19 allow_not_found: bool = False,
20) -> None:
21 """Remap copied property IDs, optionally retaining outside references."""
23 property_values = model_lookups.get(PropertyValue)
24 if property_values is None:
25 return
26 for property_value in property_values:
27 if not property_value.formula:
28 continue
29 formula_ids = [
30 extract_id_from_formula_key(key)
31 for key in get_formula_keys(property_value.formula)
32 ]
33 copied_values = [property_values.get_model(pk) for pk in formula_ids]
34 if not allow_not_found and any(value is None for value in copied_values): 34 ↛ 35line 34 didn't jump to line 35 because the condition on line 34 was never true
35 property_value.formula = (
36 "Failed to copy formula, could not find property in flowsheet."
37 )
38 continue
39 remapped_keys = [
40 f"prop{copied.pk if copied is not None else source_pk}"
41 for source_pk, copied in zip(formula_ids, copied_values, strict=True)
42 ]
43 property_value.formula = validate_formula_length(
44 replace_props(property_value.formula, remapped_keys)
45 )
47 tracked_bulk_update(
48 PropertyValue.objects,
49 property_values,
50 ["formula"],
51 )