2018-08-20 11:29:49 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Part of Flectra. See LICENSE file for full copyright and licensing details.
|
|
|
|
|
|
|
|
from flectra import api, fields, models, _
|
2018-10-15 10:46:49 +02:00
|
|
|
from flectra.exceptions import ValidationError
|
2018-08-20 11:29:49 +02:00
|
|
|
from lxml import etree
|
|
|
|
from flectra.tools.safe_eval import test_python_expr
|
2018-08-24 14:36:32 +02:00
|
|
|
import xml.etree.ElementTree as ET
|
2018-08-20 11:29:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DigestCustomFields(models.TransientModel):
|
|
|
|
_name = 'digest.custom.fields'
|
|
|
|
|
|
|
|
field_name = fields.Char('Field Name', default='x_kpi_', required=True)
|
|
|
|
label_name = fields.Char('Label Name', required=True)
|
2018-08-24 14:36:32 +02:00
|
|
|
new_group_name = fields.Char('Group Name')
|
2018-10-15 10:46:49 +02:00
|
|
|
ttype = fields.Selection([
|
|
|
|
('integer', 'Integer'),
|
|
|
|
('monetary', 'Monetary')],
|
|
|
|
string='Field Type', required=True, default='integer')
|
2018-08-31 14:09:21 +02:00
|
|
|
compute = fields.Text(string='Compute', groups='base.group_system')
|
2018-10-15 10:46:49 +02:00
|
|
|
compute_field_name = fields.Char(
|
|
|
|
compute='_compute_get_field_name', string='Compute Field Name')
|
|
|
|
available_group_name = fields.Selection(
|
|
|
|
'_get_group_name', string='Available Group')
|
|
|
|
position = fields.Selection([
|
|
|
|
('before', 'Before'),
|
|
|
|
('after', 'After'),
|
|
|
|
('inside', 'Inside')],
|
|
|
|
string='Position')
|
2018-08-31 14:09:21 +02:00
|
|
|
model_id = fields.Many2one(
|
|
|
|
'ir.model', string='Model')
|
|
|
|
model_name = fields.Char(related='model_id.model', string='Model Name')
|
|
|
|
model_domain = fields.Char(string='Domain', oldname='domain', default=[])
|
|
|
|
model_real = fields.Char(compute='_compute_model', string='Real Model')
|
|
|
|
related_date_field_id = fields.Many2one('ir.model.fields', 'Date')
|
|
|
|
|
2018-10-15 10:46:49 +02:00
|
|
|
@api.onchange('compute_field_name', 'model_id', 'model_domain',
|
|
|
|
'related_date_field_id')
|
2018-08-31 14:09:21 +02:00
|
|
|
def onchange_compute_field_name(self):
|
|
|
|
data = """for record in self:
|
|
|
|
start, end, company = record._get_kpi_compute_parameters()
|
|
|
|
"""
|
|
|
|
domain = self.model_domain
|
|
|
|
date = self.related_date_field_id
|
2018-10-15 10:46:49 +02:00
|
|
|
domain_values = """[["%s", ">=", start], ["%s", "<", end]]""" % (
|
|
|
|
date.name or '', date.name or '')
|
2018-08-31 14:09:21 +02:00
|
|
|
if domain != '[]':
|
|
|
|
domain += domain_values
|
|
|
|
domain = domain.replace("][", ",")
|
|
|
|
else:
|
|
|
|
domain = domain_values
|
2018-10-15 10:46:49 +02:00
|
|
|
data += "record['%s'] = self.env['%s'].search_count(%s)" % (
|
|
|
|
self.compute_field_name, self.model_real or '', domain) + "\n\n"
|
2018-08-31 14:09:21 +02:00
|
|
|
self.compute = data
|
|
|
|
|
|
|
|
@api.depends('model_id')
|
|
|
|
def _compute_model(self):
|
|
|
|
for record in self:
|
|
|
|
if record.model_id:
|
|
|
|
record.model_real = record.model_name or 'res.users'
|
2018-08-24 14:36:32 +02:00
|
|
|
|
|
|
|
def _get_group_name(self):
|
|
|
|
digest_view_id = self.env.ref('digest.digest_digest_view_form').id
|
2018-10-15 10:46:49 +02:00
|
|
|
view_ids = self.env['ir.ui.view'].search([
|
|
|
|
('inherit_id', 'child_of', digest_view_id)])
|
2018-08-24 14:36:32 +02:00
|
|
|
group_value = {}
|
|
|
|
for view_id in view_ids:
|
|
|
|
root = ET.fromstring(view_id.arch_base)
|
|
|
|
for group_name in root.iter('group'):
|
2018-10-15 10:46:49 +02:00
|
|
|
if group_name.attrib.get('name', False):
|
|
|
|
group_key = \
|
|
|
|
str(view_id.id) + '_' + str(group_name.attrib['name'])
|
|
|
|
if not group_name.attrib.get('string', False):
|
|
|
|
group_string = group_name.attrib['name'][4:].capitalize()
|
|
|
|
else:
|
|
|
|
group_string = group_name.attrib['string']
|
|
|
|
group_value.update({
|
|
|
|
group_key: group_string})
|
2018-08-24 14:36:32 +02:00
|
|
|
return [(x) for x in group_value.items()]
|
2018-08-20 11:29:49 +02:00
|
|
|
|
|
|
|
@api.constrains('compute')
|
|
|
|
def _check_python_code(self):
|
|
|
|
for record in self.sudo().filtered('compute'):
|
|
|
|
msg = test_python_expr(expr=record.compute.strip(), mode="exec")
|
|
|
|
if msg:
|
|
|
|
raise ValidationError(msg)
|
|
|
|
|
|
|
|
@api.depends('field_name')
|
|
|
|
def _compute_get_field_name(self):
|
|
|
|
for record in self:
|
|
|
|
if record.field_name:
|
|
|
|
record.compute_field_name = record.field_name + '_value'
|
|
|
|
|
|
|
|
@api.constrains('field_name')
|
|
|
|
def _check_name(self):
|
|
|
|
for field in self:
|
|
|
|
if not field.field_name.startswith('x_kpi_'):
|
2018-10-15 10:46:49 +02:00
|
|
|
raise ValidationError(_("Custom fields must have a name that "
|
|
|
|
"starts with 'x_kpi_'!"))
|
2018-08-20 11:29:49 +02:00
|
|
|
try:
|
|
|
|
models.check_pg_name(field.field_name)
|
|
|
|
except ValidationError:
|
2018-10-15 10:46:49 +02:00
|
|
|
msg = _("Field names can only contain characters, digits and"
|
|
|
|
" underscores (up to 63).")
|
2018-08-20 11:29:49 +02:00
|
|
|
raise ValidationError(msg)
|
|
|
|
|
|
|
|
def add_new_fields(self):
|
2018-10-15 10:46:49 +02:00
|
|
|
model_id = self.env['ir.model'].search([
|
|
|
|
('model', '=', 'digest.digest')])
|
2018-08-20 11:29:49 +02:00
|
|
|
ir_model_fields_obj = self.env['ir.model.fields']
|
|
|
|
|
|
|
|
first_field_name = self.field_name
|
|
|
|
values = {
|
|
|
|
'model_id': model_id.id,
|
|
|
|
'ttype': 'boolean',
|
|
|
|
'name': first_field_name,
|
|
|
|
'field_description': self.label_name,
|
|
|
|
'model': 'digest.digest'
|
|
|
|
}
|
|
|
|
|
2018-08-31 14:09:21 +02:00
|
|
|
compute_values = {
|
2018-08-20 11:29:49 +02:00
|
|
|
'model_id': model_id.id,
|
|
|
|
'ttype': self.ttype,
|
|
|
|
'name': self.field_name + '_value',
|
|
|
|
'field_description': self.label_name + ' Value',
|
|
|
|
'model': 'digest.digest',
|
|
|
|
'depends': first_field_name,
|
|
|
|
'compute': self.compute
|
|
|
|
}
|
2018-08-31 14:09:21 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
ir_model_fields_obj.create(values)
|
|
|
|
ir_model_fields_obj.create(compute_values)
|
|
|
|
except Exception as e:
|
|
|
|
raise ValidationError(e)
|
2018-08-20 11:29:49 +02:00
|
|
|
|
|
|
|
def field_arch(self):
|
|
|
|
xpath = etree.Element('xpath')
|
2018-10-15 10:46:49 +02:00
|
|
|
name = self.available_group_name and self.available_group_name.split(
|
|
|
|
'_', 1)[1] or "kpis"
|
2018-08-24 14:36:32 +02:00
|
|
|
expr = '//' + 'group' + '[@name="' + name + '"]'
|
2018-08-20 11:29:49 +02:00
|
|
|
xpath.set('expr', expr)
|
2018-08-24 14:36:32 +02:00
|
|
|
xpath.set('position', self.position)
|
2018-08-31 14:09:21 +02:00
|
|
|
field = etree.Element('field')
|
|
|
|
field.set('name', self.field_name)
|
|
|
|
xpath.set('expr', expr)
|
2018-08-24 14:36:32 +02:00
|
|
|
|
|
|
|
if self.position == 'inside':
|
|
|
|
xpath.append(field)
|
|
|
|
else:
|
2018-08-20 11:29:49 +02:00
|
|
|
group = etree.Element('group')
|
2018-08-24 14:36:32 +02:00
|
|
|
group.set('name', 'x_kpi_' + self.new_group_name.replace(" ", "_"))
|
|
|
|
group.set('string', self.new_group_name)
|
2018-08-20 11:29:49 +02:00
|
|
|
group.append(field)
|
|
|
|
xpath.append(group)
|
2018-08-24 14:36:32 +02:00
|
|
|
|
2018-08-20 11:29:49 +02:00
|
|
|
return etree.tostring(xpath).decode("utf-8")
|
|
|
|
|
|
|
|
@api.multi
|
2018-08-24 14:36:32 +02:00
|
|
|
def action_add_customize_digest(self):
|
2018-08-20 11:29:49 +02:00
|
|
|
self.add_new_fields()
|
2018-08-24 14:36:32 +02:00
|
|
|
arch = '<?xml version="1.0"?>' + str(self.field_arch())
|
2018-10-15 10:46:49 +02:00
|
|
|
view_id = self.available_group_name and \
|
|
|
|
self.available_group_name.split('_', 1)[0] or False
|
2018-08-20 11:29:49 +02:00
|
|
|
vals = {
|
|
|
|
'type': 'form',
|
|
|
|
'model': 'digest.digest',
|
2018-10-15 10:46:49 +02:00
|
|
|
'inherit_id':
|
|
|
|
view_id or self.env.ref('digest.digest_digest_view_form').id,
|
2018-08-20 11:29:49 +02:00
|
|
|
'mode': 'extension',
|
|
|
|
'arch_base': arch,
|
2018-08-24 14:36:32 +02:00
|
|
|
'name': 'x_kpi_' + self.field_name + "_customization",
|
2018-08-20 11:29:49 +02:00
|
|
|
}
|
2018-10-15 10:46:49 +02:00
|
|
|
ir_model = self.env['ir.model'].search([
|
|
|
|
('model', '=', 'digest.digest')])
|
2018-08-20 11:29:49 +02:00
|
|
|
if hasattr(ir_model, 'module_id'):
|
|
|
|
vals.update({'module_id': ir_model.module_id.id})
|
|
|
|
self.env['ir.ui.view'].sudo().create(vals)
|
|
|
|
return {
|
|
|
|
'type': 'ir.actions.client',
|
|
|
|
'tag': 'reload',
|
|
|
|
}
|