3
0
Fork 0

Merge pull request #405 from ivantodorovich/dashboard_tile_improvements

[8.0][IMP] web_dashboard_tile: context_today and a few other improvements.
8.0
Sylvain LE GAL 2016-08-30 00:52:51 +02:00 committed by GitHub
commit 5f3eabbd10
9 changed files with 189 additions and 224 deletions

View File

@ -1,38 +1,41 @@
Add Tiles to Dashboard Dashboard Tiles
====================== ===============
module to give you a dashboard where you can configure tile from any view Adds a dashboard where you can configure tiles from any view
and add them as short cut. and add them as short cut.
* Tile can be: * Tile can be:
* displayed only for a user; * Displayed only for a user.
* global for all users (In that case, some tiles will be hidden if * Global for all users (In that case, some tiles will be hidden if
the current user doesn't have access to the given model); the current user doesn't have access to the given model).
* The tile displays items count of a given model restricted to a given domain; * The tile displays items count of a given model restricted to a given domain.
* Optionnaly, the tile can display the result of a function of a field; * Optionally, the tile can display the result of a function of a field
* Function is one of sum/avg/min/max/median; * Function is one of sum/avg/min/max/median.
* Field must be integer or float; * Field must be integer or float.
Usage Usage
===== =====
* Dashboad sample, displaying Sale Orders to invoice: * Dashboad sample, displaying Sale Orders to invoice:
.. image:: ./static/src/img/screenshot_dashboard.png .. image:: ./static/src/img/screenshot_dashboard.png
* Tree view displayed when user click on the tile: * Tree view displayed when user click on the tile:
.. image:: ./static/src/img/screenshot_action_click.png .. image:: ./static/src/img/screenshot_action_click.png
Kown issues/limits Known issues
================== ============
* can not edit tile from dashboard (color, sequence, function, ...); * Can not edit tile from dashboard (color, sequence, function, ...).
* context are ignored; * Original context is ignored.
* date filter can not be relative; * Original domain and filter are not restored.
* combine domain of menue and filter so can not restore origin filter; * To preserve a relative date domain, you have to manually edit the tile's domain from `Configuration > User Interface > Dashboard Tile`. You can use the same variables available in filters (`uid`, `context_today()`, `current_date`, `time`, `datetime`, `relativedelta`).
possible future improvments Roadmap
=========================== =======
* support context_today; * Add icons.
* add icons; * Support client side action (like inbox).
* support client side action (like inbox); * Restore original Domain + Filter when an action is set.
Bug Tracker Bug Tracker
=========== ===========
@ -51,6 +54,7 @@ Contributors
* Markus Schneider <markus.schneider at initos.com> * Markus Schneider <markus.schneider at initos.com>
* Sylvain Le Gal (https://twitter.com/legalsylvain) * Sylvain Le Gal (https://twitter.com/legalsylvain)
* Iván Todorovich <ivan.todorovich@gmail.com>
Maintainer Maintainer
---------- ----------

View File

@ -1,26 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
############################################################################## # © 2010-2013 OpenERP s.a. (<http://openerp.com>).
# # © 2014 initOS GmbH & Co. KG (<http://www.initos.com>).
# OpenERP, Open Source Management Solution # © 2015-Today GRAP
# Copyright (C) 2010-2013 OpenERP s.a. (<http://openerp.com>). # License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
# Copyright (C) 2014 initOS GmbH & Co. KG (<http://www.initos.com>).
# Copyright (C) 2015-Today GRAP
# Author Markus Schneider <markus.schneider at initos.com>
# @author Sylvain LE GAL (https://twitter.com/legalsylvain)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from . import models from . import models

View File

@ -1,38 +1,27 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
############################################################################## # © 2010-2013 OpenERP s.a. (<http://openerp.com>).
# # © 2014 initOS GmbH & Co. KG (<http://www.initos.com>).
# OpenERP, Open Source Management Solution # License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
# Copyright (C) 2010-2013 OpenERP s.a. (<http://openerp.com>).
# Copyright (C) 2014 initOS GmbH & Co. KG (<http://www.initos.com>).
# Author Markus Schneider <markus.schneider at initos.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
{ {
"name": "Dashboard Tile", "name": "Dashboard Tile",
"summary": "Add Tiles to Dashboard", "summary": "Add Tiles to Dashboard",
"version": "8.0.1.0.0", "version": "8.0.1.1.0",
"depends": [ "depends": [
'web', 'web',
'board', 'board',
'mail', 'mail',
'web_widget_color', 'web_widget_color',
], ],
'author': "initOS GmbH & Co. KG,GRAP,Odoo Community Association (OCA)", 'author': 'initOS GmbH & Co. KG, '
'GRAP, '
'Odoo Community Association (OCA)',
"category": "web", "category": "web",
'license': 'AGPL-3', 'license': 'AGPL-3',
'contributors': [
'initOS GmbH & Co. KG',
'GRAP',
'Iván Todorovich <ivan.todorovich@gmail.com>'
],
'data': [ 'data': [
'views/tile.xml', 'views/tile.xml',
'views/templates.xml', 'views/templates.xml',

View File

@ -1,23 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
############################################################################## # © 2010-2013 OpenERP s.a. (<http://openerp.com>).
# # © 2014 initOS GmbH & Co. KG (<http://www.initos.com>).
# OpenERP, Open Source Management Solution # © 2015-Today GRAP
# Copyright (C) 2015-Today GRAP # License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
# @author Sylvain LE GAL (https://twitter.com/legalsylvain)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from . import tile_tile from . import tile_tile

View File

@ -1,35 +1,20 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
############################################################################## # © 2010-2013 OpenERP s.a. (<http://openerp.com>).
# # © 2014 initOS GmbH & Co. KG (<http://www.initos.com>).
# OpenERP, Open Source Management Solution # © 2015-Today GRAP
# Copyright (C) 2010-2013 OpenERP s.a. (<http://openerp.com>). # License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
# Copyright (C) 2014 initOS GmbH & Co. KG (<http://www.initos.com>).
# Copyright (C) 2015-Today GRAP
# Author Markus Schneider <markus.schneider at initos.com>
# @author Sylvain LE GAL (https://twitter.com/legalsylvain)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp import api, fields import datetime
from openerp.models import Model import time
from openerp.exceptions import except_orm from dateutil.relativedelta import relativedelta
from openerp import api, fields, models
from openerp.tools.safe_eval import safe_eval as eval
from openerp.tools.translate import _ from openerp.tools.translate import _
from openerp.exceptions import ValidationError, except_orm
class TileTile(Model): class TileTile(models.Model):
_name = 'tile.tile' _name = 'tile.tile'
_order = 'sequence, name' _order = 'sequence, name'
@ -37,77 +22,38 @@ class TileTile(Model):
# https://docs.python.org/3/library/statistics.html#statistics.median # https://docs.python.org/3/library/statistics.html#statistics.median
# TODO : refactor, using statistics.median when Odoo will be available # TODO : refactor, using statistics.median when Odoo will be available
# in Python 3.4 # in Python 3.4
even = (0 if len(aList) % 2 else 1) + 1 even = (0 if len(aList) % 2 else 1) + 1
half = (len(aList) - 1) / 2 half = (len(aList) - 1) / 2
return sum(sorted(aList)[half:half + even]) / float(even) return sum(sorted(aList)[half:half + even]) / float(even)
def _get_tile_info(self): def _get_eval_context(self):
ima_obj = self.env['ir.model.access'] def _context_today():
res = {} return fields.Date.from_string(fields.Date.context_today(self))
for r in self: context = self.env.context.copy()
r.active = False context.update({
r.count = 0 'time': time,
r.computed_value = 0 'datetime': datetime,
r.helper = '' 'relativedelta': relativedelta,
if ima_obj.check(r.model_id.model, 'read', False): 'context_today': _context_today,
# Compute count item 'current_date': fields.Date.today(),
model = self.env[r.model_id.model] })
r.count = model.search_count(eval(r.domain)) return context
r.active = True
# Compute datas for field_id depending of field_function
if r.field_function and r.field_id and r.count != 0:
records = model.search(eval(r.domain))
vals = [x[r.field_id.name] for x in records]
desc = r.field_id.field_description
if r.field_function == 'min':
r.computed_value = min(vals)
r.helper = _("Minimum value of '%s'") % desc
elif r.field_function == 'max':
r.computed_value = max(vals)
r.helper = _("Maximum value of '%s'") % desc
elif r.field_function == 'sum':
r.computed_value = sum(vals)
r.helper = _("Total value of '%s'") % desc
elif r.field_function == 'avg':
r.computed_value = sum(vals) / len(vals)
r.helper = _("Average value of '%s'") % desc
elif r.field_function == 'median':
r.computed_value = self.median(vals)
r.helper = _("Median value of '%s'") % desc
return res
def _search_active(self, operator, value):
cr = self.env.cr
if operator != '=':
raise except_orm(
'Unimplemented Feature',
'Search on Active field disabled.')
ima_obj = self.env['ir.model.access']
ids = []
cr.execute("""
SELECT tt.id, im.model
FROM tile_tile tt
INNER JOIN ir_model im
ON tt.model_id = im.id""")
for result in cr.fetchall():
if (ima_obj.check(result[1], 'read', False) == value):
ids.append(result[0])
return [('id', 'in', ids)]
# Column Section # Column Section
name = fields.Char(required=True) name = fields.Char(required=True)
model_id = fields.Many2one( sequence = fields.Integer(default=0, required=True)
comodel_name='ir.model', string='Model', required=True) user_id = fields.Many2one('res.users', 'User')
user_id = fields.Many2one( background_color = fields.Char(default='#0E6C7E', oldname='color')
comodel_name='res.users', string='User') font_color = fields.Char(default='#FFFFFF')
model_id = fields.Many2one('ir.model', 'Model', required=True)
domain = fields.Text(default='[]') domain = fields.Text(default='[]')
action_id = fields.Many2one( action_id = fields.Many2one('ir.actions.act_window', 'Action')
comodel_name='ir.actions.act_window', string='Action')
count = fields.Integer(compute='_get_tile_info') count = fields.Integer(compute='_compute_data')
computed_value = fields.Float(compute='_get_tile_info') computed_value = fields.Float(compute='_compute_data')
helper = fields.Char(compute='_get_tile_info')
field_function = fields.Selection(selection=[ field_function = fields.Selection([
('min', 'Minimum'), ('min', 'Minimum'),
('max', 'Maximum'), ('max', 'Maximum'),
('sum', 'Sum'), ('sum', 'Sum'),
@ -115,41 +61,95 @@ class TileTile(Model):
('median', 'Median'), ('median', 'Median'),
], string='Function') ], string='Function')
field_id = fields.Many2one( field_id = fields.Many2one(
comodel_name='ir.model.fields', string='Field', 'ir.model.fields',
string='Field',
domain="[('model_id', '=', model_id)," domain="[('model_id', '=', model_id),"
" ('ttype', 'in', ['float', 'int'])]") " ('ttype', 'in', ['float', 'int'])]")
helper = fields.Char(compute='_compute_function_helper')
active = fields.Boolean( active = fields.Boolean(
compute='_get_tile_info', readonly=True, search='_search_active') compute='_compute_active',
background_color = fields.Char(default='#0E6C7E', oldname='color') search='_search_active',
font_color = fields.Char(default='#FFFFFF') readonly=True)
sequence = fields.Integer(default=0, required=True)
# Constraint Section @api.one
def _check_model_id_field_id(self, cr, uid, ids, context=None): def _compute_data(self):
for t in self.browse(cr, uid, ids, context=context): self.count = 0
if t.field_id and t.field_id.model_id.id != t.model_id.id: self.computed_value = 0
return False if self.active:
return True # Compute count item
model = self.env[self.model_id.model]
eval_context = self._get_eval_context()
self.count = model.search_count(eval(self.domain, eval_context))
# Compute datas for field_id depending of field_function
if self.field_function and self.field_id and self.count != 0:
records = model.search(eval(self.domain, eval_context))
vals = [x[self.field_id.name] for x in records]
if self.field_function == 'min':
self.computed_value = min(vals)
elif self.field_function == 'max':
self.computed_value = max(vals)
elif self.field_function == 'sum':
self.computed_value = sum(vals)
elif self.field_function == 'avg':
self.computed_value = sum(vals) / len(vals)
elif self.field_function == 'median':
self.computed_value = self.median(vals)
def _check_field_id_field_function(self, cr, uid, ids, context=None): @api.one
for t in self.browse(cr, uid, ids, context=context): @api.onchange('field_function', 'field_id')
if t.field_id and not t.field_function or\ def _compute_function_helper(self):
t.field_function and not t.field_id: self.helper = ''
return False if self.field_function and self.field_id:
return True desc = self.field_id.field_description
helpers = {
'min': "Minimum value of '%s'",
'max': "Maximum value of '%s'",
'sum': "Total value of '%s'",
'avg': "Average value of '%s'",
'median': "Median value of '%s'",
}
self.helper = _(helpers.get(self.field_function, '')) % desc
_constraints = [ @api.one
( def _compute_active(self):
_check_model_id_field_id, ima = self.env['ir.model.access']
"Error ! Please select a field of the selected model.", self.active = ima.check(self.model_id.model, 'read', False)
['model_id', 'field_id']),
(
_check_field_id_field_function,
"Error ! Please set both fields: 'Field' and 'Function'.",
['field_id', 'field_function']),
]
# View / action Section def _search_active(self, operator, value):
cr = self.env.cr
if operator != '=':
raise except_orm(
_('Unimplemented Feature. Search on Active field disabled.'))
ima = self.env['ir.model.access']
ids = []
cr.execute("""
SELECT tt.id, im.model
FROM tile_tile tt
INNER JOIN ir_model im
ON tt.model_id = im.id""")
for result in cr.fetchall():
if (ima.check(result[1], 'read', False) == value):
ids.append(result[0])
return [('id', 'in', ids)]
# Constraints and onchanges
@api.one
@api.constrains('model_id', 'field_id')
def _check_model_id_field_id(self):
if self.field_id and self.field_id.model_id.id != self.model_id.id:
raise ValidationError(
_("Please select a field from the selected model."))
@api.one
@api.constrains('field_id', 'field_function')
def _check_field_id_field_function(self):
validations = self.field_id, self.field_function
if any(validations) and not all(validations):
raise ValidationError(
_("Please set both: 'Field' and 'Function'."))
# Action methods
@api.multi @api.multi
def open_link(self): def open_link(self):
res = { res = {
@ -166,8 +166,7 @@ class TileTile(Model):
} }
if self.action_id: if self.action_id:
res.update(self.action_id.read( res.update(self.action_id.read(
['view_type', 'view_mode', 'view_id', 'type'])[0]) ['view_type', 'view_mode', 'type'])[0])
# FIXME: restore original Domain + Filter would be better
return res return res
@api.model @api.model

View File

@ -1,24 +1,24 @@
.openerp .oe_kanban_view .oe_dashbaord_tile{ .openerp .oe_kanban_view .oe_dashboard_tile {
width: 150px; width: 150px;
height: 150px; height: 150px;
border: 0; border: 0;
border-radius: 0; border-radius: 0;
} }
.openerp .oe_kanban_view .oe_dashbaord_tile .tile_label, .openerp .oe_kanban_view .oe_dashboard_tile .tile_label,
.openerp .oe_kanban_view .oe_dashbaord_tile .tile_count_without_computed_value, .openerp .oe_kanban_view .oe_dashboard_tile .tile_count_without_computed_value,
.openerp .oe_kanban_view .oe_dashbaord_tile .tile_count_with_computed_value, .openerp .oe_kanban_view .oe_dashboard_tile .tile_count_with_computed_value,
.openerp .oe_kanban_view .oe_dashbaord_tile .tile_computed_value { .openerp .oe_kanban_view .oe_dashboard_tile .tile_computed_value {
width: 140px; width: 140px;
text-align: center; text-align: center;
} }
.openerp .oe_kanban_view .oe_dashbaord_tile .tile_label{ .openerp .oe_kanban_view .oe_dashboard_tile .tile_label{
padding: 5px; padding: 5px;
font-size: 15px; font-size: 15px;
} }
.openerp .oe_kanban_view .oe_dashbaord_tile .tile_count_without_computed_value{ .openerp .oe_kanban_view .oe_dashboard_tile .tile_count_without_computed_value{
font-size: 52px; font-size: 52px;
font-weight: bold; font-weight: bold;
position: absolute; position: absolute;
@ -26,7 +26,7 @@
bottom: 5px; bottom: 5px;
} }
.openerp .oe_kanban_view .oe_dashbaord_tile .tile_count_with_computed_value{ .openerp .oe_kanban_view .oe_dashboard_tile .tile_count_with_computed_value{
font-size: 38px; font-size: 38px;
font-weight: bold; font-weight: bold;
position: absolute; position: absolute;
@ -34,7 +34,7 @@
bottom: 30px; bottom: 30px;
} }
.openerp .oe_kanban_view .oe_dashbaord_tile .tile_computed_value{ .openerp .oe_kanban_view .oe_dashboard_tile .tile_computed_value{
font-size: 18px; font-size: 18px;
font-weight: bold; font-weight: bold;
position: absolute; position: absolute;
@ -42,3 +42,11 @@
bottom: 5px; bottom: 5px;
font-style: italic; font-style: italic;
} }
.openerp .oe_searchview_drawer .oe_searchview_dashboard .oe_dashboard_tile_form {
display: none;
}
.openerp .oe_searchview_drawer .oe_opened .oe_dashboard_tile_form {
display: block;
}

View File

@ -35,13 +35,13 @@ _.mixin({
var self = this; var self = this;
this.$('#add_dashboard_tile').on('click', this, function (){ this.$('#add_dashboard_tile').on('click', this, function (){
self.save_tile(); self.save_tile();
}) });
}, },
render_data: function(dashboard_choices){ render_data: function(dashboard_choices){
var selection = instance.web.qweb.render( var selection = instance.web.qweb.render(
"SearchView.addtodashboard.selection", { "SearchView.addtodashboard.selection", {
selections: dashboard_choices}); selections: dashboard_choices});
this.$("form input").before(selection) this.$("form input").before(selection);
}, },
save_tile: function () { save_tile: function () {
var self = this; var self = this;
@ -97,4 +97,4 @@ _.mixin({
} }
}); });
} };

View File

@ -2,7 +2,7 @@
<templates id="template" xml:space="preserve"> <templates id="template" xml:space="preserve">
<t t-extend="SearchView.addtodashboard"> <t t-extend="SearchView.addtodashboard">
<t t-jquery="form" t-operation="after"> <t t-jquery="form" t-operation="after">
<div> <div class="oe_dashboard_tile_form">
<label for="dashboard_tile_new_name">Tile:</label> <label for="dashboard_tile_new_name">Tile:</label>
<input id="dashboard_tile_new_name" /> <input id="dashboard_tile_new_name" />
<button id="add_dashboard_tile">Create</button> <button id="add_dashboard_tile">Create</button>

View File

@ -61,7 +61,7 @@
<field name="helper" /> <field name="helper" />
<templates> <templates>
<t t-name="kanban-box"> <t t-name="kanban-box">
<div t-attf-class="oe_dashbaord_tile oe_kanban_global_click" t-attf-style="background-color:#{record.background_color.raw_value}" > <div t-attf-class="oe_dashboard_tile oe_kanban_global_click" t-attf-style="background-color:#{record.background_color.raw_value}" >
<div class="oe_kanban_content"> <div class="oe_kanban_content">
<a type="object" name="open_link" args="[]" t-attf-style="color:#{record.font_color.raw_value};"> <a type="object" name="open_link" args="[]" t-attf-style="color:#{record.font_color.raw_value};">
<div class="tile_label"> <div class="tile_label">