From ee275c03bf63a11fc98ea48ffd21fa80344145a6 Mon Sep 17 00:00:00 2001 From: Enric Tobella Date: Wed, 29 Jul 2020 18:43:01 +0200 Subject: [PATCH] [IMP] kpi_dashboard: edit color --- kpi_dashboard/demo/demo_dashboard.xml | 15 +++++---- kpi_dashboard/models/kpi_dashboard.py | 5 +++ kpi_dashboard/models/kpi_kpi.py | 2 ++ .../static/src/js/dashboard_controller.js | 32 ++++++++++++++++++- .../static/src/js/dashboard_renderer.js | 11 +++++++ kpi_dashboard/views/kpi_dashboard.xml | 4 +++ 6 files changed, 62 insertions(+), 7 deletions(-) diff --git a/kpi_dashboard/demo/demo_dashboard.xml b/kpi_dashboard/demo/demo_dashboard.xml index 2d460cd5..3a31344c 100644 --- a/kpi_dashboard/demo/demo_dashboard.xml +++ b/kpi_dashboard/demo/demo_dashboard.xml @@ -165,21 +165,24 @@ result = {"value": self.env.context.get('counter', 990)} +1 to Counter - 4 + 3 10 - 3 + 1 + 2 #B41F1F #EEBF77 {'counter': (context.counter or 990) + 1} + + check_if(((context.counter or 990) + 1) % 2, '#ff0000', '#00ff00') Counter - 2 - 10 + 3 + 11 3 #4B0082 #ffffff @@ -189,8 +192,8 @@ result = {"value": self.env.context.get('counter', 990)} Integer - 3 - 10 + 4 + 11 3 #ffffff #4B0082 diff --git a/kpi_dashboard/models/kpi_dashboard.py b/kpi_dashboard/models/kpi_dashboard.py index 244e402e..24d42301 100644 --- a/kpi_dashboard/models/kpi_dashboard.py +++ b/kpi_dashboard/models/kpi_dashboard.py @@ -119,6 +119,8 @@ class KpiDashboardItem(models.Model): font_color = fields.Char() modify_context = fields.Boolean() modify_context_expression = fields.Char() + modify_color = fields.Boolean() + modify_color_expression = fields.Char() @api.depends('row', 'size_y') def _compute_end_row(self): @@ -176,9 +178,12 @@ class KpiDashboardItem(models.Model): "color": self.color, "font_color": self.font_color or "000000", "modify_context": self.modify_context, + "modify_color": self.modify_color, } if self.modify_context: vals['modify_context_expression'] = self.modify_context_expression + if self.modify_color: + vals['modify_color_expression'] = self.modify_color_expression if self.kpi_id: vals.update( { diff --git a/kpi_dashboard/models/kpi_kpi.py b/kpi_dashboard/models/kpi_kpi.py index 3a9506c0..a16d613d 100644 --- a/kpi_dashboard/models/kpi_kpi.py +++ b/kpi_dashboard/models/kpi_kpi.py @@ -10,6 +10,7 @@ from odoo.tools.float_utils import float_compare import re import json import datetime +from dateutil import relativedelta class KpiKpi(models.Model): @@ -137,6 +138,7 @@ class KpiKpi(models.Model): "model": self.browse(), "datetime": datetime, "float_compare": float_compare, + "relativedelta": relativedelta.relativedelta, } def _forbidden_code(self): diff --git a/kpi_dashboard/static/src/js/dashboard_controller.js b/kpi_dashboard/static/src/js/dashboard_controller.js index a6efee0a..97db1765 100644 --- a/kpi_dashboard/static/src/js/dashboard_controller.js +++ b/kpi_dashboard/static/src/js/dashboard_controller.js @@ -11,11 +11,14 @@ odoo.define('kpi_dashboard.DashboardController', function (require) { init: function () { this._super.apply(this, arguments); this.dashboard_context = {}; + this.dashboard_color_data = [] }, custom_events: _.extend({}, BasicController.prototype.custom_events, { addDashboard: '_addDashboard', refresh_on_fly: '_refreshOnFly', modify_context: '_modifyContext', + add_modify_color: '_addModifyColor', + refresh_colors: '_refreshColors', }), _refreshOnFly: function (event) { var self = this; @@ -112,7 +115,34 @@ odoo.define('kpi_dashboard.DashboardController', function (require) { )}), ); this._refreshOnFly(event); - } + this._refreshColors(); + }, + _addModifyColor: function (event) { + this.dashboard_color_data.push([ + event.data.element_id, + event.data.expression, + ]); + }, + _refreshColors: function () { + var self = this; + var ctx = this._getContext(); + _.each(this.dashboard_color_data, function (data) { + var color = py.eval(data[1], { + context: _.extend(ctx, { + __getattr__: function() {return false}, + + }), + check_if: function(args) { + if (args[0].toJSON()) { + return args[1]; + } + return args[2]; + } + }); + var $element = self.renderer.$el.find('#' + data[0]); + $element.css('background-color', color); + }); + }, }); return DashboardController; diff --git a/kpi_dashboard/static/src/js/dashboard_renderer.js b/kpi_dashboard/static/src/js/dashboard_renderer.js index 3390b5f1..6bc38c49 100644 --- a/kpi_dashboard/static/src/js/dashboard_renderer.js +++ b/kpi_dashboard/static/src/js/dashboard_renderer.js @@ -36,7 +36,14 @@ odoo.define('kpi_dashboard.DashboardRenderer', function (require) { 'kpi_dashboard.kpi', {widget: kpi})); element.css('background-color', kpi.color); element.css('color', kpi.font_color); + element.attr('id', _.uniqueId('kpi_')); self.$grid.append(element); + if (kpi.modify_color) { + self.trigger_up("add_modify_color", { + element_id: element.attr("id"), + expression: kpi.modify_color_expression, + }) + } if (kpi.modify_context) { element.on("click", self._onClickModifyContext.bind( self, kpi.modify_context_expression)); @@ -71,6 +78,10 @@ odoo.define('kpi_dashboard.DashboardRenderer', function (require) { self.trigger_up('refresh_on_fly'); }, this.state.specialData.compute_on_fly_refresh *1000); }; + this.trigger_up('refresh_colors'); + this.trigger_up('refresh_on_fly'); + // We need to refreshs data in order compute with the current + // context return $.when(); }, on_detach_callback: function () { diff --git a/kpi_dashboard/views/kpi_dashboard.xml b/kpi_dashboard/views/kpi_dashboard.xml index f73eb9aa..90543125 100644 --- a/kpi_dashboard/views/kpi_dashboard.xml +++ b/kpi_dashboard/views/kpi_dashboard.xml @@ -150,6 +150,10 @@ + +