mirror of
https://github.com/OCA/reporting-engine.git
synced 2025-02-16 16:30:38 +02:00
Initial commit bi_view_editor V9
This commit is contained in:
6
bi_view_editor/models/__init__.py
Normal file
6
bi_view_editor/models/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2015-2017 Onestein (<http://www.onestein.eu>)
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
|
||||
from . import bve_view
|
||||
from . import ir_model
|
||||
354
bi_view_editor/models/bve_view.py
Normal file
354
bi_view_editor/models/bve_view.py
Normal file
@@ -0,0 +1,354 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2015-2017 Onestein (<http://www.onestein.eu>)
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
|
||||
import json
|
||||
|
||||
from openerp import api, fields, models, tools
|
||||
from openerp.exceptions import Warning as UserError
|
||||
from openerp.tools.translate import _
|
||||
|
||||
|
||||
class BveView(models.Model):
|
||||
_name = 'bve.view'
|
||||
_description = 'BI View Editor'
|
||||
|
||||
@api.depends('group_ids')
|
||||
@api.multi
|
||||
def _compute_users(self):
|
||||
for bve_view in self:
|
||||
group_ids = bve_view.sudo().group_ids
|
||||
if group_ids:
|
||||
bve_view.user_ids = group_ids.mapped('users')
|
||||
else:
|
||||
bve_view.user_ids = self.env['res.users'].sudo().search([])
|
||||
|
||||
name = fields.Char(required=True, copy=False)
|
||||
model_name = fields.Char()
|
||||
|
||||
note = fields.Text(string='Notes')
|
||||
|
||||
state = fields.Selection(
|
||||
[('draft', 'Draft'),
|
||||
('created', 'Created')],
|
||||
default='draft',
|
||||
copy=False)
|
||||
data = fields.Text(
|
||||
help="Use the special query builder to define the query "
|
||||
"to generate your report dataset. "
|
||||
"NOTE: Te be edited, the query should be in 'Draft' status.")
|
||||
|
||||
action_id = fields.Many2one('ir.actions.act_window', string='Action')
|
||||
view_id = fields.Many2one('ir.ui.view', string='View')
|
||||
|
||||
group_ids = fields.Many2many(
|
||||
'res.groups',
|
||||
string='Groups',
|
||||
help="User groups allowed to see the generated report; "
|
||||
"if NO groups are specified the report will be public "
|
||||
"for everyone.")
|
||||
|
||||
user_ids = fields.Many2many(
|
||||
'res.users',
|
||||
string='Users',
|
||||
compute=_compute_users,
|
||||
store=True)
|
||||
|
||||
_sql_constraints = [
|
||||
('name_uniq',
|
||||
'unique(name)',
|
||||
_('Custom BI View names must be unique!')),
|
||||
]
|
||||
|
||||
@api.multi
|
||||
def unlink(self):
|
||||
for view in self:
|
||||
if view.state == 'created':
|
||||
raise UserError(
|
||||
_('You cannot delete a created view! '
|
||||
'Reset the view to draft first.'))
|
||||
return super(BveView, self).unlink()
|
||||
|
||||
@api.multi
|
||||
def action_reset(self):
|
||||
self.ensure_one()
|
||||
if self.action_id:
|
||||
if self.action_id.view_id:
|
||||
self.action_id.view_id.sudo().unlink()
|
||||
self.action_id.sudo().unlink()
|
||||
|
||||
models = self.env['ir.model'].sudo().search(
|
||||
[('model', '=', self.model_name)])
|
||||
for model in models:
|
||||
model.sudo().unlink()
|
||||
|
||||
table_name = self.model_name.replace('.', '_')
|
||||
tools.drop_view_if_exists(self.env.cr, table_name)
|
||||
|
||||
self.state = 'draft'
|
||||
|
||||
@api.multi
|
||||
def _create_view_arch(self):
|
||||
self.ensure_one()
|
||||
|
||||
def _get_field_def(field_name, def_type):
|
||||
return """<field name="x_{}" type="{}" />""".format(
|
||||
field_name, def_type
|
||||
)
|
||||
|
||||
def _get_field_type(field_info):
|
||||
row = field_info['row'] and 'row'
|
||||
column = field_info['column'] and 'col'
|
||||
measure = field_info['measure'] and 'measure'
|
||||
return row or column or measure
|
||||
|
||||
fields_info = json.loads(self._get_format_data(self.data))
|
||||
view_fields = []
|
||||
for field_info in fields_info:
|
||||
field_name = field_info['name']
|
||||
def_type = _get_field_type(field_info)
|
||||
if def_type:
|
||||
field_def = _get_field_def(field_name, def_type)
|
||||
view_fields.append(field_def)
|
||||
return view_fields
|
||||
|
||||
@api.model
|
||||
def _get_format_data(self, data):
|
||||
data = data.replace('\'', '"')
|
||||
data = data.replace(': u"', ':"')
|
||||
return data
|
||||
|
||||
@api.multi
|
||||
def action_create(self):
|
||||
self.ensure_one()
|
||||
|
||||
self._create_bve_object()
|
||||
self._create_bve_view()
|
||||
|
||||
@api.multi
|
||||
def _create_bve_view(self):
|
||||
self.ensure_one()
|
||||
|
||||
# create views
|
||||
View = self.env['ir.ui.view']
|
||||
old_views = View.sudo().search([('model', '=', self.model_name)])
|
||||
old_views.sudo().unlink()
|
||||
|
||||
view_vals = [{
|
||||
'name': 'Pivot Analysis',
|
||||
'type': 'pivot',
|
||||
'model': self.model_name,
|
||||
'priority': 16,
|
||||
'arch': """<?xml version="1.0"?>
|
||||
<pivot string="Pivot Analysis"> {} </pivot>
|
||||
""".format("".join(self._create_view_arch()))
|
||||
}, {
|
||||
'name': 'Graph Analysis',
|
||||
'type': 'graph',
|
||||
'model': self.model_name,
|
||||
'priority': 16,
|
||||
'arch': """<?xml version="1.0"?>
|
||||
<graph string="Graph Analysis"
|
||||
type="bar"
|
||||
stacked="True"> {} </graph>
|
||||
""".format("".join(self._create_view_arch()))
|
||||
}]
|
||||
|
||||
for vals in view_vals:
|
||||
View.sudo().create(vals)
|
||||
|
||||
# create Tree view
|
||||
tree_view = View.sudo().create(
|
||||
{'name': 'Tree Analysis',
|
||||
'type': 'tree',
|
||||
'model': self.model_name,
|
||||
'priority': 16,
|
||||
'arch': """<?xml version="1.0"?>
|
||||
<tree string="List Analysis" create="false"> {} </tree>
|
||||
""".format("".join(self._create_view_arch()))
|
||||
})
|
||||
|
||||
# set the Tree view as the default one
|
||||
action_vals = {
|
||||
'name': self.name,
|
||||
'res_model': self.model_name,
|
||||
'type': 'ir.actions.act_window',
|
||||
'view_type': 'form',
|
||||
'view_mode': 'tree,graph,pivot',
|
||||
'view_id': tree_view.id,
|
||||
'context': "{'service_name': '%s'}" % self.name,
|
||||
}
|
||||
|
||||
ActWindow = self.env['ir.actions.act_window']
|
||||
action_id = ActWindow.sudo().create(action_vals)
|
||||
self.write({
|
||||
'action_id': action_id.id,
|
||||
'view_id': tree_view.id,
|
||||
'state': 'created'
|
||||
})
|
||||
|
||||
@api.multi
|
||||
def _create_bve_object(self):
|
||||
self.ensure_one()
|
||||
|
||||
def _get_fields_info(fields_data):
|
||||
fields_info = []
|
||||
for field_data in fields_data:
|
||||
field = self.env['ir.model.fields'].browse(field_data['id'])
|
||||
vals = {
|
||||
'table': self.env[field.model_id.model]._table,
|
||||
'table_alias': field_data['table_alias'],
|
||||
'select_field': field.name,
|
||||
'as_field': 'x_' + field_data['name'],
|
||||
'join': False,
|
||||
'model': field.model_id.model
|
||||
}
|
||||
if field_data.get('join_node'):
|
||||
vals.update({'join': field_data['join_node']})
|
||||
fields_info.append(vals)
|
||||
return fields_info
|
||||
|
||||
def _build_query():
|
||||
data = self.data
|
||||
if not data:
|
||||
raise UserError(_('No data to process.'))
|
||||
|
||||
formatted_data = json.loads(self._get_format_data(data))
|
||||
info = _get_fields_info(formatted_data)
|
||||
fields = [("{}.{}".format(f['table_alias'],
|
||||
f['select_field']),
|
||||
f['as_field']) for f in info if 'join_node' not in f]
|
||||
tables = set([(f['table'], f['table_alias']) for f in info])
|
||||
join_nodes = [
|
||||
(f['table_alias'],
|
||||
f['join'],
|
||||
f['select_field']) for f in info if f['join'] is not False]
|
||||
|
||||
table_name = self.model_name.replace('.', '_')
|
||||
tools.drop_view_if_exists(self.env.cr, table_name)
|
||||
|
||||
basic_fields = [
|
||||
("t0.id", "id"),
|
||||
("t0.write_uid", "write_uid"),
|
||||
("t0.write_date", "write_date"),
|
||||
("t0.create_uid", "create_uid"),
|
||||
("t0.create_date", "create_date")
|
||||
]
|
||||
|
||||
q = """CREATE or REPLACE VIEW %s as (
|
||||
SELECT %s
|
||||
FROM %s
|
||||
WHERE %s
|
||||
)""" % (table_name, ','.join(
|
||||
["{} AS {}".format(f[0], f[1])
|
||||
for f in basic_fields + fields]), ','.join(
|
||||
["{} AS {}".format(t[0], t[1])
|
||||
for t in list(tables)]), " AND ".join(
|
||||
["{}.{} = {}.id".format(j[0], j[2], j[1])
|
||||
for j in join_nodes] + ["TRUE"]))
|
||||
|
||||
self.env.cr.execute(q)
|
||||
|
||||
def _prepare_field(field_data):
|
||||
if not field_data['custom']:
|
||||
field = self.env['ir.model.fields'].browse(field_data['id'])
|
||||
vals = {
|
||||
'name': 'x_' + field_data['name'],
|
||||
'complete_name': field.complete_name,
|
||||
'model': self.model_name,
|
||||
'relation': field.relation,
|
||||
'field_description': field_data.get(
|
||||
'description', field.field_description),
|
||||
'ttype': field.ttype,
|
||||
'selection': field.selection,
|
||||
'size': field.size,
|
||||
'state': 'manual'
|
||||
}
|
||||
if vals['ttype'] == 'monetary':
|
||||
vals.update({'ttype': 'float'})
|
||||
if field.ttype == 'selection' and not field.selection:
|
||||
model_obj = self.env[field.model_id.model]
|
||||
selection = model_obj._columns[field.name].selection
|
||||
selection_domain = str(selection)
|
||||
vals.update({'selection': selection_domain})
|
||||
return vals
|
||||
|
||||
def _prepare_object():
|
||||
data = json.loads(self._get_format_data(self.data))
|
||||
return {
|
||||
'name': self.name,
|
||||
'model': self.model_name,
|
||||
'field_id': [
|
||||
(0, 0, _prepare_field(field))
|
||||
for field in data
|
||||
if 'join_node' not in field]
|
||||
}
|
||||
|
||||
def _build_object():
|
||||
vals = _prepare_object()
|
||||
Model = self.env['ir.model']
|
||||
res_id = Model.sudo().with_context(bve=True).create(vals)
|
||||
return res_id
|
||||
|
||||
def group_ids_with_access(model_name, access_mode):
|
||||
self.env.cr.execute('''SELECT
|
||||
g.id
|
||||
FROM
|
||||
ir_model_access a
|
||||
JOIN ir_model m ON (a.model_id=m.id)
|
||||
JOIN res_groups g ON (a.group_id=g.id)
|
||||
LEFT JOIN ir_module_category c ON (c.id=g.category_id)
|
||||
WHERE
|
||||
m.model=%s AND
|
||||
a.active IS True AND
|
||||
a.perm_''' + access_mode, (model_name,))
|
||||
return [x[0] for x in self.env.cr.fetchall()]
|
||||
|
||||
def _build_access_rules(obj):
|
||||
info = json.loads(self._get_format_data(self.data))
|
||||
models = list(set([f['model'] for f in info]))
|
||||
read_groups = set.intersection(*[set(
|
||||
group_ids_with_access(model, 'read')) for model in models])
|
||||
|
||||
# read access
|
||||
for group in read_groups:
|
||||
self.env['ir.model.access'].sudo().create({
|
||||
'name': 'read access to ' + self.model_name,
|
||||
'model_id': obj.id,
|
||||
'group_id': group,
|
||||
'perm_read': True,
|
||||
})
|
||||
|
||||
# read and write access
|
||||
for group in self.group_ids:
|
||||
self.env['ir.model.access'].sudo().create({
|
||||
'name': 'read-write access to ' + self.model_name,
|
||||
'model_id': obj.id,
|
||||
'group_id': group.id,
|
||||
'perm_read': True,
|
||||
'perm_write': True,
|
||||
})
|
||||
|
||||
self.model_name = 'x_bve.' + ''.join(
|
||||
[x for x in self.name.lower()
|
||||
if x.isalnum()]).replace('_', '.').replace(' ', '.')
|
||||
_build_query()
|
||||
obj = _build_object()
|
||||
_build_access_rules(obj)
|
||||
|
||||
@api.multi
|
||||
def open_view(self):
|
||||
self.ensure_one()
|
||||
return {
|
||||
'name': _('BI View'),
|
||||
'type': 'ir.actions.act_window',
|
||||
'res_model': self.model_name,
|
||||
'view_type': 'form',
|
||||
'view_mode': 'tree,graph,pivot',
|
||||
}
|
||||
|
||||
@api.multi
|
||||
def copy(self, default=None):
|
||||
self.ensure_one()
|
||||
default = dict(default or {}, name=_("%s (copy)") % self.name)
|
||||
return super(BveView, self).copy(default=default)
|
||||
327
bi_view_editor/models/ir_model.py
Normal file
327
bi_view_editor/models/ir_model.py
Normal file
@@ -0,0 +1,327 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2015-2017 Onestein (<http://www.onestein.eu>)
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
|
||||
from openerp import api, models
|
||||
from openerp.modules.registry import RegistryManager
|
||||
|
||||
NO_BI_MODELS = [
|
||||
'temp.range',
|
||||
'account.statement.operation.template',
|
||||
'fetchmail.server'
|
||||
]
|
||||
|
||||
NO_BI_FIELDS = [
|
||||
'id',
|
||||
'create_uid',
|
||||
'create_date',
|
||||
'write_uid',
|
||||
'write_date'
|
||||
]
|
||||
|
||||
NO_BI_TTYPES = [
|
||||
'many2many',
|
||||
'one2many',
|
||||
'html',
|
||||
'binary',
|
||||
'reference'
|
||||
]
|
||||
|
||||
|
||||
def dict_for_field(field):
|
||||
return {
|
||||
'id': field.id,
|
||||
'name': field.name,
|
||||
'description': field.field_description,
|
||||
'type': field.ttype,
|
||||
'relation': field.relation,
|
||||
'custom': False,
|
||||
'model_id': field.model_id.id,
|
||||
'model': field.model_id.model,
|
||||
'model_name': field.model_id.name
|
||||
}
|
||||
|
||||
|
||||
class IrModel(models.Model):
|
||||
_inherit = 'ir.model'
|
||||
|
||||
@api.model
|
||||
def _filter_bi_fields(self, ir_model_field_obj):
|
||||
name = ir_model_field_obj.name
|
||||
model = ir_model_field_obj.model_id
|
||||
model_name = model.model
|
||||
Model = self.env[model_name]
|
||||
if name in Model._columns:
|
||||
f = Model._columns[name]
|
||||
return f._classic_write
|
||||
return False
|
||||
|
||||
@api.model
|
||||
def _filter_bi_models(self, model):
|
||||
|
||||
def _check_name(model_model):
|
||||
if model_model in NO_BI_MODELS:
|
||||
return 1
|
||||
return 0
|
||||
|
||||
def _check_startswith(model_model):
|
||||
if model_model.startswith('workflow') or \
|
||||
model_model.startswith('ir.') or \
|
||||
model_model.startswith('base_'):
|
||||
return 1
|
||||
return 0
|
||||
|
||||
def _check_contains(model_model):
|
||||
if 'mail' in model_model or \
|
||||
'_' in model_model or \
|
||||
'report' in model_model or \
|
||||
'edi.' in model_model:
|
||||
return 1
|
||||
return 0
|
||||
|
||||
def _check_unknow(model_name):
|
||||
if model_name == 'Unknow' or '.' in model_name:
|
||||
return 1
|
||||
return 0
|
||||
|
||||
model_model = model['model']
|
||||
model_name = model['name']
|
||||
count_check = 0
|
||||
count_check += _check_name(model_model)
|
||||
count_check += _check_startswith(model_model)
|
||||
count_check += _check_contains(model_model)
|
||||
count_check += _check_unknow(model_name)
|
||||
if not count_check:
|
||||
return self.env['ir.model.access'].check(
|
||||
model['model'], 'read', False)
|
||||
return False
|
||||
|
||||
@api.model
|
||||
def get_related_fields(self, model_ids):
|
||||
""" Return list of field dicts for all fields that can be
|
||||
joined with models in model_ids
|
||||
"""
|
||||
Model = self.env['ir.model']
|
||||
domain = [('id', 'in', model_ids.values())]
|
||||
models = Model.sudo().search(domain)
|
||||
model_names = {}
|
||||
for model in models:
|
||||
model_names.update({model.id: model.model})
|
||||
|
||||
related_fields = self._get_related_fields_list(model_ids, model_names)
|
||||
return related_fields
|
||||
|
||||
@api.model
|
||||
def _get_related_fields_list(self, model_ids, model_names):
|
||||
|
||||
def _get_right_fields(model_ids, model_names):
|
||||
Fields = self.env['ir.model.fields']
|
||||
rfields = []
|
||||
domain = [('model_id', 'in', model_ids.values()),
|
||||
('ttype', 'in', ['many2one'])]
|
||||
for field in filter(
|
||||
self._filter_bi_fields,
|
||||
Fields.sudo().search(domain)):
|
||||
for model in model_ids.items():
|
||||
if model[1] == field.model_id.id:
|
||||
rfields.append(
|
||||
dict(dict_for_field(field),
|
||||
join_node=-1,
|
||||
table_alias=model[0])
|
||||
)
|
||||
return rfields
|
||||
|
||||
def _get_left_fields(model_ids, model_names):
|
||||
Fields = self.env['ir.model.fields']
|
||||
lfields = []
|
||||
domain = [('relation', 'in', model_names.values()),
|
||||
('ttype', 'in', ['many2one'])]
|
||||
for field in filter(
|
||||
self._filter_bi_fields,
|
||||
Fields.sudo().search(domain)):
|
||||
for model in model_ids.items():
|
||||
if model_names[model[1]] == field['relation']:
|
||||
lfields.append(
|
||||
dict(dict_for_field(field),
|
||||
join_node=model[0],
|
||||
table_alias=-1)
|
||||
)
|
||||
return lfields
|
||||
|
||||
def _get_relation_list(model_ids, model_names, lfields):
|
||||
relation_list = []
|
||||
for model in model_ids.items():
|
||||
for field in lfields:
|
||||
if model_names[model[1]] == field['relation']:
|
||||
relation_list.append(
|
||||
dict(field, join_node=model[0])
|
||||
)
|
||||
return relation_list
|
||||
|
||||
def _get_model_list(model_ids, rfields):
|
||||
model_list = []
|
||||
for model in model_ids.items():
|
||||
for field in rfields:
|
||||
if model[1] == field['model_id']:
|
||||
model_list.append(
|
||||
dict(field, table_alias=model[0])
|
||||
)
|
||||
return model_list
|
||||
|
||||
lfields = _get_left_fields(model_ids, model_names)
|
||||
rfields = _get_right_fields(model_ids, model_names)
|
||||
|
||||
relation_list = _get_relation_list(model_ids, model_names, lfields)
|
||||
model_list = _get_model_list(model_ids, rfields)
|
||||
|
||||
related_fields = relation_list + model_list
|
||||
return related_fields
|
||||
|
||||
@api.model
|
||||
def get_related_models(self, model_ids):
|
||||
""" Return list of model dicts for all models that can be
|
||||
joined with models in model_ids
|
||||
"""
|
||||
def _get_field(fields, orig, target):
|
||||
field_list = []
|
||||
for f in fields:
|
||||
if f[orig] == -1:
|
||||
field_list.append(f[target])
|
||||
return field_list
|
||||
|
||||
def _get_list_id(model_ids, fields):
|
||||
list_model = model_ids.values()
|
||||
list_model += _get_field(fields, 'table_alias', 'model_id')
|
||||
return list_model
|
||||
|
||||
def _get_list_relation(fields):
|
||||
list_model = _get_field(fields, 'join_node', 'relation')
|
||||
return list_model
|
||||
|
||||
models_list = []
|
||||
related_fields = self.get_related_fields(model_ids)
|
||||
list_id = _get_list_id(model_ids, related_fields)
|
||||
list_model = _get_list_relation(related_fields)
|
||||
domain = ['|',
|
||||
('id', 'in', list_id),
|
||||
('model', 'in', list_model)]
|
||||
models = self.env['ir.model'].sudo().search(domain)
|
||||
for model in models:
|
||||
models_list.append({
|
||||
'id': model.id,
|
||||
'name': model.name,
|
||||
'model': model.model
|
||||
})
|
||||
return sorted(
|
||||
filter(self._filter_bi_models, models_list),
|
||||
key=lambda x: x['name']
|
||||
)
|
||||
|
||||
@api.model
|
||||
def get_models(self):
|
||||
""" Return list of model dicts for all available models.
|
||||
"""
|
||||
def dict_for_model(model):
|
||||
return {
|
||||
'id': model.id,
|
||||
'name': model.name,
|
||||
'model': model.model
|
||||
}
|
||||
|
||||
models_domain = [('transient', '=', False)]
|
||||
return sorted(filter(
|
||||
self._filter_bi_models,
|
||||
[dict_for_model(model)
|
||||
for model in self.search(models_domain)]),
|
||||
key=lambda x: x['name'])
|
||||
|
||||
@api.model
|
||||
def get_join_nodes(self, field_data, new_field):
|
||||
""" Return list of field dicts of join nodes
|
||||
|
||||
Return all possible join nodes to add new_field to the query
|
||||
containing model_ids.
|
||||
"""
|
||||
def _get_model_ids(field_data):
|
||||
model_ids = dict([(field['table_alias'],
|
||||
field['model_id']) for field in field_data])
|
||||
return model_ids
|
||||
|
||||
def _get_join_nodes_dict(model_ids, new_field):
|
||||
join_nodes = []
|
||||
for alias, model_id in model_ids.items():
|
||||
if model_id == new_field['model_id']:
|
||||
join_nodes.append({'table_alias': alias})
|
||||
for dict_field in self.get_related_fields(model_ids):
|
||||
condition = [
|
||||
dict_field['join_node'] == -1,
|
||||
dict_field['table_alias'] == -1
|
||||
]
|
||||
relation = (new_field['model'] == dict_field['relation'])
|
||||
model = (new_field['model_id'] == dict_field['model_id'])
|
||||
if (relation and condition[0]) or (model and condition[1]):
|
||||
join_nodes.append(dict_field)
|
||||
return join_nodes
|
||||
|
||||
model_ids = _get_model_ids(field_data)
|
||||
keys = [(field['table_alias'], field['id'])
|
||||
for field in field_data if field.get('join_node', -1) != -1]
|
||||
join_nodes = _get_join_nodes_dict(model_ids, new_field)
|
||||
return filter(
|
||||
lambda x: 'id' not in x or
|
||||
(x['table_alias'], x['id']) not in keys, join_nodes)
|
||||
|
||||
@api.model
|
||||
def get_fields(self, model_id):
|
||||
bi_field_domain = [
|
||||
('model_id', '=', model_id),
|
||||
('name', 'not in', NO_BI_FIELDS),
|
||||
('ttype', 'not in', NO_BI_TTYPES)
|
||||
]
|
||||
Fields = self.env['ir.model.fields']
|
||||
fields = filter(
|
||||
self._filter_bi_fields,
|
||||
Fields.sudo().search(bi_field_domain)
|
||||
)
|
||||
fields_dict = []
|
||||
for field in fields:
|
||||
fields_dict.append(
|
||||
{'id': field.id,
|
||||
'model_id': model_id,
|
||||
'name': field.name,
|
||||
'description': field.field_description,
|
||||
'type': field.ttype,
|
||||
'custom': False,
|
||||
'model': field.model_id.model,
|
||||
'model_name': field.model_id.name
|
||||
}
|
||||
)
|
||||
sorted_fields = sorted(
|
||||
fields_dict,
|
||||
key=lambda x: x['description'],
|
||||
reverse=True
|
||||
)
|
||||
return sorted_fields
|
||||
|
||||
@api.model
|
||||
def create(self, vals):
|
||||
if self._context and self._context.get('bve'):
|
||||
vals['state'] = 'base'
|
||||
res = super(IrModel, self).create(vals)
|
||||
|
||||
# this sql update is necessary since a write method here would
|
||||
# be not working (an orm constraint is restricting the modification
|
||||
# of the state field while updating ir.model)
|
||||
q = ("""UPDATE ir_model SET state = 'manual'
|
||||
WHERE id = """ + str(res.id))
|
||||
self.env.cr.execute(q)
|
||||
|
||||
# update registry
|
||||
if self._context.get('bve'):
|
||||
# setup models; this reloads custom models in registry
|
||||
self.pool.setup_models(self._cr, partial=(not self.pool.ready))
|
||||
|
||||
# signal that registry has changed
|
||||
RegistryManager.signal_registry_change(self.env.cr.dbname)
|
||||
|
||||
return res
|
||||
Reference in New Issue
Block a user