Skip to content

Commit ee275c0

Browse files
committed
[IMP] kpi_dashboard: edit color
1 parent 2eea8b7 commit ee275c0

File tree

6 files changed

+62
-7
lines changed

6 files changed

+62
-7
lines changed

kpi_dashboard/demo/demo_dashboard.xml

+9-6
Original file line numberDiff line numberDiff line change
@@ -165,21 +165,24 @@ result = {"value": self.env.context.get('counter', 990)}
165165
<record id="dashboard_widget_add_counter" model="kpi.dashboard.item">
166166
<field name="name">+1 to Counter</field>
167167
<field name="dashboard_id" ref="demo_dashboard"/>
168-
<field name="column">4</field>
168+
<field name="column">3</field>
169169
<field name="row">10</field>
170-
<field name="size_y">3</field>
170+
<field name="size_y">1</field>
171+
<field name="size_x">2</field>
171172
<field name="color">#B41F1F</field>
172173
<field name="font_color">#EEBF77</field>
173174
<field name="modify_context" eval="True"/>
174175
<field name="modify_context_expression">{'counter': (context.counter or 990) + 1}</field>
176+
<field name="modify_color" eval="True"/>
177+
<field name="modify_color_expression">check_if(((context.counter or 990) + 1) % 2, '#ff0000', '#00ff00')</field>
175178
</record>
176179

177180
<record id="dashboard_widget_counter" model="kpi.dashboard.item">
178181
<field name="name">Counter</field>
179182
<field name="dashboard_id" ref="demo_dashboard"/>
180183
<field name="kpi_id" ref="widget_counter"/>
181-
<field name="column">2</field>
182-
<field name="row">10</field>
184+
<field name="column">3</field>
185+
<field name="row">11</field>
183186
<field name="size_y">3</field>
184187
<field name="color">#4B0082</field>
185188
<field name="font_color">#ffffff</field>
@@ -189,8 +192,8 @@ result = {"value": self.env.context.get('counter', 990)}
189192
<field name="name">Integer</field>
190193
<field name="dashboard_id" ref="demo_dashboard"/>
191194
<field name="kpi_id" ref="widget_integer"/>
192-
<field name="column">3</field>
193-
<field name="row">10</field>
195+
<field name="column">4</field>
196+
<field name="row">11</field>
194197
<field name="size_y">3</field>
195198
<field name="color">#ffffff</field>
196199
<field name="font_color">#4B0082</field>

kpi_dashboard/models/kpi_dashboard.py

+5
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ class KpiDashboardItem(models.Model):
119119
font_color = fields.Char()
120120
modify_context = fields.Boolean()
121121
modify_context_expression = fields.Char()
122+
modify_color = fields.Boolean()
123+
modify_color_expression = fields.Char()
122124

123125
@api.depends('row', 'size_y')
124126
def _compute_end_row(self):
@@ -176,9 +178,12 @@ def _read_dashboard(self):
176178
"color": self.color,
177179
"font_color": self.font_color or "000000",
178180
"modify_context": self.modify_context,
181+
"modify_color": self.modify_color,
179182
}
180183
if self.modify_context:
181184
vals['modify_context_expression'] = self.modify_context_expression
185+
if self.modify_color:
186+
vals['modify_color_expression'] = self.modify_color_expression
182187
if self.kpi_id:
183188
vals.update(
184189
{

kpi_dashboard/models/kpi_kpi.py

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import re
1111
import json
1212
import datetime
13+
from dateutil import relativedelta
1314

1415

1516
class KpiKpi(models.Model):
@@ -137,6 +138,7 @@ def _get_code_input_dict(self):
137138
"model": self.browse(),
138139
"datetime": datetime,
139140
"float_compare": float_compare,
141+
"relativedelta": relativedelta.relativedelta,
140142
}
141143

142144
def _forbidden_code(self):

kpi_dashboard/static/src/js/dashboard_controller.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ odoo.define('kpi_dashboard.DashboardController', function (require) {
1111
init: function () {
1212
this._super.apply(this, arguments);
1313
this.dashboard_context = {};
14+
this.dashboard_color_data = []
1415
},
1516
custom_events: _.extend({}, BasicController.prototype.custom_events, {
1617
addDashboard: '_addDashboard',
1718
refresh_on_fly: '_refreshOnFly',
1819
modify_context: '_modifyContext',
20+
add_modify_color: '_addModifyColor',
21+
refresh_colors: '_refreshColors',
1922
}),
2023
_refreshOnFly: function (event) {
2124
var self = this;
@@ -112,7 +115,34 @@ odoo.define('kpi_dashboard.DashboardController', function (require) {
112115
)}),
113116
);
114117
this._refreshOnFly(event);
115-
}
118+
this._refreshColors();
119+
},
120+
_addModifyColor: function (event) {
121+
this.dashboard_color_data.push([
122+
event.data.element_id,
123+
event.data.expression,
124+
]);
125+
},
126+
_refreshColors: function () {
127+
var self = this;
128+
var ctx = this._getContext();
129+
_.each(this.dashboard_color_data, function (data) {
130+
var color = py.eval(data[1], {
131+
context: _.extend(ctx, {
132+
__getattr__: function() {return false},
133+
134+
}),
135+
check_if: function(args) {
136+
if (args[0].toJSON()) {
137+
return args[1];
138+
}
139+
return args[2];
140+
}
141+
});
142+
var $element = self.renderer.$el.find('#' + data[0]);
143+
$element.css('background-color', color);
144+
});
145+
},
116146
});
117147

118148
return DashboardController;

kpi_dashboard/static/src/js/dashboard_renderer.js

+11
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ odoo.define('kpi_dashboard.DashboardRenderer', function (require) {
3636
'kpi_dashboard.kpi', {widget: kpi}));
3737
element.css('background-color', kpi.color);
3838
element.css('color', kpi.font_color);
39+
element.attr('id', _.uniqueId('kpi_'));
3940
self.$grid.append(element);
41+
if (kpi.modify_color) {
42+
self.trigger_up("add_modify_color", {
43+
element_id: element.attr("id"),
44+
expression: kpi.modify_color_expression,
45+
})
46+
}
4047
if (kpi.modify_context) {
4148
element.on("click", self._onClickModifyContext.bind(
4249
self, kpi.modify_context_expression));
@@ -71,6 +78,10 @@ odoo.define('kpi_dashboard.DashboardRenderer', function (require) {
7178
self.trigger_up('refresh_on_fly');
7279
}, this.state.specialData.compute_on_fly_refresh *1000);
7380
};
81+
this.trigger_up('refresh_colors');
82+
this.trigger_up('refresh_on_fly');
83+
// We need to refreshs data in order compute with the current
84+
// context
7485
return $.when();
7586
},
7687
on_detach_callback: function () {

kpi_dashboard/views/kpi_dashboard.xml

+4
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@
150150
<field name="modify_context_expression"
151151
attrs="{'invisible': [('modify_context', '=', False)]}"
152152
widget="ace" options="{'mode': 'python'}"/>
153+
<field name="modify_color"/>
154+
<field name="modify_color_expression"
155+
attrs="{'invisible': [('modify_color', '=', False)]}"
156+
widget="ace" options="{'mode': 'python'}"/>
153157
</group>
154158
</sheet>
155159
<footer>

0 commit comments

Comments
 (0)