mirror of
https://github.com/OCA/web.git
synced 2025-02-22 13:21:25 +02:00
[MIG] web_dashboard_tile from 12.0 to 16.0
- remove 12.0 migration scripts - bump version to 16.0 - use new api convention @api.model_create_multi ; new compute function logic ; - add dependency to spreasheet_dashboard to use 'Dashboard' main menu item - use new way to include assets - remove totally useless controllers - distinct different errors, depending on domain or format errors - fix : _compute_data depends on many fields - update : documentation and printscreens
This commit is contained in:
@@ -3,8 +3,6 @@
|
||||
# © 2015-Today GRAP
|
||||
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
|
||||
|
||||
import datetime
|
||||
import time
|
||||
from collections import OrderedDict
|
||||
from statistics import median
|
||||
|
||||
@@ -12,7 +10,7 @@ from dateutil.relativedelta import relativedelta
|
||||
|
||||
from odoo import api, fields, models
|
||||
from odoo.exceptions import ValidationError, except_orm
|
||||
from odoo.tools.safe_eval import safe_eval as eval
|
||||
from odoo.tools.safe_eval import safe_eval
|
||||
from odoo.tools.translate import _
|
||||
|
||||
FIELD_FUNCTIONS = OrderedDict(
|
||||
@@ -101,11 +99,15 @@ class TileTile(models.Model):
|
||||
"(that is, when User field is left empty)",
|
||||
)
|
||||
|
||||
model_id = fields.Many2one(comodel_name="ir.model", string="Model", required=True)
|
||||
model_id = fields.Many2one(
|
||||
comodel_name="ir.model", string="Model", required=True, ondelete="cascade"
|
||||
)
|
||||
|
||||
model_name = fields.Char(string="Model name", related="model_id.model")
|
||||
|
||||
domain = fields.Text(default="[]")
|
||||
domain = fields.Text(default="[]", required=True)
|
||||
|
||||
domain_error = fields.Char(compute="_compute_data")
|
||||
|
||||
action_id = fields.Many2one(
|
||||
comodel_name="ir.actions.act_window",
|
||||
@@ -123,13 +125,10 @@ class TileTile(models.Model):
|
||||
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"
|
||||
)
|
||||
hidden = fields.Boolean(compute="_compute_data", search="_search_hidden")
|
||||
|
||||
# Primary Value
|
||||
primary_function = fields.Selection(
|
||||
string="Primary Function",
|
||||
required=True,
|
||||
selection=FIELD_FUNCTION_SELECTION,
|
||||
default="count",
|
||||
@@ -143,24 +142,20 @@ class TileTile(models.Model):
|
||||
)
|
||||
|
||||
primary_format = fields.Char(
|
||||
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.Float(string="Primary Value", compute="_compute_data")
|
||||
primary_value = fields.Float(compute="_compute_data")
|
||||
|
||||
primary_formated_value = fields.Char(
|
||||
string="Primary Formated Value", compute="_compute_data"
|
||||
)
|
||||
primary_formated_value = fields.Char(compute="_compute_data")
|
||||
|
||||
primary_helper = fields.Char(
|
||||
string="Primary Helper", compute="_compute_helper", store=True
|
||||
)
|
||||
primary_helper = fields.Char(compute="_compute_helper", store=True)
|
||||
|
||||
primary_error = fields.Char(compute="_compute_data")
|
||||
|
||||
# Secondary Value
|
||||
secondary_function = fields.Selection(
|
||||
string="Secondary Function",
|
||||
selection=FIELD_FUNCTION_SELECTION,
|
||||
)
|
||||
|
||||
@@ -172,44 +167,61 @@ class TileTile(models.Model):
|
||||
)
|
||||
|
||||
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.Float(string="Secondary Value", compute="_compute_data")
|
||||
secondary_value = fields.Float(compute="_compute_data")
|
||||
|
||||
secondary_formated_value = fields.Char(
|
||||
string="Secondary Formated Value", compute="_compute_data"
|
||||
)
|
||||
secondary_formated_value = fields.Char(compute="_compute_data")
|
||||
|
||||
secondary_helper = fields.Char(
|
||||
string="Secondary Helper", compute="_compute_helper", store=True
|
||||
)
|
||||
secondary_helper = fields.Char(compute="_compute_helper", store=True)
|
||||
|
||||
error = fields.Char(string="Error Details", compute="_compute_data")
|
||||
secondary_error = fields.Char(compute="_compute_data")
|
||||
|
||||
# Compute Section
|
||||
@api.depends("primary_format", "secondary_format", "model_id", "domain")
|
||||
@api.depends(
|
||||
"model_id",
|
||||
"domain",
|
||||
"primary_format",
|
||||
"primary_function",
|
||||
"primary_field_id",
|
||||
"secondary_format",
|
||||
"secondary_function",
|
||||
"secondary_field_id",
|
||||
)
|
||||
def _compute_data(self):
|
||||
for tile in self:
|
||||
# initialize all vals
|
||||
tile.hidden = False
|
||||
tile.primary_value = False
|
||||
tile.primary_formated_value = False
|
||||
tile.secondary_value = False
|
||||
tile.secondary_formated_value = False
|
||||
tile.domain_error = False
|
||||
tile.primary_error = False
|
||||
tile.secondary_error = False
|
||||
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))
|
||||
count = model.search_count(safe_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)
|
||||
tile.primary_formated_value = tile.secondary_formated_value = _(
|
||||
"Domain Error"
|
||||
)
|
||||
tile.domain_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 []
|
||||
fields
|
||||
and model.search_read(safe_eval(domain, eval_context), fields)
|
||||
or []
|
||||
)
|
||||
for f in ["primary_", "secondary_"]:
|
||||
f_function = f + "function"
|
||||
@@ -217,28 +229,25 @@ class TileTile(models.Model):
|
||||
f_format = f + "format"
|
||||
f_value = f + "value"
|
||||
f_formated_value = f + "formated_value"
|
||||
value = 0
|
||||
f_error = f + "error"
|
||||
|
||||
if not tile[f_function]:
|
||||
tile[f_value] = 0.0
|
||||
tile[f_formated_value] = False
|
||||
continue
|
||||
elif tile[f_function] == "count":
|
||||
value = count
|
||||
else:
|
||||
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)
|
||||
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])
|
||||
tile[f_value] = value
|
||||
|
||||
try:
|
||||
tile[f_formated_value] = (tile[f_format] or "{:,}").format(value)
|
||||
except ValueError as e:
|
||||
tile[f_formated_value] = _("Error")
|
||||
tile[f_error] = str(e)
|
||||
|
||||
tile.hidden = tile.hide_if_null and not tile.primary_value
|
||||
|
||||
@api.depends(
|
||||
"primary_function",
|
||||
@@ -254,19 +263,17 @@ class TileTile(models.Model):
|
||||
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
|
||||
help_text = field_func.get("help", False)
|
||||
if help_text and tile[f_function] != "count" and tile[f_field_id]:
|
||||
tile[f_helper] = help_text % tile[f_field_id].field_description
|
||||
else:
|
||||
tile[f_helper] = help_text
|
||||
|
||||
def _compute_active(self):
|
||||
ima = self.env["ir.model.access"]
|
||||
IrModelAccess = self.env["ir.model.access"]
|
||||
for tile in self:
|
||||
if tile.model_id:
|
||||
tile.active = ima.check(tile.model_id.model, "read", False)
|
||||
tile.active = IrModelAccess.check(tile.model_id.model, "read", False)
|
||||
else:
|
||||
tile.active = True
|
||||
|
||||
@@ -288,7 +295,7 @@ class TileTile(models.Model):
|
||||
raise except_orm(
|
||||
_("Unimplemented Feature. Search on Active field disabled.")
|
||||
)
|
||||
ima = self.env["ir.model.access"]
|
||||
IrModelAccess = self.env["ir.model.access"]
|
||||
ids = []
|
||||
cr.execute(
|
||||
"""
|
||||
@@ -298,20 +305,20 @@ class TileTile(models.Model):
|
||||
ON tt.model_id = im.id"""
|
||||
)
|
||||
for result in cr.fetchall():
|
||||
if ima.check(result[1], "read", False) == value:
|
||||
if IrModelAccess.check(result[1], "read", False) == value:
|
||||
ids.append(result[0])
|
||||
return [("id", "in", ids)]
|
||||
|
||||
# Constraints Sections
|
||||
@api.constrains("model_id", "primary_field_id", "secondary_field_id")
|
||||
def _check_model_id_field_id(self):
|
||||
for rec in self:
|
||||
for tile in self:
|
||||
if any(
|
||||
[
|
||||
rec.primary_field_id
|
||||
and rec.primary_field_id.model_id.id != rec.model_id.id,
|
||||
rec.secondary_field_id
|
||||
and rec.secondary_field_id.model_id.id != rec.model_id.id,
|
||||
tile.primary_field_id
|
||||
and tile.primary_field_id.model_id.id != tile.model_id.id,
|
||||
tile.secondary_field_id
|
||||
and tile.secondary_field_id.model_id.id != tile.model_id.id,
|
||||
]
|
||||
):
|
||||
raise ValidationError(
|
||||
@@ -333,13 +340,11 @@ class TileTile(models.Model):
|
||||
self.secondary_field_id = False
|
||||
|
||||
# Action methods
|
||||
@api.multi
|
||||
def open_link(self):
|
||||
if self.action_id:
|
||||
action = self.action_id.read()[0]
|
||||
else:
|
||||
action = {
|
||||
"view_type": "form",
|
||||
"view_mode": "tree",
|
||||
"view_id": False,
|
||||
"res_model": self.model_id.model,
|
||||
@@ -357,27 +362,15 @@ class TileTile(models.Model):
|
||||
)
|
||||
return action
|
||||
|
||||
@api.model
|
||||
def add(self, vals):
|
||||
if "model_id" in vals and not vals["model_id"].isdigit():
|
||||
# need to replace model_name with its id
|
||||
vals["model_id"] = (
|
||||
self.env["ir.model"].search([("model", "=", vals["model_id"])]).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,
|
||||
"context_today": fields.Date.from_string(
|
||||
fields.Date.context_today(self)
|
||||
),
|
||||
"current_date": fields.Date.today(),
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user