Skip to content

Commit

Permalink
[IMP] mis_builder: allow definition of style expression on subkpi level
Browse files Browse the repository at this point in the history
  • Loading branch information
AnizR committed Aug 28, 2024
1 parent 3aa42b4 commit 429706a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
55 changes: 35 additions & 20 deletions mis_builder/models/kpimatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions mis_builder/models/mis_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions mis_builder/views/mis_report.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
domain="[('report_id', '=', parent.report_id)]"
/>
<field name="name" />
<field name="style_expression" />
</tree>
</field>
<field
Expand Down

0 comments on commit 429706a

Please sign in to comment.