Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP][16.0] mis_builder: allow definition of style expression on subkpi level #623

Draft
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Draft
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
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 = ""
AnizR marked this conversation as resolved.
Show resolved Hide resolved
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(
AnizR marked this conversation as resolved.
Show resolved Hide resolved
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
Loading