mirror of
https://github.com/OCA/reporting-engine.git
synced 2025-02-16 16:30:38 +02:00
[MIG] bi_view_editor: Migration to 11.0
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2015-2017 Onestein (<http://www.onestein.eu>)
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
|
||||
from . import models
|
||||
from . import bve_view
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2015-2017 Onestein (<http://www.onestein.eu>)
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
# Copyright 2015-2018 Onestein (<http://www.onestein.eu>)
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
|
||||
import json
|
||||
|
||||
@@ -130,7 +129,7 @@ class BveView(models.Model):
|
||||
# create views
|
||||
View = self.env['ir.ui.view']
|
||||
old_views = View.sudo().search([('model', '=', self.model_name)])
|
||||
old_views.sudo().unlink()
|
||||
old_views.unlink()
|
||||
|
||||
view_vals = [{
|
||||
'name': 'Pivot Analysis',
|
||||
@@ -205,18 +204,19 @@ class BveView(models.Model):
|
||||
self.ensure_one()
|
||||
|
||||
def group_ids_with_access(model_name, access_mode):
|
||||
# pylint: disable=sql-injection
|
||||
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.active = true AND
|
||||
a.perm_''' + access_mode, (model_name,))
|
||||
return [x[0] for x in self.env.cr.fetchall()]
|
||||
res = self.env.cr.fetchall()
|
||||
return [x[0] for x in res]
|
||||
|
||||
info = json.loads(self._get_format_data(self.data))
|
||||
model_names = list(set([f['model'] for f in info]))
|
||||
@@ -224,6 +224,10 @@ class BveView(models.Model):
|
||||
group_ids_with_access(model_name, 'read')
|
||||
) for model_name in model_names])
|
||||
|
||||
if not read_groups and not self.group_ids:
|
||||
raise UserError(_('Please select at least one group'
|
||||
' on the security tab.'))
|
||||
|
||||
# read access
|
||||
for group in read_groups:
|
||||
self.env['ir.model.access'].sudo().create({
|
||||
@@ -294,12 +298,13 @@ class BveView(models.Model):
|
||||
table_name = self.model_name.replace('.', '_')
|
||||
|
||||
# robustness in case something went wrong
|
||||
# pylint: disable=sql-injection
|
||||
self._cr.execute('DROP TABLE IF EXISTS "%s"' % table_name)
|
||||
|
||||
basic_fields = [
|
||||
("t0.id", "id")
|
||||
]
|
||||
|
||||
# pylint: disable=sql-injection
|
||||
q = """CREATE or REPLACE VIEW %s as (
|
||||
SELECT %s
|
||||
FROM %s
|
||||
@@ -314,6 +319,35 @@ class BveView(models.Model):
|
||||
|
||||
self.env.cr.execute(q)
|
||||
|
||||
@api.multi
|
||||
def action_translations(self):
|
||||
self.ensure_one()
|
||||
model = self.env['ir.model'].sudo().search([
|
||||
('model', '=', self.model_name)
|
||||
])
|
||||
translation_obj = self.env['ir.translation'].sudo()
|
||||
translation_obj.translate_fields('ir.model', model.id)
|
||||
for field_id in model.field_id.ids:
|
||||
translation_obj.translate_fields('ir.model.fields', field_id)
|
||||
return {
|
||||
'name': 'Translations',
|
||||
'res_model': 'ir.translation',
|
||||
'type': 'ir.actions.act_window',
|
||||
'view_mode': 'tree',
|
||||
'view_id': self.env.ref('base.view_translation_dialog_tree').id,
|
||||
'target': 'current',
|
||||
'flags': {'search_view': True, 'action_buttons': True},
|
||||
'domain': [
|
||||
'|',
|
||||
'&',
|
||||
('res_id', 'in', model.field_id.ids),
|
||||
('name', '=', 'ir.model.fields,field_description'),
|
||||
'&',
|
||||
('res_id', '=', model.id),
|
||||
('name', '=', 'ir.model,name')
|
||||
],
|
||||
}
|
||||
|
||||
@api.multi
|
||||
def action_create(self):
|
||||
self.ensure_one()
|
||||
@@ -331,7 +365,8 @@ class BveView(models.Model):
|
||||
'ttype': field.ttype,
|
||||
'selection': field.selection,
|
||||
'size': field.size,
|
||||
'state': 'manual'
|
||||
'state': 'manual',
|
||||
'readonly': True
|
||||
}
|
||||
if vals['ttype'] == 'monetary':
|
||||
vals.update({'ttype': 'float'})
|
||||
@@ -391,11 +426,11 @@ class BveView(models.Model):
|
||||
has_menus = False
|
||||
if self.action_id:
|
||||
action = 'ir.actions.act_window,%d' % (self.action_id.id,)
|
||||
menus = self.env['ir.ui.menu'].sudo().search(
|
||||
[('action', '=', action)]
|
||||
)
|
||||
menus = self.env['ir.ui.menu'].sudo().search([
|
||||
('action', '=', action)
|
||||
])
|
||||
has_menus = True if menus else False
|
||||
menus.sudo().unlink()
|
||||
menus.unlink()
|
||||
|
||||
if self.action_id.view_id:
|
||||
self.action_id.view_id.sudo().unlink()
|
||||
@@ -403,10 +438,11 @@ class BveView(models.Model):
|
||||
|
||||
self.env['ir.ui.view'].sudo().search(
|
||||
[('model', '=', self.model_name)]).unlink()
|
||||
ir_models = self.env['ir.model'].sudo().search(
|
||||
[('model', '=', self.model_name)])
|
||||
ir_models = self.env['ir.model'].sudo().search([
|
||||
('model', '=', self.model_name)
|
||||
])
|
||||
for model in ir_models:
|
||||
model.sudo().unlink()
|
||||
model.unlink()
|
||||
|
||||
table_name = self.model_name.replace('.', '_')
|
||||
tools.drop_view_if_exists(self.env.cr, table_name)
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2015-2017 Onestein (<http://www.onestein.eu>)
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
# Copyright 2015-2018 Onestein (<http://www.onestein.eu>)
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import api, models
|
||||
from odoo import api, models, registry
|
||||
|
||||
NO_BI_MODELS = [
|
||||
'temp.range',
|
||||
@@ -112,7 +111,7 @@ class IrModel(models.Model):
|
||||
|
||||
def get_model_list(model_ids):
|
||||
model_list = []
|
||||
domain = [('model_id', 'in', model_ids.values()),
|
||||
domain = [('model_id', 'in', list(model_ids.values())),
|
||||
('store', '=', True),
|
||||
('ttype', 'in', ['many2one'])]
|
||||
filtered_fields = self._search_fields(domain)
|
||||
@@ -128,7 +127,7 @@ class IrModel(models.Model):
|
||||
|
||||
def get_relation_list(model_ids, model_names):
|
||||
relation_list = []
|
||||
domain = [('relation', 'in', model_names.values()),
|
||||
domain = [('relation', 'in', list(model_names.values())),
|
||||
('store', '=', True),
|
||||
('ttype', 'in', ['many2one'])]
|
||||
filtered_fields = self._search_fields(domain)
|
||||
@@ -166,7 +165,7 @@ class IrModel(models.Model):
|
||||
return field_list
|
||||
|
||||
def _get_list_id(model_ids, fields):
|
||||
list_model = model_ids.values()
|
||||
list_model = list(model_ids.values())
|
||||
list_model += _get_field(fields, 'table_alias', 'model_id')
|
||||
return list_model
|
||||
|
||||
@@ -236,9 +235,9 @@ class IrModel(models.Model):
|
||||
join_nodes = _get_join_nodes_dict(model_ids, new_field)
|
||||
join_nodes = remove_duplicate_nodes(join_nodes)
|
||||
|
||||
return filter(
|
||||
return list(filter(
|
||||
lambda x: 'id' not in x or
|
||||
(x['table_alias'], x['id']) not in keys, join_nodes)
|
||||
(x['table_alias'], x['id']) not in keys, join_nodes))
|
||||
|
||||
@api.model
|
||||
def get_fields(self, model_id):
|
||||
@@ -284,9 +283,9 @@ class IrModel(models.Model):
|
||||
# # 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))
|
||||
self.pool.setup_models(self._cr)
|
||||
|
||||
# signal that registry has changed
|
||||
self.pool.signal_registry_change()
|
||||
registry(self.env.cr.dbname).signal_changes()
|
||||
|
||||
return res
|
||||
|
||||
@@ -1,39 +1,123 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2017 Onestein (<http://www.onestein.eu>)
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
# Copyright 2017-2018 Onestein (<http://www.onestein.eu>)
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import api, models
|
||||
import logging
|
||||
|
||||
from odoo import _, api, models, tools
|
||||
from odoo.exceptions import UserError
|
||||
from odoo.tools.translate import _
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@api.model
|
||||
def _bi_view(_name):
|
||||
return _name[0:6] == 'x_bve.'
|
||||
|
||||
|
||||
@api.model_cr_context
|
||||
def _auto_init(self):
|
||||
""" Initialize the database schema of ``self``:
|
||||
- create the corresponding table,
|
||||
- create/update the necessary columns/tables for fields,
|
||||
- initialize new columns on existing rows,
|
||||
- add the SQL constraints given on the model,
|
||||
- add the indexes on indexed fields,
|
||||
|
||||
Also prepare post-init stuff to:
|
||||
- add foreign key constraints,
|
||||
- reflect models, fields, relations and constraints,
|
||||
- mark fields to recompute on existing records.
|
||||
|
||||
Note: you should not override this method. Instead, you can modify
|
||||
the model's database schema by overriding method :meth:`~.init`,
|
||||
which is called right after this one.
|
||||
"""
|
||||
# This monkey patch is meant to fix an error (probably
|
||||
# introduced by https://github.com/odoo/odoo/pull/15412), while
|
||||
# running an update all. The _auto_init() method invoked during
|
||||
# an update all is the one of BaseModel, and not the one of Base.
|
||||
|
||||
# START OF patch
|
||||
# TODO: find better ways to do this patch
|
||||
if _bi_view(self._name):
|
||||
return
|
||||
# END of patch
|
||||
|
||||
models.raise_on_invalid_object_name(self._name)
|
||||
|
||||
# This prevents anything called by this method (in particular default
|
||||
# values) from prefetching a field for which the corresponding column
|
||||
# has not been added in database yet!
|
||||
self = self.with_context(prefetch_fields=False)
|
||||
|
||||
self.pool.post_init(self._reflect)
|
||||
|
||||
cr = self._cr
|
||||
parent_store_compute = False
|
||||
update_custom_fields = self._context.get('update_custom_fields', False)
|
||||
must_create_table = not tools.table_exists(cr, self._table)
|
||||
|
||||
if self._auto:
|
||||
if must_create_table:
|
||||
tools.create_model_table(cr, self._table, self._description)
|
||||
|
||||
if self._parent_store:
|
||||
if not tools.column_exists(cr, self._table, 'parent_left'):
|
||||
self._create_parent_columns()
|
||||
parent_store_compute = True
|
||||
|
||||
self._check_removed_columns(log=False)
|
||||
|
||||
# update the database schema for fields
|
||||
columns = tools.table_columns(cr, self._table)
|
||||
|
||||
def recompute(field):
|
||||
_logger.info("Storing computed values of %s", field)
|
||||
recs = self.with_context(active_test=False).search([])
|
||||
recs._recompute_todo(field)
|
||||
|
||||
for field in self._fields.values():
|
||||
if not field.store:
|
||||
continue
|
||||
|
||||
if field.manual and not update_custom_fields:
|
||||
continue # don't update custom fields
|
||||
|
||||
new = field.update_db(self, columns)
|
||||
if new and field.compute:
|
||||
self.pool.post_init(recompute, field)
|
||||
|
||||
if self._auto:
|
||||
self._add_sql_constraints()
|
||||
|
||||
if must_create_table:
|
||||
self._execute_sql()
|
||||
|
||||
if parent_store_compute:
|
||||
self._parent_store_compute()
|
||||
|
||||
|
||||
models.BaseModel._auto_init = _auto_init
|
||||
|
||||
|
||||
class Base(models.AbstractModel):
|
||||
_inherit = 'base'
|
||||
|
||||
@api.model
|
||||
def _bi_view(self):
|
||||
return self._name[0:6] == 'x_bve.'
|
||||
|
||||
@api.model
|
||||
def _auto_end(self):
|
||||
if not self._bi_view():
|
||||
super(Base, self)._auto_end()
|
||||
|
||||
@api.model
|
||||
def _auto_init(self):
|
||||
if not self._bi_view():
|
||||
if not _bi_view(self._name):
|
||||
super(Base, self)._auto_init()
|
||||
|
||||
@api.model
|
||||
def _setup_complete(self):
|
||||
if not self._bi_view():
|
||||
if not _bi_view(self._name):
|
||||
super(Base, self)._setup_complete()
|
||||
else:
|
||||
self.pool.models[self._name]._log_access = False
|
||||
|
||||
@api.model
|
||||
def _read_group_process_groupby(self, gb, query):
|
||||
if not self._bi_view():
|
||||
if not _bi_view(self._name):
|
||||
return super(Base, self)._read_group_process_groupby(gb, query)
|
||||
|
||||
split = gb.split(':')
|
||||
@@ -44,18 +128,6 @@ class Base(models.AbstractModel):
|
||||
|
||||
@api.model
|
||||
def _add_magic_fields(self):
|
||||
if self._bi_view():
|
||||
if _bi_view(self._name):
|
||||
self._log_access = False
|
||||
return super(Base, self)._add_magic_fields()
|
||||
|
||||
@api.model_cr
|
||||
def _table_exist(self):
|
||||
if not self._bi_view():
|
||||
return super(Base, self)._table_exist()
|
||||
return 1
|
||||
|
||||
# @api.model_cr
|
||||
# def _create_table(self):
|
||||
# if not self._bi_view():
|
||||
# return super(Base, self)._create_table()
|
||||
# return 1
|
||||
|
||||
Reference in New Issue
Block a user