mirror of
https://github.com/OCA/web.git
synced 2025-02-22 13:21:25 +02:00
[MIG][12.0] web_dashboard_tile
- refactor tile category - improve description - add legalsylvain as maintainer - update code and translation
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# © 2010-2013 OpenERP s.a. (<http://openerp.com>).
|
||||
# © 2014 initOS GmbH & Co. KG (<http://www.initos.com>).
|
||||
# © 2015-Today GRAP
|
||||
@@ -6,22 +5,14 @@
|
||||
|
||||
import datetime
|
||||
import time
|
||||
from statistics import median
|
||||
from dateutil.relativedelta import relativedelta
|
||||
from collections import OrderedDict
|
||||
|
||||
from openerp import api, fields, models
|
||||
from openerp.tools.safe_eval import safe_eval as eval
|
||||
from openerp.tools.translate import _
|
||||
from openerp.exceptions import ValidationError, except_orm
|
||||
|
||||
|
||||
def median(vals):
|
||||
# https://docs.python.org/3/library/statistics.html#statistics.median
|
||||
# TODO : refactor, using statistics.median when Odoo will be available
|
||||
# in Python 3.4
|
||||
even = (0 if len(vals) % 2 else 1) + 1
|
||||
half = (len(vals) - 1) / 2
|
||||
return sum(sorted(vals)[half : half + even]) / float(even)
|
||||
from odoo import api, fields, models
|
||||
from odoo.tools.safe_eval import safe_eval as eval
|
||||
from odoo.tools.translate import _
|
||||
from odoo.exceptions import ValidationError, except_orm
|
||||
|
||||
|
||||
FIELD_FUNCTIONS = OrderedDict(
|
||||
@@ -59,7 +50,7 @@ FIELD_FUNCTIONS = OrderedDict(
|
||||
{
|
||||
"name": "Average",
|
||||
"func": lambda vals: sum(vals) / len(vals),
|
||||
"help": _("Minimum value of '%s'"),
|
||||
"help": _("Average value of '%s'"),
|
||||
},
|
||||
),
|
||||
(
|
||||
@@ -84,160 +75,211 @@ class TileTile(models.Model):
|
||||
_description = "Dashboard Tile"
|
||||
_order = "sequence, name"
|
||||
|
||||
def _get_eval_context(self):
|
||||
def _context_today():
|
||||
return fields.Date.from_string(fields.Date.context_today(self))
|
||||
|
||||
context = self.env.context.copy()
|
||||
context.update(
|
||||
{
|
||||
"time": time,
|
||||
"datetime": datetime,
|
||||
"relativedelta": relativedelta,
|
||||
"context_today": _context_today,
|
||||
"current_date": fields.Date.today(),
|
||||
}
|
||||
)
|
||||
return context
|
||||
|
||||
# Column Section
|
||||
name = fields.Char(required=True)
|
||||
|
||||
sequence = fields.Integer(default=0, required=True)
|
||||
category_id = fields.Many2one("tile.category", "Category")
|
||||
user_id = fields.Many2one("res.users", "User")
|
||||
background_color = fields.Char(default="#0E6C7E", oldname="color")
|
||||
|
||||
category_id = fields.Many2one(
|
||||
string="Category", comodel_name="tile.category", required=True,
|
||||
ondelete="CASCADE")
|
||||
|
||||
user_id = fields.Many2one(string="User", comodel_name="res.users")
|
||||
|
||||
background_color = fields.Char(default="#0E6C7E")
|
||||
|
||||
font_color = fields.Char(default="#FFFFFF")
|
||||
|
||||
group_ids = fields.Many2many(
|
||||
"res.groups",
|
||||
comodel_name="res.groups",
|
||||
string="Groups",
|
||||
help="If this field is set, only users of this group can view this "
|
||||
"tile. Please note that it will only work for global tiles "
|
||||
"(that is, when User field is left empty)",
|
||||
)
|
||||
|
||||
model_id = fields.Many2one("ir.model", "Model", required=True)
|
||||
model_id = fields.Many2one(
|
||||
comodel_name="ir.model", string="Model", required=True
|
||||
)
|
||||
|
||||
model_name = fields.Char(string="Model name", related="model_id.model")
|
||||
|
||||
domain = fields.Text(default="[]")
|
||||
action_id = fields.Many2one("ir.actions.act_window", "Action")
|
||||
|
||||
action_id = fields.Many2one(
|
||||
comodel_name="ir.actions.act_window",
|
||||
string="Action", help="Let empty to use the default action related to"
|
||||
" the selected model.",
|
||||
domain="[('res_model', '=', model_name)]")
|
||||
|
||||
active = fields.Boolean(
|
||||
compute="_compute_active", search="_search_active", readonly=True
|
||||
)
|
||||
|
||||
hide_if_null = fields.Boolean(
|
||||
string="Hide if null", help="If checked, the item will be hidden"
|
||||
" if the primary value is null.")
|
||||
|
||||
hidden = fields.Boolean(
|
||||
string="Hidden", compute="_compute_data",
|
||||
search="_search_hidden")
|
||||
|
||||
# Primary Value
|
||||
primary_function = fields.Selection(
|
||||
FIELD_FUNCTION_SELECTION, string="Function", default="count"
|
||||
string="Primary Function", required=True,
|
||||
selection=FIELD_FUNCTION_SELECTION, default="count",
|
||||
)
|
||||
|
||||
primary_field_id = fields.Many2one(
|
||||
"ir.model.fields",
|
||||
string="Field",
|
||||
comodel_name="ir.model.fields",
|
||||
string="Primary Field",
|
||||
domain="[('model_id', '=', model_id),"
|
||||
" ('ttype', 'in', ['float', 'integer', 'monetary'])]",
|
||||
)
|
||||
|
||||
primary_format = fields.Char(
|
||||
string="Format",
|
||||
string="Primary Format",
|
||||
help="Python Format String valid with str.format()\n"
|
||||
"ie: '{:,} Kgs' will output '1,000 Kgs' if value is 1000.",
|
||||
)
|
||||
primary_value = fields.Char(string="Value", compute="_compute_data")
|
||||
primary_helper = fields.Char(string="Helper", compute="_compute_helper")
|
||||
|
||||
primary_value = fields.Float(
|
||||
string="Primary Value", compute="_compute_data")
|
||||
|
||||
primary_formated_value = fields.Char(
|
||||
string="Primary Formated Value", compute="_compute_data")
|
||||
|
||||
primary_helper = fields.Char(
|
||||
string="Primary Helper", compute="_compute_helper",
|
||||
store=True)
|
||||
|
||||
# Secondary Value
|
||||
secondary_function = fields.Selection(
|
||||
FIELD_FUNCTION_SELECTION, string="Secondary Function"
|
||||
string="Secondary Function", selection=FIELD_FUNCTION_SELECTION,
|
||||
)
|
||||
|
||||
secondary_field_id = fields.Many2one(
|
||||
"ir.model.fields",
|
||||
comodel_name="ir.model.fields",
|
||||
string="Secondary Field",
|
||||
domain="[('model_id', '=', model_id),"
|
||||
" ('ttype', 'in', ['float', 'integer', 'monetary'])]",
|
||||
)
|
||||
|
||||
secondary_format = fields.Char(
|
||||
string="Secondary Format",
|
||||
help="Python Format String valid with str.format()\n"
|
||||
"ie: '{:,} Kgs' will output '1,000 Kgs' if value is 1000.",
|
||||
)
|
||||
secondary_value = fields.Char(
|
||||
string="Secondary Value", compute="_compute_data"
|
||||
|
||||
secondary_value = fields.Float(
|
||||
string="Secondary Value", compute="_compute_data")
|
||||
|
||||
secondary_formated_value = fields.Char(
|
||||
string="Secondary Formated Value", compute="_compute_data"
|
||||
)
|
||||
|
||||
secondary_helper = fields.Char(
|
||||
string="Secondary Helper", compute="_compute_helper"
|
||||
string="Secondary Helper", compute="_compute_helper",
|
||||
store=True
|
||||
)
|
||||
|
||||
error = fields.Char(string="Error Details", compute="_compute_data")
|
||||
|
||||
@api.one
|
||||
# Compute Section
|
||||
@api.depends("primary_format", "secondary_format", "model_id", "domain")
|
||||
def _compute_data(self):
|
||||
if not self.active:
|
||||
return
|
||||
model = self.env[self.model_id.model]
|
||||
eval_context = self._get_eval_context()
|
||||
domain = self.domain or "[]"
|
||||
try:
|
||||
count = model.search_count(eval(domain, eval_context))
|
||||
except Exception as e:
|
||||
self.primary_value = self.secondary_value = "ERR!"
|
||||
self.error = str(e)
|
||||
return
|
||||
fields = [
|
||||
f.name
|
||||
for f in [self.primary_field_id, self.secondary_field_id]
|
||||
if f
|
||||
]
|
||||
read_vals = (
|
||||
fields
|
||||
and model.search_read(eval(domain, eval_context), fields)
|
||||
or []
|
||||
)
|
||||
for f in ["primary_", "secondary_"]:
|
||||
f_function = f + "function"
|
||||
f_field_id = f + "field_id"
|
||||
f_format = f + "format"
|
||||
f_value = f + "value"
|
||||
value = 0
|
||||
if not self[f_function]:
|
||||
self[f_value] = False
|
||||
else:
|
||||
if self[f_function] == "count":
|
||||
value = count
|
||||
for tile in self:
|
||||
if not tile.model_id or not tile.active:
|
||||
return
|
||||
model = self.env[tile.model_id.model]
|
||||
eval_context = self._get_eval_context()
|
||||
domain = tile.domain or "[]"
|
||||
try:
|
||||
count = model.search_count(eval(domain, eval_context))
|
||||
except Exception as e:
|
||||
tile.primary_value = 0.0
|
||||
tile.primary_formated_value =\
|
||||
tile.secondary_formated_value = _("Error")
|
||||
tile.error = str(e)
|
||||
return
|
||||
fields = [
|
||||
f.name
|
||||
for f in [tile.primary_field_id, tile.secondary_field_id]
|
||||
if f
|
||||
]
|
||||
read_vals = (
|
||||
fields
|
||||
and model.search_read(eval(domain, eval_context), fields)
|
||||
or []
|
||||
)
|
||||
for f in ["primary_", "secondary_"]:
|
||||
f_function = f + "function"
|
||||
f_field_id = f + "field_id"
|
||||
f_format = f + "format"
|
||||
f_value = f + "value"
|
||||
f_formated_value = f + "formated_value"
|
||||
value = 0
|
||||
if not tile[f_function]:
|
||||
tile[f_value] = 0.0
|
||||
tile[f_formated_value] = False
|
||||
else:
|
||||
func = FIELD_FUNCTIONS[self[f_function]]["func"]
|
||||
vals = [x[self[f_field_id].name] for x in read_vals]
|
||||
value = func(vals)
|
||||
try:
|
||||
self[f_value] = (self[f_format] or "{:,}").format(value)
|
||||
except ValueError as e:
|
||||
self[f_value] = "F_ERR!"
|
||||
self.error = str(e)
|
||||
return
|
||||
if tile[f_function] == "count":
|
||||
value = count
|
||||
else:
|
||||
func = FIELD_FUNCTIONS[tile[f_function]]["func"]
|
||||
vals = [x[tile[f_field_id].name] for x in read_vals]
|
||||
value = func(vals or [0.0])
|
||||
try:
|
||||
tile[f_value] = value
|
||||
tile[f_formated_value] = (
|
||||
tile[f_format] or "{:,}").format(value)
|
||||
if tile.hide_if_null and not value:
|
||||
tile.hidden = True
|
||||
except ValueError as e:
|
||||
tile[f_value] = 0.0
|
||||
tile[f_formated_value] = _("Error")
|
||||
tile.error = str(e)
|
||||
|
||||
@api.one
|
||||
@api.onchange(
|
||||
@api.depends(
|
||||
"primary_function",
|
||||
"primary_field_id",
|
||||
"secondary_function",
|
||||
"secondary_field_id",
|
||||
)
|
||||
def _compute_helper(self):
|
||||
for f in ["primary_", "secondary_"]:
|
||||
f_function = f + "function"
|
||||
f_field_id = f + "field_id"
|
||||
f_helper = f + "helper"
|
||||
self[f_helper] = ""
|
||||
field_func = FIELD_FUNCTIONS.get(self[f_function], {})
|
||||
help = field_func.get("help", False)
|
||||
if help:
|
||||
if self[f_function] != "count" and self[f_field_id]:
|
||||
desc = self[f_field_id].field_description
|
||||
self[f_helper] = help % desc
|
||||
else:
|
||||
self[f_helper] = help
|
||||
for tile in self:
|
||||
for f in ["primary_", "secondary_"]:
|
||||
f_function = f + "function"
|
||||
f_field_id = f + "field_id"
|
||||
f_helper = f + "helper"
|
||||
tile[f_helper] = ""
|
||||
field_func = FIELD_FUNCTIONS.get(tile[f_function], {})
|
||||
help = field_func.get("help", False)
|
||||
if help:
|
||||
if tile[f_function] != "count" and tile[f_field_id]:
|
||||
desc = tile[f_field_id].field_description
|
||||
tile[f_helper] = help % desc
|
||||
else:
|
||||
tile[f_helper] = help
|
||||
|
||||
@api.one
|
||||
def _compute_active(self):
|
||||
ima = self.env["ir.model.access"]
|
||||
for rec in self:
|
||||
rec.active = ima.check(rec.model_id.model, "read", False)
|
||||
for tile in self:
|
||||
if tile.model_id:
|
||||
tile.active = ima.check(tile.model_id.model, "read", False)
|
||||
else:
|
||||
tile.active = True
|
||||
|
||||
# Search Sections
|
||||
def _search_hidden(self, operator, operand):
|
||||
items = self.search([])
|
||||
hidden_tile_ids = [x.id for x in items if x.hidden]
|
||||
if (operator == "=" and operand is False) or\
|
||||
(operator == "!=" and operand is True):
|
||||
domain = [("id", "not in", hidden_tile_ids)]
|
||||
else:
|
||||
domain = [("id", "in", hidden_tile_ids)]
|
||||
return domain
|
||||
|
||||
def _search_active(self, operator, value):
|
||||
cr = self.env.cr
|
||||
@@ -259,8 +301,7 @@ class TileTile(models.Model):
|
||||
ids.append(result[0])
|
||||
return [("id", "in", ids)]
|
||||
|
||||
# Constraints and onchanges
|
||||
@api.multi
|
||||
# Constraints Sections
|
||||
@api.constrains("model_id", "primary_field_id", "secondary_field_id")
|
||||
def _check_model_id_field_id(self):
|
||||
for rec in self:
|
||||
@@ -276,10 +317,12 @@ class TileTile(models.Model):
|
||||
_("Please select a field from the selected model.")
|
||||
)
|
||||
|
||||
# Onchange Sections
|
||||
@api.onchange("model_id")
|
||||
def _onchange_model_id(self):
|
||||
self.primary_field_id = False
|
||||
self.secondary_field_id = False
|
||||
self.action_id = False
|
||||
|
||||
@api.onchange("primary_function", "secondary_function")
|
||||
def _onchange_function(self):
|
||||
@@ -319,3 +362,20 @@ class TileTile(models.Model):
|
||||
.id
|
||||
)
|
||||
self.create(vals)
|
||||
|
||||
@api.model
|
||||
def _get_eval_context(self):
|
||||
def _context_today():
|
||||
return fields.Date.from_string(fields.Date.context_today(self))
|
||||
|
||||
context = self.env.context.copy()
|
||||
context.update(
|
||||
{
|
||||
"time": time,
|
||||
"datetime": datetime,
|
||||
"relativedelta": relativedelta,
|
||||
"context_today": _context_today,
|
||||
"current_date": fields.Date.today(),
|
||||
}
|
||||
)
|
||||
return context
|
||||
|
||||
Reference in New Issue
Block a user