From 429706a81f2878078a1e01c63a301df8bf17adf1 Mon Sep 17 00:00:00 2001 From: Zina Rasoamanana Date: Mon, 26 Aug 2024 16:39:28 +0200 Subject: [PATCH] [IMP] mis_builder: allow definition of style expression on subkpi level --- mis_builder/models/kpimatrix.py | 55 ++++++++++++++++++++------------ mis_builder/models/mis_report.py | 5 +++ mis_builder/views/mis_report.xml | 1 + 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/mis_builder/models/kpimatrix.py b/mis_builder/models/kpimatrix.py index 3a9ab498f..aa3f48773 100644 --- a/mis_builder/models/kpimatrix.py +++ b/mis_builder/models/kpimatrix.py @@ -235,32 +235,30 @@ def set_values_detail_account( assert len(vals) == col.colspan assert len(drilldown_args) == col.colspan for val, drilldown_arg, subcol in zip(vals, drilldown_args, col.iter_subcols()): # noqa: B905 - if isinstance(val, DataError): - val_rendered = val.name - val_comment = val.msg - else: - val_rendered = self._style_model.render( - self.lang, row.style_props, kpi.type, val - ) - if row.kpi.multi and subcol.subkpi: - val_comment = "{}.{} = {}".format( - row.kpi.name, - subcol.subkpi.name, - row.kpi._get_expression_str_for_subkpi(subcol.subkpi), - ) - else: - val_comment = f"{row.kpi.name} = {row.kpi.expression}" cell_style_props = row.style_props - if row.kpi.style_expression: + + style_expression = "" + if ( + row.kpi.multi + and subcol.subkpi + and row.kpi.expression_ids.filtered( + lambda rec, subkpi=subcol.subkpi: rec.subkpi_id == subkpi + ).style_expression + ): + style_expression = row.kpi.expression_ids.filtered( + lambda rec, subkpi=subcol.subkpi: rec.subkpi_id == subkpi + ).style_expression + elif row.kpi.style_expression: + style_expression = row.kpi.style_expression + + if style_expression: # evaluate style expression try: - style_name = mis_safe_eval( - row.kpi.style_expression, col.locals_dict - ) + style_name = mis_safe_eval(style_expression, col.locals_dict) except Exception: _logger.error( "Error evaluating style expression <%s>", - row.kpi.style_expression, + style_expression, exc_info=True, ) if style_name: @@ -271,6 +269,23 @@ def set_values_detail_account( ) else: _logger.error("Style '%s' not found.", style_name) + + if isinstance(val, DataError): + val_rendered = val.name + val_comment = val.msg + else: + val_rendered = self._style_model.render( + self.lang, cell_style_props, kpi.type, val + ) + if row.kpi.multi and subcol.subkpi: + val_comment = "{}.{} = {}".format( + row.kpi.name, + subcol.subkpi.name, + row.kpi._get_expression_str_for_subkpi(subcol.subkpi), + ) + else: + val_comment = f"{row.kpi.name} = {row.kpi.expression}" + cell = KpiMatrixCell( row, subcol, diff --git a/mis_builder/models/mis_report.py b/mis_builder/models/mis_report.py index b2a6996ec..4ba4e7bd5 100644 --- a/mis_builder/models/mis_report.py +++ b/mis_builder/models/mis_report.py @@ -298,6 +298,11 @@ class MisReportKpiExpression(models.Model): # TODO FIXME set readonly=True when onchange('subkpi_ids') below works subkpi_id = fields.Many2one("mis.report.subkpi", readonly=False, ondelete="cascade") + style_expression = fields.Char( + help="An expression that returns a style depending on the KPI value. " + "Such style is applied on top of the row style.", + ) + _sql_constraints = [ ( "subkpi_kpi_unique", diff --git a/mis_builder/views/mis_report.xml b/mis_builder/views/mis_report.xml index 9ce492d4c..516e1c8df 100644 --- a/mis_builder/views/mis_report.xml +++ b/mis_builder/views/mis_report.xml @@ -142,6 +142,7 @@ domain="[('report_id', '=', parent.report_id)]" /> +