Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions kpi/models/kpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ def _compute_display_last_kpi_value(self):
obj.color = "#FFFFFF"
obj.last_execution = False

def _get_eval_context(self):
"""Return the evaluation context used for Python KPI expressions.

This method is meant to be extended by other modules to inject
additional safe globals into KPI formulas.
"""
self.ensure_one()
return {"self": self, "datetime": fields.datetime}

def _get_kpi_value(self):
self.ensure_one()
kpi_value = 0
Expand All @@ -163,9 +172,7 @@ def _get_kpi_value(self):
if is_one_value(res):
kpi_value = res[0]["value"]
elif self.kpi_type == "python":
kpi_value = safe_eval(
self.kpi_code, {"self": self, "datetime": fields.datetime}
)
kpi_value = safe_eval(self.kpi_code, self._get_eval_context())
if isinstance(kpi_value, dict):
res = kpi_value
else:
Expand Down
26 changes: 26 additions & 0 deletions kpi/tests/test_kpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,29 @@ def test_kpi_python(self):
self.assertEqual(len(kpi_history), 1)
self.assertEqual(kpi_history.color, "#00FF00")
self.assertEqual(kpi_history.value, 1.0)

def test_kpi_eval_context(self):
kpi_category = self.env["kpi.category"].create({"name": "Dynamic KPIs"})
kpi_threshold = self.env["kpi.threshold"].create(
{"name": "KPI Threshold for dynamic KPIs"}
)
kpi = self.env["kpi"].create(
{
"name": "KPI eval context",
"description": "KPI to test eval context",
"category_id": kpi_category.id,
"threshold_id": kpi_threshold.id,
"periodicity": 1,
"periodicity_uom": "day",
"kpi_type": "python",
"kpi_code": "self.name == 'KPI eval context' and 1.0 or 0.0",
}
)
ctx = kpi._get_eval_context()
self.assertIn("self", ctx)
self.assertIn("datetime", ctx)
self.assertEqual(ctx["self"], kpi)
kpi.update_kpi_value()
kpi_history = self.env["kpi.history"].search([("kpi_id", "=", kpi.id)])
self.assertEqual(len(kpi_history), 1)
self.assertEqual(kpi_history.value, 1.0)
Loading