[IMP] kpi_dashboard: Allow to set a specific context for a KPI

pull/575/head
Enric Tobella 2022-01-11 15:00:31 +01:00
parent a77de56567
commit 91604231d4
2 changed files with 19 additions and 7 deletions

View File

@ -3,6 +3,7 @@
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.tools.safe_eval import safe_eval
class KpiDashboard(models.Model):
@ -113,9 +114,11 @@ class KpiDashboardItem(models.Model):
color = fields.Char()
font_color = fields.Char()
modify_context = fields.Boolean()
compute_on_fly = fields.Boolean(related="kpi_id.compute_on_fly")
modify_context_expression = fields.Char()
modify_color = fields.Boolean()
modify_color_expression = fields.Char()
special_context = fields.Char()
@api.depends("row", "size_y")
def _compute_end_row(self):
@ -189,9 +192,17 @@ class KpiDashboardItem(models.Model):
}
)
if self.kpi_id.compute_on_fly:
kpi = self.kpi_id
if self.special_context:
try:
ctx = safe_eval(self.special_context)
if isinstance(ctx, dict):
kpi = kpi.with_context(**ctx)
except SyntaxError:
pass
vals.update(
{
"value": self.kpi_id._compute_value(),
"value": kpi._compute_value(),
"value_last_update": fields.Datetime.now(),
}
)

View File

@ -162,15 +162,16 @@
widget="ace"
options="{'mode': 'python'}"
/>
<field
name="special_context"
attrs="{'invisible': [('compute_on_fly', '=', False)]}"
/>
<field name="compute_on_fly" invisible="1" />
</group>
</sheet>
<footer>
<button
name="write"
string="Save"
type="object"
class="oe_highlight"
/>
<button string="Save" special="save" class="oe_highlight" />
<button special="cancel" string="Cancel" class="oe_link" />
</footer>
</form>