pre-commit

pull/444/head
Andrea 2020-09-09 12:52:32 +02:00
parent 2d17441547
commit fb5ba3bce4
17 changed files with 1368 additions and 1093 deletions

View File

@ -2,28 +2,24 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{ {
'name': 'BI View Editor', "name": "BI View Editor",
'summary': 'Graphical BI views builder for Odoo', "summary": "Graphical BI views builder for Odoo",
'images': ['static/description/main_screenshot.png'], "images": ["static/description/main_screenshot.png"],
'author': 'Onestein,Odoo Community Association (OCA)', "author": "Onestein,Odoo Community Association (OCA)",
'license': 'AGPL-3', "license": "AGPL-3",
'website': 'https://github.com/OCA/reporting-engine', "website": "https://github.com/OCA/reporting-engine",
'category': 'Reporting', "category": "Reporting",
'version': '12.0.1.0.0', "version": "12.0.1.0.0",
'development_status': 'Beta', "development_status": "Beta",
'depends': [ "depends": ["web",],
'web', "data": [
"security/ir.model.access.csv",
"security/rules.xml",
"templates/assets_template.xml",
"views/bve_view.xml",
], ],
'data': [ "qweb": ["static/src/xml/bi_view_editor.xml"],
'security/ir.model.access.csv', "post_load": "post_load",
'security/rules.xml', "uninstall_hook": "uninstall_hook",
'templates/assets_template.xml', "installable": True,
'views/bve_view.xml',
],
'qweb': [
'static/src/xml/bi_view_editor.xml'
],
'post_load': 'post_load',
'uninstall_hook': 'uninstall_hook',
'installable': True,
} }

View File

@ -3,20 +3,17 @@
import logging import logging
from odoo import SUPERUSER_ID from odoo import SUPERUSER_ID, api, modules
from odoo import api, modules
from odoo.tools import existing_tables, topological_sort from odoo.tools import existing_tables, topological_sort
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
def _bi_view(_name): def _bi_view(_name):
return _name.startswith('x_bve.') return _name.startswith("x_bve.")
def post_load(): def post_load():
def check_tables_exist(self, cr): def check_tables_exist(self, cr):
""" """
Verify that all tables are present and try to initialize Verify that all tables are present and try to initialize
@ -28,11 +25,11 @@ def post_load():
env = api.Environment(cr, SUPERUSER_ID, {}) env = api.Environment(cr, SUPERUSER_ID, {})
table2model = { table2model = {
model._table: name for name, model in env.items() model._table: name
for name, model in env.items()
if not model._abstract and not _bi_view(name) # here is the patch if not model._abstract and not _bi_view(name) # here is the patch
} }
missing_tables = set(table2model).difference( missing_tables = set(table2model).difference(existing_tables(cr, table2model))
existing_tables(cr, table2model))
if missing_tables: if missing_tables:
missing = {table2model[table] for table in missing_tables} missing = {table2model[table] for table in missing_tables}
@ -45,7 +42,8 @@ def post_load():
env[name].init() env[name].init()
# check again, and log errors if tables are still missing # check again, and log errors if tables are still missing
missing_tables = set(table2model).difference( missing_tables = set(table2model).difference(
existing_tables(cr, table2model)) existing_tables(cr, table2model)
)
for table in missing_tables: for table in missing_tables:
_logger.error("Model %s has no table.", table2model[table]) _logger.error("Model %s has no table.", table2model[table])
@ -55,18 +53,24 @@ def post_load():
def uninstall_hook(cr, registry): def uninstall_hook(cr, registry):
# delete dirty data that could cause problems # delete dirty data that could cause problems
# while re-installing the module # while re-installing the module
cr.execute(""" cr.execute(
"""
delete from ir_model where model like 'x_bve.%' delete from ir_model where model like 'x_bve.%'
""") """
cr.execute(""" )
cr.execute(
"""
delete from bve_view where model_name like 'x_bve.%' delete from bve_view where model_name like 'x_bve.%'
""") """
cr.execute(""" )
cr.execute(
"""
SELECT 'DROP VIEW ' || table_name SELECT 'DROP VIEW ' || table_name
FROM information_schema.views FROM information_schema.views
WHERE table_schema NOT IN ('pg_catalog', 'information_schema') WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
AND table_name like 'x_bve_%' AND table_name like 'x_bve_%'
""") """
)
results = list(cr.fetchall()) results = list(cr.fetchall())
for result in results: for result in results:
cr.execute(result[0]) cr.execute(result[0])

View File

@ -3,6 +3,7 @@
import base64 import base64
import json import json
import pydot import pydot
from psycopg2.extensions import AsIs from psycopg2.extensions import AsIs
@ -11,163 +12,160 @@ from odoo.exceptions import UserError, ValidationError
class BveView(models.Model): class BveView(models.Model):
_name = 'bve.view' _name = "bve.view"
_description = 'BI View Editor' _description = "BI View Editor"
@api.depends('group_ids', 'group_ids.users') @api.depends("group_ids", "group_ids.users")
def _compute_users(self): def _compute_users(self):
for bve_view in self.sudo(): for bve_view in self.sudo():
if bve_view.group_ids: if bve_view.group_ids:
bve_view.user_ids = bve_view.group_ids.mapped('users') bve_view.user_ids = bve_view.group_ids.mapped("users")
else: else:
bve_view.user_ids = self.env['res.users'].sudo().search([]) bve_view.user_ids = self.env["res.users"].sudo().search([])
@api.depends('name') @api.depends("name")
def _compute_model_name(self): def _compute_model_name(self):
for bve_view in self: for bve_view in self:
name = [x for x in bve_view.name.lower() if x.isalnum()] name = [x for x in bve_view.name.lower() if x.isalnum()]
model_name = ''.join(name).replace('_', '.').replace(' ', '.') model_name = "".join(name).replace("_", ".").replace(" ", ".")
bve_view.model_name = 'x_bve.' + model_name bve_view.model_name = "x_bve." + model_name
def _compute_serialized_data(self): def _compute_serialized_data(self):
for bve_view in self: for bve_view in self:
serialized_data = [] serialized_data = []
for line in bve_view.line_ids.sorted(key=lambda r: r.sequence): for line in bve_view.line_ids.sorted(key=lambda r: r.sequence):
serialized_data.append({ serialized_data.append(
'sequence': line.sequence, {
'model_id': line.model_id.id, "sequence": line.sequence,
'id': line.field_id.id, "model_id": line.model_id.id,
'name': line.name, "id": line.field_id.id,
'model_name': line.model_id.name, "name": line.name,
'model': line.model_id.model, "model_name": line.model_id.name,
'type': line.ttype, "model": line.model_id.model,
'table_alias': line.table_alias, "type": line.ttype,
'description': line.description, "table_alias": line.table_alias,
'row': line.row, "description": line.description,
'column': line.column, "row": line.row,
'measure': line.measure, "column": line.column,
'list': line.in_list, "measure": line.measure,
'join_node': line.join_node, "list": line.in_list,
'relation': line.relation, "join_node": line.join_node,
}) "relation": line.relation,
}
)
bve_view.data = json.dumps(serialized_data) bve_view.data = json.dumps(serialized_data)
def _inverse_serialized_data(self): def _inverse_serialized_data(self):
for bve_view in self: for bve_view in self:
line_ids = self._sync_lines_and_data(bve_view.data) line_ids = self._sync_lines_and_data(bve_view.data)
bve_view.write({'line_ids': line_ids}) bve_view.write({"line_ids": line_ids})
name = fields.Char(required=True, copy=False) name = fields.Char(required=True, copy=False)
model_name = fields.Char(compute='_compute_model_name', store=True) model_name = fields.Char(compute="_compute_model_name", store=True)
note = fields.Text(string='Notes') note = fields.Text(string="Notes")
state = fields.Selection([ state = fields.Selection(
('draft', 'Draft'), [("draft", "Draft"), ("created", "Created")], default="draft", copy=False
('created', 'Created') )
], default='draft', copy=False)
data = fields.Char( data = fields.Char(
compute='_compute_serialized_data', compute="_compute_serialized_data",
inverse='_inverse_serialized_data', inverse="_inverse_serialized_data",
help="Use the special query builder to define the query " help="Use the special query builder to define the query "
"to generate your report dataset. " "to generate your report dataset. "
"NOTE: To be edited, the query should be in 'Draft' status.") "NOTE: To be edited, the query should be in 'Draft' status.",
line_ids = fields.One2many( )
'bve.view.line', line_ids = fields.One2many("bve.view.line", "bve_view_id", string="Lines")
'bve_view_id',
string='Lines')
field_ids = fields.One2many( field_ids = fields.One2many(
'bve.view.line', "bve.view.line",
'bve_view_id', "bve_view_id",
domain=['|', ('join_node', '=', -1), ('join_node', '=', False)], domain=["|", ("join_node", "=", -1), ("join_node", "=", False)],
string='Fields') string="Fields",
)
relation_ids = fields.One2many( relation_ids = fields.One2many(
'bve.view.line', "bve.view.line",
'bve_view_id', "bve_view_id",
domain=[('join_node', '!=', -1), ('join_node', '!=', False)], domain=[("join_node", "!=", -1), ("join_node", "!=", False)],
string='Relations') string="Relations",
action_id = fields.Many2one('ir.actions.act_window', string='Action') )
view_id = fields.Many2one('ir.ui.view', string='View') action_id = fields.Many2one("ir.actions.act_window", string="Action")
view_id = fields.Many2one("ir.ui.view", string="View")
group_ids = fields.Many2many( group_ids = fields.Many2many(
'res.groups', "res.groups",
string='Groups', string="Groups",
help="User groups allowed to see the generated report; " help="User groups allowed to see the generated report; "
"if NO groups are specified the report will be public " "if NO groups are specified the report will be public "
"for everyone.") "for everyone.",
)
user_ids = fields.Many2many( user_ids = fields.Many2many(
'res.users', "res.users", string="Users", compute="_compute_users", store=True
string='Users', )
compute='_compute_users', query = fields.Text(compute="_compute_sql_query")
store=True)
query = fields.Text(compute='_compute_sql_query')
over_condition = fields.Text( over_condition = fields.Text(
states={ states={"draft": [("readonly", False),],},
'draft': [
('readonly', False),
],
},
readonly=True, readonly=True,
help="Condition to be inserted in the OVER part " help="Condition to be inserted in the OVER part "
"of the ID's row_number function.\n" "of the ID's row_number function.\n"
"For instance, 'ORDER BY t1.id' would create " "For instance, 'ORDER BY t1.id' would create "
"IDs ordered in the same way as t1's IDs; otherwise " "IDs ordered in the same way as t1's IDs; otherwise "
"IDs are assigned with no specific order.", "IDs are assigned with no specific order.",
) )
er_diagram_image = fields.Binary(compute='_compute_er_diagram_image') er_diagram_image = fields.Binary(compute="_compute_er_diagram_image")
_sql_constraints = [ _sql_constraints = [
('name_uniq', ("name_uniq", "unique(name)", _("Custom BI View names must be unique!")),
'unique(name)',
_('Custom BI View names must be unique!')),
] ]
@api.depends('line_ids') @api.depends("line_ids")
def _compute_er_diagram_image(self): def _compute_er_diagram_image(self):
for bve_view in self: for bve_view in self:
graph = pydot.Dot(graph_type='graph') graph = pydot.Dot(graph_type="graph")
table_model_map = {} table_model_map = {}
for line in bve_view.field_ids: for line in bve_view.field_ids:
if line.table_alias not in table_model_map: if line.table_alias not in table_model_map:
table_alias_node = pydot.Node( table_alias_node = pydot.Node(
line.model_id.name + ' ' + line.table_alias, line.model_id.name + " " + line.table_alias,
style="filled", style="filled",
shape='box', shape="box",
fillcolor="#DDDDDD" fillcolor="#DDDDDD",
) )
table_model_map[line.table_alias] = table_alias_node table_model_map[line.table_alias] = table_alias_node
graph.add_node(table_model_map[line.table_alias]) graph.add_node(table_model_map[line.table_alias])
field_node = pydot.Node( field_node = pydot.Node(
line.table_alias + '.' + line.field_id.field_description, line.table_alias + "." + line.field_id.field_description,
label=line.description, label=line.description,
style="filled", style="filled",
fillcolor="green" fillcolor="green",
) )
graph.add_node(field_node) graph.add_node(field_node)
graph.add_edge(pydot.Edge( graph.add_edge(
table_model_map[line.table_alias], pydot.Edge(table_model_map[line.table_alias], field_node)
field_node )
))
for line in bve_view.relation_ids: for line in bve_view.relation_ids:
field_description = line.field_id.field_description field_description = line.field_id.field_description
table_alias = line.table_alias table_alias = line.table_alias
diamond_node = pydot.Node( diamond_node = pydot.Node(
line.ttype + ' ' + table_alias + '.' + field_description, line.ttype + " " + table_alias + "." + field_description,
label=table_alias + '.' + field_description, label=table_alias + "." + field_description,
style="filled", style="filled",
shape='diamond', shape="diamond",
fillcolor="#D2D2FF" fillcolor="#D2D2FF",
) )
graph.add_node(diamond_node) graph.add_node(diamond_node)
graph.add_edge(pydot.Edge( graph.add_edge(
table_model_map[table_alias], pydot.Edge(
diamond_node, table_model_map[table_alias],
labelfontcolor="#D2D2FF", diamond_node,
color="blue" labelfontcolor="#D2D2FF",
)) color="blue",
graph.add_edge(pydot.Edge( )
diamond_node, )
table_model_map[line.join_node], graph.add_edge(
labelfontcolor="black", pydot.Edge(
color="blue" diamond_node,
)) table_model_map[line.join_node],
labelfontcolor="black",
color="blue",
)
)
try: try:
png_base64_image = base64.b64encode(graph.create_png()) png_base64_image = base64.b64encode(graph.create_png())
bve_view.er_diagram_image = png_base64_image bve_view.er_diagram_image = png_base64_image
@ -179,9 +177,9 @@ class BveView(models.Model):
def _get_field_def(line): def _get_field_def(line):
field_type = line.view_field_type field_type = line.view_field_type
return '<field name="%s" type="%s" />' % (line.name, field_type) return '<field name="{}" type="{}" />'.format(line.name, field_type)
bve_field_lines = self.field_ids.filtered('view_field_type') bve_field_lines = self.field_ids.filtered("view_field_type")
return list(map(_get_field_def, bve_field_lines)) return list(map(_get_field_def, bve_field_lines))
def _create_tree_view_arch(self): def _create_tree_view_arch(self):
@ -189,134 +187,161 @@ class BveView(models.Model):
def _get_field_attrs(line): def _get_field_attrs(line):
attr = line.list_attr attr = line.list_attr
res = attr and '%s="%s"' % (attr, line.description) or '' res = attr and '{}="{}"'.format(attr, line.description) or ""
return '<field name="%s" %s />' % (line.name, res) return '<field name="{}" {} />'.format(line.name, res)
bve_field_lines = self.field_ids.filtered(lambda l: l.in_list) bve_field_lines = self.field_ids.filtered(lambda l: l.in_list)
return list(map(_get_field_attrs, bve_field_lines)) return list(map(_get_field_attrs, bve_field_lines))
def _create_bve_view(self): def _create_bve_view(self):
self.ensure_one() self.ensure_one()
View = self.env['ir.ui.view'].sudo() View = self.env["ir.ui.view"].sudo()
# delete old views # delete old views
View.search([('model', '=', self.model_name)]).unlink() View.search([("model", "=", self.model_name)]).unlink()
# create views # create views
View.create([{ View.create(
'name': 'Pivot Analysis', [
'type': 'pivot', {
'model': self.model_name, "name": "Pivot Analysis",
'priority': 16, "type": "pivot",
'arch': """<?xml version="1.0"?> "model": self.model_name,
"priority": 16,
"arch": """<?xml version="1.0"?>
<pivot string="Pivot Analysis"> <pivot string="Pivot Analysis">
{} {}
</pivot> </pivot>
""".format("".join(self._create_view_arch())) """.format(
}, { "".join(self._create_view_arch())
'name': 'Graph Analysis', ),
'type': 'graph', },
'model': self.model_name, {
'priority': 16, "name": "Graph Analysis",
'arch': """<?xml version="1.0"?> "type": "graph",
"model": self.model_name,
"priority": 16,
"arch": """<?xml version="1.0"?>
<graph string="Graph Analysis" <graph string="Graph Analysis"
type="bar" stacked="True"> type="bar" stacked="True">
{} {}
</graph> </graph>
""".format("".join(self._create_view_arch())) """.format(
}, { "".join(self._create_view_arch())
'name': 'Search BI View', ),
'type': 'search', },
'model': self.model_name, {
'priority': 16, "name": "Search BI View",
'arch': """<?xml version="1.0"?> "type": "search",
"model": self.model_name,
"priority": 16,
"arch": """<?xml version="1.0"?>
<search string="Search BI View"> <search string="Search BI View">
{} {}
</search> </search>
""".format("".join(self._create_view_arch())) """.format(
}]) "".join(self._create_view_arch())
),
},
]
)
# create Tree view # create Tree view
tree_view = View.create({ tree_view = View.create(
'name': 'Tree Analysis', {
'type': 'tree', "name": "Tree Analysis",
'model': self.model_name, "type": "tree",
'priority': 16, "model": self.model_name,
'arch': """<?xml version="1.0"?> "priority": 16,
"arch": """<?xml version="1.0"?>
<tree string="List Analysis" create="false"> <tree string="List Analysis" create="false">
{} {}
</tree> </tree>
""".format("".join(self._create_tree_view_arch())) """.format(
}) "".join(self._create_tree_view_arch())
),
}
)
# set the Tree view as the default one # set the Tree view as the default one
action = self.env['ir.actions.act_window'].sudo().create({ action = (
'name': self.name, self.env["ir.actions.act_window"]
'res_model': self.model_name, .sudo()
'type': 'ir.actions.act_window', .create(
'view_type': 'form', {
'view_mode': 'tree,graph,pivot', "name": self.name,
'view_id': tree_view.id, "res_model": self.model_name,
'context': "{'service_name': '%s'}" % self.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,
}
)
)
self.write({ self.write(
'action_id': action.id, {"action_id": action.id, "view_id": tree_view.id, "state": "created"}
'view_id': tree_view.id, )
'state': 'created'
})
def _build_access_rules(self, model): def _build_access_rules(self, model):
self.ensure_one() self.ensure_one()
if not self.group_ids: if not self.group_ids:
self.env['ir.model.access'].sudo().create({ self.env["ir.model.access"].sudo().create(
'name': 'read access to ' + self.model_name, {
'model_id': model.id, "name": "read access to " + self.model_name,
'perm_read': True, "model_id": model.id,
}) "perm_read": True,
}
)
else: else:
# read access only to model # read access only to model
access_vals = [{ access_vals = [
'name': 'read access to ' + self.model_name, {
'model_id': model.id, "name": "read access to " + self.model_name,
'group_id': group.id, "model_id": model.id,
'perm_read': True "group_id": group.id,
} for group in self.group_ids] "perm_read": True,
self.env['ir.model.access'].sudo().create(access_vals) }
for group in self.group_ids
]
self.env["ir.model.access"].sudo().create(access_vals)
def _create_sql_view(self): def _create_sql_view(self):
self.ensure_one() self.ensure_one()
view_name = self.model_name.replace('.', '_') view_name = self.model_name.replace(".", "_")
query = self.query and self.query.replace('\n', ' ') query = self.query and self.query.replace("\n", " ")
# robustness in case something went wrong # robustness in case something went wrong
self._cr.execute('DROP TABLE IF EXISTS %s', (AsIs(view_name), )) self._cr.execute("DROP TABLE IF EXISTS %s", (AsIs(view_name),))
# create postgres view # create postgres view
try: try:
with self.env.cr.savepoint(): with self.env.cr.savepoint():
self.env.cr.execute('CREATE or REPLACE VIEW %s as (%s)', ( self.env.cr.execute(
AsIs(view_name), AsIs(query), )) "CREATE or REPLACE VIEW %s as (%s)", (AsIs(view_name), AsIs(query),)
)
except Exception as e: except Exception as e:
raise UserError( raise UserError(
_("Error creating the view '{query}':\n{error}") _("Error creating the view '{query}':\n{error}").format(
.format( query=query, error=e
query=query, )
error=e)) )
@api.depends('line_ids', 'state', 'over_condition') @api.depends("line_ids", "state", "over_condition")
def _compute_sql_query(self): def _compute_sql_query(self):
for bve_view in self: for bve_view in self:
tables_map = {} tables_map = {}
select_str = '\n CAST(row_number() OVER ({}) as integer) AS id' \ select_str = "\n CAST(row_number() OVER ({}) as integer) AS id".format(
.format(bve_view.over_condition or '') bve_view.over_condition or ""
)
for line in bve_view.field_ids: for line in bve_view.field_ids:
table = line.table_alias table = line.table_alias
select = line.field_id.name select = line.field_id.name
as_name = line.name as_name = line.name
select_str += ',\n {}.{} AS {}'.format(table, select, as_name) select_str += ",\n {}.{} AS {}".format(table, select, as_name)
if line.table_alias not in tables_map: if line.table_alias not in tables_map:
table = self.env[line.field_id.model_id.model]._table table = self.env[line.field_id.model_id.model]._table
@ -339,45 +364,53 @@ class BveView(models.Model):
from_str += " LEFT" if line.left_join else "" from_str += " LEFT" if line.left_join else ""
from_str += " JOIN {} ON {}.id = {}.{}".format( from_str += " JOIN {} ON {}.id = {}.{}".format(
table_format, table_format,
line.join_node, line.table_alias, line.field_id.name line.join_node,
line.table_alias,
line.field_id.name,
) )
if line.join_node not in seen: if line.join_node not in seen:
from_str += "\n" from_str += "\n"
seen.add(line.join_node) seen.add(line.join_node)
from_str += " LEFT" if line.left_join else "" from_str += " LEFT" if line.left_join else ""
from_str += " JOIN {} AS {} ON {}.{} = {}.id".format( from_str += " JOIN {} AS {} ON {}.{} = {}.id".format(
tables_map[line.join_node], line.join_node, tables_map[line.join_node],
line.table_alias, line.field_id.name, line.join_node line.join_node,
line.table_alias,
line.field_id.name,
line.join_node,
) )
bve_view.query = """SELECT %s\n\nFROM %s bve_view.query = """SELECT %s\n\nFROM %s
""" % (AsIs(select_str), AsIs(from_str),) """ % (
AsIs(select_str),
AsIs(from_str),
)
def action_translations(self): def action_translations(self):
self.ensure_one() self.ensure_one()
if self.state != 'created': if self.state != "created":
return return
self = self.sudo() self = self.sudo()
model = self.env['ir.model'].search([('model', '=', self.model_name)]) model = self.env["ir.model"].search([("model", "=", self.model_name)])
IrTranslation = self.env['ir.translation'] IrTranslation = self.env["ir.translation"]
IrTranslation.translate_fields('ir.model', model.id) IrTranslation.translate_fields("ir.model", model.id)
for field in model.field_id: for field in model.field_id:
IrTranslation.translate_fields('ir.model.fields', field.id) IrTranslation.translate_fields("ir.model.fields", field.id)
return { return {
'name': 'Translations', "name": "Translations",
'res_model': 'ir.translation', "res_model": "ir.translation",
'type': 'ir.actions.act_window', "type": "ir.actions.act_window",
'view_mode': 'tree', "view_mode": "tree",
'view_id': self.env.ref('base.view_translation_dialog_tree').id, "view_id": self.env.ref("base.view_translation_dialog_tree").id,
'target': 'current', "target": "current",
'flags': {'search_view': True, 'action_buttons': True}, "flags": {"search_view": True, "action_buttons": True},
'domain': [ "domain": [
'|', "|",
'&', "&",
('res_id', 'in', model.field_id.ids), ("res_id", "in", model.field_id.ids),
('name', '=', 'ir.model.fields,field_description'), ("name", "=", "ir.model.fields,field_description"),
'&', "&",
('res_id', '=', model.id), ("res_id", "=", model.id),
('name', '=', 'ir.model,name') ("name", "=", "ir.model,name"),
], ],
} }
@ -396,12 +429,19 @@ class BveView(models.Model):
# create model and fields # create model and fields
bve_fields = self.line_ids.filtered(lambda l: not l.join_node) bve_fields = self.line_ids.filtered(lambda l: not l.join_node)
model = self.env['ir.model'].sudo().with_context(bve=True).create({ model = (
'name': self.name, self.env["ir.model"]
'model': self.model_name, .sudo()
'state': 'manual', .with_context(bve=True)
'field_id': [(0, 0, f) for f in bve_fields._prepare_field_vals()], .create(
}) {
"name": self.name,
"model": self.model_name,
"state": "manual",
"field_id": [(0, 0, f) for f in bve_fields._prepare_field_vals()],
}
)
)
# give access rights # give access rights
self._build_access_rules(model) self._build_access_rules(model)
@ -415,57 +455,69 @@ class BveView(models.Model):
if not self.group_ids: if not self.group_ids:
return return
for line_model in self.line_ids.mapped('model_id'): for line_model in self.line_ids.mapped("model_id"):
res_count = self.env['ir.model.access'].sudo().search([ res_count = (
('model_id', '=', line_model.id), self.env["ir.model.access"]
('perm_read', '=', True), .sudo()
'|', .search(
('group_id', '=', False), [
('group_id', 'in', self.group_ids.ids), ("model_id", "=", line_model.id),
], limit=1) ("perm_read", "=", True),
"|",
("group_id", "=", False),
("group_id", "in", self.group_ids.ids),
],
limit=1,
)
)
if not res_count: if not res_count:
access_records = self.env['ir.model.access'].sudo().search([ access_records = (
('model_id', '=', line_model.id), self.env["ir.model.access"]
('perm_read', '=', True), .sudo()
]) .search(
group_list = '' [("model_id", "=", line_model.id), ("perm_read", "=", True),]
for group in access_records.mapped('group_id'): )
group_list += ' * %s\n' % (group.full_name, ) )
group_list = ""
for group in access_records.mapped("group_id"):
group_list += " * {}\n".format(group.full_name)
msg_title = _( msg_title = _(
'The model "%s" cannot be accessed by users with the ' 'The model "%s" cannot be accessed by users with the '
'selected groups only.' % (line_model.name, )) "selected groups only." % (line_model.name,)
msg_details = _( )
'At least one of the following groups must be added:') msg_details = _("At least one of the following groups must be added:")
raise UserError(_( raise UserError(
'%s\n\n%s\n%s' % (msg_title, msg_details, group_list,) _("{}\n\n{}\n{}".format(msg_title, msg_details, group_list))
)) )
def _check_invalid_lines(self): def _check_invalid_lines(self):
self.ensure_one() self.ensure_one()
if not self.line_ids: if not self.line_ids:
raise ValidationError(_('No data to process.')) raise ValidationError(_("No data to process."))
if any(not line.model_id for line in self.line_ids): if any(not line.model_id for line in self.line_ids):
invalid_lines = self.line_ids.filtered(lambda l: not l.model_id) invalid_lines = self.line_ids.filtered(lambda l: not l.model_id)
missing_models = set(invalid_lines.mapped('model_name')) missing_models = set(invalid_lines.mapped("model_name"))
missing_models = ', '.join(missing_models) missing_models = ", ".join(missing_models)
raise ValidationError(_( raise ValidationError(
'Following models are missing: %s.\n' _(
'Probably some modules were uninstalled.' % (missing_models,) "Following models are missing: %s.\n"
)) "Probably some modules were uninstalled." % (missing_models,)
)
)
if any(not line.field_id for line in self.line_ids): if any(not line.field_id for line in self.line_ids):
invalid_lines = self.line_ids.filtered(lambda l: not l.field_id) invalid_lines = self.line_ids.filtered(lambda l: not l.field_id)
missing_fields = set(invalid_lines.mapped('field_name')) missing_fields = set(invalid_lines.mapped("field_name"))
missing_fields = ', '.join(missing_fields) missing_fields = ", ".join(missing_fields)
raise ValidationError(_( raise ValidationError(
'Following fields are missing: %s.' % (missing_fields,) _("Following fields are missing: {}.".format(missing_fields))
)) )
def open_view(self): def open_view(self):
self.ensure_one() self.ensure_one()
self._check_invalid_lines() self._check_invalid_lines()
[action] = self.action_id.read() [action] = self.action_id.read()
action['display_name'] = _('BI View') action["display_name"] = _("BI View")
return action return action
@api.multi @api.multi
@ -479,10 +531,8 @@ class BveView(models.Model):
has_menus = False has_menus = False
if self.action_id: if self.action_id:
action = 'ir.actions.act_window,%d' % (self.action_id.id,) action = "ir.actions.act_window,%d" % (self.action_id.id,)
menus = self.env['ir.ui.menu'].search([ menus = self.env["ir.ui.menu"].search([("action", "=", action)])
('action', '=', action)
])
has_menus = True if menus else False has_menus = True if menus else False
menus.unlink() menus.unlink()
@ -490,26 +540,26 @@ class BveView(models.Model):
self.sudo().action_id.view_id.unlink() self.sudo().action_id.view_id.unlink()
self.sudo().action_id.unlink() self.sudo().action_id.unlink()
self.env['ir.ui.view'].sudo().search( self.env["ir.ui.view"].sudo().search([("model", "=", self.model_name)]).unlink()
[('model', '=', self.model_name)]).unlink() models_to_delete = (
models_to_delete = self.env['ir.model'].sudo().search([ self.env["ir.model"].sudo().search([("model", "=", self.model_name)])
('model', '=', self.model_name)]) )
if models_to_delete: if models_to_delete:
models_to_delete.unlink() models_to_delete.unlink()
table_name = self.model_name.replace('.', '_') table_name = self.model_name.replace(".", "_")
tools.drop_view_if_exists(self.env.cr, table_name) tools.drop_view_if_exists(self.env.cr, table_name)
self.state = 'draft' self.state = "draft"
if has_menus: if has_menus:
return {'type': 'ir.actions.client', 'tag': 'reload'} return {"type": "ir.actions.client", "tag": "reload"}
def unlink(self): def unlink(self):
if self.filtered(lambda v: v.state == 'created'): if self.filtered(lambda v: v.state == "created"):
raise UserError( raise UserError(
_('You cannot delete a created view! ' _("You cannot delete a created view! " "Reset the view to draft first.")
'Reset the view to draft first.')) )
return super().unlink() return super().unlink()
@api.model @api.model
@ -521,63 +571,69 @@ class BveView(models.Model):
table_model_map = {} table_model_map = {}
for item in fields_info: for item in fields_info:
if item.get('join_node', -1) == -1: if item.get("join_node", -1) == -1:
table_model_map[item['table_alias']] = item['model_id'] table_model_map[item["table_alias"]] = item["model_id"]
for sequence, field_info in enumerate(fields_info, start=1): for sequence, field_info in enumerate(fields_info, start=1):
join_model_id = False join_model_id = False
join_node = field_info.get('join_node', -1) join_node = field_info.get("join_node", -1)
if join_node != -1 and table_model_map.get(join_node): if join_node != -1 and table_model_map.get(join_node):
join_model_id = int(table_model_map[join_node]) join_model_id = int(table_model_map[join_node])
line_ids += [(0, False, { line_ids += [
'sequence': sequence, (
'model_id': field_info['model_id'], 0,
'table_alias': field_info['table_alias'], False,
'description': field_info['description'], {
'field_id': field_info['id'], "sequence": sequence,
'ttype': field_info['type'], "model_id": field_info["model_id"],
'row': field_info['row'], "table_alias": field_info["table_alias"],
'column': field_info['column'], "description": field_info["description"],
'measure': field_info['measure'], "field_id": field_info["id"],
'in_list': field_info['list'], "ttype": field_info["type"],
'relation': field_info.get('relation'), "row": field_info["row"],
'join_node': field_info.get('join_node'), "column": field_info["column"],
'join_model_id': join_model_id, "measure": field_info["measure"],
})] "in_list": field_info["list"],
"relation": field_info.get("relation"),
"join_node": field_info.get("join_node"),
"join_model_id": join_model_id,
},
)
]
return line_ids return line_ids
@api.constrains('line_ids') @api.constrains("line_ids")
def _constraint_line_ids(self): def _constraint_line_ids(self):
models_with_tables = self.env.registry.models.keys() models_with_tables = self.env.registry.models.keys()
for view in self: for view in self:
nodes = view.line_ids.filtered(lambda n: n.join_node) nodes = view.line_ids.filtered(lambda n: n.join_node)
nodes_models = nodes.mapped('table_alias') nodes_models = nodes.mapped("table_alias")
nodes_models += nodes.mapped('join_node') nodes_models += nodes.mapped("join_node")
not_nodes = view.line_ids.filtered(lambda n: not n.join_node) not_nodes = view.line_ids.filtered(lambda n: not n.join_node)
not_nodes_models = not_nodes.mapped('table_alias') not_nodes_models = not_nodes.mapped("table_alias")
err_msg = _('Inconsistent lines.') err_msg = _("Inconsistent lines.")
if set(nodes_models) - set(not_nodes_models): if set(nodes_models) - set(not_nodes_models):
raise ValidationError(err_msg) raise ValidationError(err_msg)
if len(set(not_nodes_models) - set(nodes_models)) > 1: if len(set(not_nodes_models) - set(nodes_models)) > 1:
raise ValidationError(err_msg) raise ValidationError(err_msg)
models = view.line_ids.mapped('model_id') models = view.line_ids.mapped("model_id")
if models.filtered(lambda m: m.model not in models_with_tables): if models.filtered(lambda m: m.model not in models_with_tables):
raise ValidationError(_('Abstract models not supported.')) raise ValidationError(_("Abstract models not supported."))
@api.model @api.model
def get_clean_list(self, data_dict): def get_clean_list(self, data_dict):
serialized_data = json.loads(data_dict) serialized_data = json.loads(data_dict)
table_alias_list = set() table_alias_list = set()
for item in serialized_data: for item in serialized_data:
if item.get('join_node', -1) in [-1, False]: if item.get("join_node", -1) in [-1, False]:
table_alias_list.add(item['table_alias']) table_alias_list.add(item["table_alias"])
for item in serialized_data: for item in serialized_data:
if item.get('join_node', -1) not in [-1, False]: if item.get("join_node", -1) not in [-1, False]:
if item['table_alias'] not in table_alias_list: if item["table_alias"] not in table_alias_list:
serialized_data.remove(item) serialized_data.remove(item)
elif item['join_node'] not in table_alias_list: elif item["join_node"] not in table_alias_list:
serialized_data.remove(item) serialized_data.remove(item)
return json.dumps(serialized_data) return json.dumps(serialized_data)

View File

@ -6,19 +6,19 @@ from odoo.exceptions import ValidationError
class BveViewLine(models.Model): class BveViewLine(models.Model):
_name = 'bve.view.line' _name = "bve.view.line"
_description = 'BI View Editor Lines' _description = "BI View Editor Lines"
name = fields.Char(compute='_compute_name') name = fields.Char(compute="_compute_name")
sequence = fields.Integer(default=1) sequence = fields.Integer(default=1)
bve_view_id = fields.Many2one('bve.view', ondelete='cascade') bve_view_id = fields.Many2one("bve.view", ondelete="cascade")
model_id = fields.Many2one('ir.model', string='Model') model_id = fields.Many2one("ir.model", string="Model")
model_name = fields.Char(compute='_compute_model_name', store=True) model_name = fields.Char(compute="_compute_model_name", store=True)
table_alias = fields.Char() table_alias = fields.Char()
join_model_id = fields.Many2one('ir.model', string='Join Model') join_model_id = fields.Many2one("ir.model", string="Join Model")
field_id = fields.Many2one('ir.model.fields', string='Field') field_id = fields.Many2one("ir.model.fields", string="Field")
field_name = fields.Char(compute='_compute_model_field_name', store=True) field_name = fields.Char(compute="_compute_model_field_name", store=True)
ttype = fields.Char(string='Type') ttype = fields.Char(string="Type")
description = fields.Char(translate=True) description = fields.Char(translate=True)
relation = fields.Char() relation = fields.Char()
join_node = fields.Char() join_node = fields.Char()
@ -28,88 +28,87 @@ class BveViewLine(models.Model):
column = fields.Boolean() column = fields.Boolean()
measure = fields.Boolean() measure = fields.Boolean()
in_list = fields.Boolean() in_list = fields.Boolean()
list_attr = fields.Selection([ list_attr = fields.Selection(
('sum', 'Sum'), [("sum", "Sum"), ("avg", "Average"),], string="List Attribute", default="sum"
('avg', 'Average'), )
], string='List Attribute', default='sum') view_field_type = fields.Char(compute="_compute_view_field_type")
view_field_type = fields.Char(compute='_compute_view_field_type')
@api.depends('row', 'column', 'measure') @api.depends("row", "column", "measure")
def _compute_view_field_type(self): def _compute_view_field_type(self):
for line in self: for line in self:
row = line.row and 'row' row = line.row and "row"
column = line.column and 'col' column = line.column and "col"
measure = line.measure and 'measure' measure = line.measure and "measure"
line.view_field_type = row or column or measure line.view_field_type = row or column or measure
@api.constrains('row', 'column', 'measure') @api.constrains("row", "column", "measure")
def _constrains_options_check(self): def _constrains_options_check(self):
measure_types = ['float', 'integer', 'monetary'] measure_types = ["float", "integer", "monetary"]
for line in self.filtered(lambda l: l.row or l.column): for line in self.filtered(lambda l: l.row or l.column):
if line.join_model_id or line.ttype in measure_types: if line.join_model_id or line.ttype in measure_types:
err_msg = _('This field cannot be a row or a column.') err_msg = _("This field cannot be a row or a column.")
raise ValidationError(err_msg) raise ValidationError(err_msg)
for line in self.filtered(lambda l: l.measure): for line in self.filtered(lambda l: l.measure):
if line.join_model_id or line.ttype not in measure_types: if line.join_model_id or line.ttype not in measure_types:
err_msg = _('This field cannot be a measure.') err_msg = _("This field cannot be a measure.")
raise ValidationError(err_msg) raise ValidationError(err_msg)
@api.constrains('table_alias', 'field_id') @api.constrains("table_alias", "field_id")
def _constrains_unique_fields_check(self): def _constrains_unique_fields_check(self):
seen = set() seen = set()
for line in self.mapped('bve_view_id.field_ids'): for line in self.mapped("bve_view_id.field_ids"):
if (line.table_alias, line.field_id.id, ) not in seen: if (line.table_alias, line.field_id.id,) not in seen:
seen.add((line.table_alias, line.field_id.id, )) seen.add((line.table_alias, line.field_id.id,))
else: else:
raise ValidationError(_('Field %s/%s is duplicated.\n' raise ValidationError(
'Please remove the duplications.') % ( _("Field %s/%s is duplicated.\n" "Please remove the duplications.")
line.field_id.model, line.field_id.name % (line.field_id.model, line.field_id.name)
)) )
@api.depends('field_id', 'sequence') @api.depends("field_id", "sequence")
def _compute_name(self): def _compute_name(self):
for line in self.filtered(lambda l: l.field_id): for line in self.filtered(lambda l: l.field_id):
field_name = line.field_id.name field_name = line.field_id.name
line.name = 'x_bve_%s_%s' % (line.table_alias, field_name,) line.name = "x_bve_{}_{}".format(line.table_alias, field_name)
@api.depends('model_id') @api.depends("model_id")
def _compute_model_name(self): def _compute_model_name(self):
for line in self.filtered(lambda l: l.model_id): for line in self.filtered(lambda l: l.model_id):
line.model_name = line.model_id.model line.model_name = line.model_id.model
@api.depends('field_id') @api.depends("field_id")
def _compute_model_field_name(self): def _compute_model_field_name(self):
for line in self.filtered(lambda l: l.field_id): for line in self.filtered(lambda l: l.field_id):
field_name = line.description field_name = line.description
model_name = line.model_name model_name = line.model_name
line.field_name = '%s (%s)' % (field_name, model_name, ) line.field_name = "{} ({})".format(field_name, model_name)
def _prepare_field_vals(self): def _prepare_field_vals(self):
vals_list = [] vals_list = []
for line in self: for line in self:
field = line.field_id field = line.field_id
vals = { vals = {
'name': line.name, "name": line.name,
'complete_name': field.complete_name, "complete_name": field.complete_name,
'model': line.bve_view_id.model_name, "model": line.bve_view_id.model_name,
'relation': field.relation, "relation": field.relation,
'field_description': line.description, "field_description": line.description,
'ttype': field.ttype, "ttype": field.ttype,
'selection': field.selection, "selection": field.selection,
'size': field.size, "size": field.size,
'state': 'manual', "state": "manual",
'readonly': True, "readonly": True,
'groups': [(6, 0, field.groups.ids)], "groups": [(6, 0, field.groups.ids)],
} }
if vals['ttype'] == 'monetary': if vals["ttype"] == "monetary":
vals.update({'ttype': 'float'}) vals.update({"ttype": "float"})
if field.ttype == 'selection' and not field.selection: if field.ttype == "selection" and not field.selection:
model_obj = self.env[field.model_id.model] model_obj = self.env[field.model_id.model]
selection = model_obj._fields[field.name].selection selection = model_obj._fields[field.name].selection
if callable(selection): if callable(selection):
selection_domain = selection(model_obj) selection_domain = selection(model_obj)
else: else:
selection_domain = selection selection_domain = selection
vals.update({'selection': str(selection_domain)}) vals.update({"selection": str(selection_domain)})
vals_list.append(vals) vals_list.append(vals)
return vals_list return vals_list

View File

@ -5,98 +5,88 @@ from collections import defaultdict
from odoo import api, models, registry from odoo import api, models, registry
NO_BI_MODELS = [ NO_BI_MODELS = ["fetchmail.server"]
'fetchmail.server'
]
NO_BI_TTYPES = [ NO_BI_TTYPES = ["many2many", "one2many", "html", "binary", "reference"]
'many2many',
'one2many',
'html',
'binary',
'reference'
]
def dict_for_field(field): def dict_for_field(field):
return { return {
'id': field.id, "id": field.id,
'name': field.name, "name": field.name,
'description': field.field_description, "description": field.field_description,
'type': field.ttype, "type": field.ttype,
'relation': field.relation, "relation": field.relation,
'custom': False, "custom": False,
'model_id': field.model_id.id, "model_id": field.model_id.id,
'model': field.model_id.model, "model": field.model_id.model,
'model_name': field.model_id.name "model_name": field.model_id.name,
} }
def dict_for_model(model): def dict_for_model(model):
return { return {"id": model.id, "name": model.name, "model": model.model}
'id': model.id,
'name': model.name,
'model': model.model
}
class IrModel(models.Model): class IrModel(models.Model):
_inherit = 'ir.model' _inherit = "ir.model"
@api.model @api.model
def _filter_bi_models(self, model): def _filter_bi_models(self, model):
def _check_name(model_model): def _check_name(model_model):
if model_model in NO_BI_MODELS: if model_model in NO_BI_MODELS:
return 1 return 1
return 0 return 0
def _check_startswith(model_model): def _check_startswith(model_model):
if model_model.startswith('workflow') or \ if (
model_model.startswith('ir.') or \ model_model.startswith("workflow")
model_model.startswith('base_'): or model_model.startswith("ir.")
or model_model.startswith("base_")
):
return 1 return 1
return 0 return 0
def _check_contains(model_model): def _check_contains(model_model):
if 'mail' in model_model or \ if (
'report' in model_model or \ "mail" in model_model
'edi.' in model_model: or "report" in model_model
or "edi." in model_model
):
return 1 return 1
return 0 return 0
def _check_unknown(model_name): def _check_unknown(model_name):
if model_name == 'Unknown' or '.' in model_name: if model_name == "Unknown" or "." in model_name:
return 1 return 1
return 0 return 0
model_model = model['model'] model_model = model["model"]
model_name = model['name'] model_name = model["name"]
count_check = 0 count_check = 0
count_check += _check_name(model_model) count_check += _check_name(model_model)
count_check += _check_startswith(model_model) count_check += _check_startswith(model_model)
count_check += _check_contains(model_model) count_check += _check_contains(model_model)
count_check += _check_unknown(model_name) count_check += _check_unknown(model_name)
if not count_check: if not count_check:
return self.env['ir.model.access'].check( return self.env["ir.model.access"].check(model["model"], "read", False)
model['model'], 'read', False)
return False return False
def get_model_list(self, model_table_map): def get_model_list(self, model_table_map):
if not model_table_map: if not model_table_map:
return [] return []
domain = [('model_id', 'in', list(model_table_map.keys())), domain = [
('store', '=', True), ("model_id", "in", list(model_table_map.keys())),
('ttype', '=', 'many2one')] ("store", "=", True),
fields = self.env['ir.model.fields'].sudo().search(domain) ("ttype", "=", "many2one"),
]
fields = self.env["ir.model.fields"].sudo().search(domain)
model_list = [] model_list = []
for field in fields: for field in fields:
for table_alias in model_table_map[field.model_id.id]: for table_alias in model_table_map[field.model_id.id]:
model_list.append(dict( model_list.append(
dict_for_field(field), dict(dict_for_field(field), table_alias=table_alias, join_node=-1,)
table_alias=table_alias, )
join_node=-1,
))
return model_list return model_list
def get_relation_list(self, model_table_map): def get_relation_list(self, model_table_map):
@ -106,32 +96,31 @@ class IrModel(models.Model):
for model in self.sudo().browse(model_table_map.keys()): for model in self.sudo().browse(model_table_map.keys()):
model_names.update({model.model: model.id}) model_names.update({model.model: model.id})
domain = [('relation', 'in', list(model_names.keys())), domain = [
('store', '=', True), ("relation", "in", list(model_names.keys())),
('ttype', '=', 'many2one')] ("store", "=", True),
fields = self.env['ir.model.fields'].sudo().search(domain) ("ttype", "=", "many2one"),
]
fields = self.env["ir.model.fields"].sudo().search(domain)
relation_list = [] relation_list = []
for field in fields: for field in fields:
model_id = model_names[field.relation] model_id = model_names[field.relation]
for join_node in model_table_map[model_id]: for join_node in model_table_map[model_id]:
relation_list.append(dict( relation_list.append(
dict_for_field(field), dict(dict_for_field(field), join_node=join_node, table_alias=-1)
join_node=join_node, )
table_alias=-1
))
return relation_list return relation_list
@api.model @api.model
def _get_related_models_domain(self, model_table_map): def _get_related_models_domain(self, model_table_map):
domain = [('transient', '=', False)] domain = [("transient", "=", False)]
if model_table_map: if model_table_map:
model_list = self.get_model_list(model_table_map) model_list = self.get_model_list(model_table_map)
relation_list = self.get_relation_list(model_table_map) relation_list = self.get_relation_list(model_table_map)
model_ids = [f['model_id'] for f in relation_list + model_list] model_ids = [f["model_id"] for f in relation_list + model_list]
model_ids += list(model_table_map.keys()) model_ids += list(model_table_map.keys())
relations = [f['relation'] for f in model_list] relations = [f["relation"] for f in model_list]
domain += [ domain += ["|", ("id", "in", model_ids), ("model", "in", relations)]
'|', ('id', 'in', model_ids), ('model', 'in', relations)]
return domain return domain
@api.model @api.model
@ -140,7 +129,7 @@ class IrModel(models.Model):
joined with the already selected models. joined with the already selected models.
""" """
domain = self._get_related_models_domain(model_table_map) domain = self._get_related_models_domain(model_table_map)
return self.sudo().search(domain, order='name asc') return self.sudo().search(domain, order="name asc")
@api.model @api.model
def get_models(self, table_model_map=None): def get_models(self, table_model_map=None):
@ -166,6 +155,7 @@ class IrModel(models.Model):
Return all possible join nodes to add new_field to the query Return all possible join nodes to add new_field to the query
containing model_ids. containing model_ids.
""" """
def remove_duplicate_nodes(join_nodes): def remove_duplicate_nodes(join_nodes):
seen = set() seen = set()
nodes_list = [] nodes_list = []
@ -181,24 +171,24 @@ class IrModel(models.Model):
keys = [] keys = []
model_table_map = defaultdict(list) model_table_map = defaultdict(list)
for field in field_data: for field in field_data:
model_table_map[field['model_id']].append(field['table_alias']) model_table_map[field["model_id"]].append(field["table_alias"])
if field.get('join_node', -1) != -1: if field.get("join_node", -1) != -1:
keys.append((field['table_alias'], field['id'])) keys.append((field["table_alias"], field["id"]))
# nodes in current model # nodes in current model
existing_aliases = model_table_map[new_field['model_id']] existing_aliases = model_table_map[new_field["model_id"]]
join_nodes = [{'table_alias': alias} for alias in existing_aliases] join_nodes = [{"table_alias": alias} for alias in existing_aliases]
# nodes in past selected models # nodes in past selected models
for field in self.get_model_list(model_table_map): for field in self.get_model_list(model_table_map):
if new_field['model'] == field['relation']: if new_field["model"] == field["relation"]:
if (field['table_alias'], field['id']) not in keys: if (field["table_alias"], field["id"]) not in keys:
join_nodes.append(field) join_nodes.append(field)
# nodes in new model # nodes in new model
for field in self.get_relation_list(model_table_map): for field in self.get_relation_list(model_table_map):
if new_field['model_id'] == field['model_id']: if new_field["model_id"] == field["model_id"]:
if (field['table_alias'], field['id']) not in keys: if (field["table_alias"], field["id"]) not in keys:
join_nodes.append(field) join_nodes.append(field)
return remove_duplicate_nodes(join_nodes) return remove_duplicate_nodes(join_nodes)
@ -207,29 +197,36 @@ class IrModel(models.Model):
def get_fields(self, model_id): def get_fields(self, model_id):
self = self.with_context(lang=self.env.user.lang) self = self.with_context(lang=self.env.user.lang)
fields = self.env['ir.model.fields'].sudo().search([ fields = (
('model_id', '=', model_id), self.env["ir.model.fields"]
('store', '=', True), .sudo()
('name', 'not in', models.MAGIC_COLUMNS), .search(
('ttype', 'not in', NO_BI_TTYPES) [
], order='field_description desc') ("model_id", "=", model_id),
("store", "=", True),
("name", "not in", models.MAGIC_COLUMNS),
("ttype", "not in", NO_BI_TTYPES),
],
order="field_description desc",
)
)
fields_dict = list(map(dict_for_field, fields)) fields_dict = list(map(dict_for_field, fields))
return fields_dict return fields_dict
@api.model @api.model
def create(self, vals): def create(self, vals):
if self.env.context and self.env.context.get('bve'): if self.env.context and self.env.context.get("bve"):
vals['state'] = 'base' vals["state"] = "base"
res = super().create(vals) res = super().create(vals)
# this sql update is necessary since a write method here would # this sql update is necessary since a write method here would
# be not working (an orm constraint is restricting the modification # be not working (an orm constraint is restricting the modification
# of the state field while updating ir.model) # of the state field while updating ir.model)
q = "UPDATE ir_model SET state = 'manual' WHERE id = %s" q = "UPDATE ir_model SET state = 'manual' WHERE id = %s"
self.env.cr.execute(q, (res.id, )) self.env.cr.execute(q, (res.id,))
# # update registry # # update registry
if self.env.context.get('bve'): if self.env.context.get("bve"):
# setup models; this reloads custom models in registry # setup models; this reloads custom models in registry
self.pool.setup_models(self._cr) self.pool.setup_models(self._cr)

View File

@ -11,7 +11,7 @@ _logger = logging.getLogger(__name__)
@api.model @api.model
def _bi_view(_name): def _bi_view(_name):
return _name.startswith('x_bve.') return _name.startswith("x_bve.")
_auto_init_orig = models.BaseModel._auto_init _auto_init_orig = models.BaseModel._auto_init
@ -36,7 +36,7 @@ models.BaseModel._auto_init = _auto_init
class Base(models.AbstractModel): class Base(models.AbstractModel):
_inherit = 'base' _inherit = "base"
@api.model @api.model
def _setup_complete(self): def _setup_complete(self):
@ -50,10 +50,9 @@ class Base(models.AbstractModel):
if not _bi_view(self._name): if not _bi_view(self._name):
return super()._read_group_process_groupby(gb, query) return super()._read_group_process_groupby(gb, query)
split = gb.split(':') split = gb.split(":")
if split[0] not in self._fields: if split[0] not in self._fields:
raise UserError( raise UserError(_("No data to be displayed."))
_('No data to be displayed.'))
return super()._read_group_process_groupby(gb, query) return super()._read_group_process_groupby(gb, query)
@api.model @api.model

View File

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8" ?>
<odoo> <odoo>
<record id="bve_view_rule" model="ir.rule"> <record id="bve_view_rule" model="ir.rule">
<field name="name">bve_view read access</field> <field name="name">bve_view read access</field>
<field name="model_id" search="[('model','=','bve.view')]" model="ir.model"/> <field name="model_id" search="[('model','=','bve.view')]" model="ir.model" />
<field name="global" eval="True"/> <field name="global" eval="True" />
<field name="domain_force"> ['|',('user_ids','=',False),('user_ids','in',user.id)]</field> <field
name="domain_force"
> ['|',('user_ids','=',False),('user_ids','in',user.id)]</field>
</record> </record>
</odoo> </odoo>

View File

@ -1,9 +1,10 @@
.oe_form_field_bi_editor { .oe_form_field_bi_editor {
/*box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);*/ /*box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);*/
border: 1px solid #DDDDDD; border: 1px solid #dddddd;
} }
.oe_form_field_bi_editor .header, .oe_form_field_bi_editor .footer { .oe_form_field_bi_editor .header,
.oe_form_field_bi_editor .footer {
width: 100%; width: 100%;
height: 50px; height: 50px;
background-color: #7c7bad; background-color: #7c7bad;
@ -11,11 +12,12 @@
} }
.oe_form_field_bi_editor .footer { .oe_form_field_bi_editor .footer {
background-color: #FFF; background-color: #fff;
border-top: 1px solid #DDDDDD; border-top: 1px solid #dddddd;
} }
.oe_form_field_bi_editor .header .left, .oe_form_field_bi_editor .footer .left { .oe_form_field_bi_editor .header .left,
.oe_form_field_bi_editor .footer .left {
width: 75%; width: 75%;
float: left; float: left;
line-height: 50px; line-height: 50px;
@ -23,7 +25,8 @@
padding-top: 13px; padding-top: 13px;
} }
.oe_form_field_bi_editor .header .right, .oe_form_field_bi_editor .footer .right { .oe_form_field_bi_editor .header .right,
.oe_form_field_bi_editor .footer .right {
width: 25%; width: 25%;
float: right; float: right;
padding-top: 13px; padding-top: 13px;
@ -39,7 +42,7 @@
float: left; float: left;
width: 30%; width: 30%;
box-sizing: border-box; box-sizing: border-box;
border-right: 1px solid #DDDDDD; border-right: 1px solid #dddddd;
} }
.oe_form_field_bi_editor .body .left .search-bar { .oe_form_field_bi_editor .body .left .search-bar {
@ -84,19 +87,19 @@
} }
.oe_form_field_bi_editor .body .left .class-list.readonly { .oe_form_field_bi_editor .body .left .class-list.readonly {
opacity: .35; opacity: 0.35;
} }
.oe_form_field_bi_editor .body .left .class-list .class.readonly { .oe_form_field_bi_editor .body .left .class-list .class.readonly {
cursor: default; cursor: default;
} }
.oe_form_field_bi_editor .body .left .class-list .class:hover { .oe_form_field_bi_editor .body .left .class-list .class:hover {
background-color: #7C7BAD; background-color: #7c7bad;
color: #FFF; color: #fff;
} }
.oe_form_field_bi_editor .body .left .class-list .field { .oe_form_field_bi_editor .body .left .class-list .field {
font-weight: normal; font-weight: normal;
padding-left: 20px; padding-left: 20px;
padding-top: 3px; padding-top: 3px;
@ -130,26 +133,27 @@
background-color: transparent; background-color: transparent;
border: none; border: none;
background-image: none; background-image: none;
padding: 0; padding: 0;
cursor: pointer; cursor: pointer;
} }
.oe_form_field_bi_editor .body .right .field-list tbody tr:hover { .oe_form_field_bi_editor .body .right .field-list tbody tr:hover {
background-color: #DDD; background-color: #ddd;
} }
.oe_form_field_bi_editor .body .right .field-list tbody tr.join-node { .oe_form_field_bi_editor .body .right .field-list tbody tr.join-node {
background-color: #D2D2FF; background-color: #d2d2ff;
text-align: center; text-align: center;
border-top: 1px solid #DDDDDD; border-top: 1px solid #dddddd;
} }
.oe_form_field_bi_editor .context-menu, .oe_form_field_bi_editor .context-menu ul { .oe_form_field_bi_editor .context-menu,
.oe_form_field_bi_editor .context-menu ul {
z-index: 1000; z-index: 1000;
position: fixed; position: fixed;
background-color: #fff; background-color: #fff;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15); box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
border: 1px solid #DDDDDD; border: 1px solid #dddddd;
list-style-type: none; list-style-type: none;
padding: 0; padding: 0;
width: 175px; width: 175px;
@ -174,8 +178,8 @@
} }
.oe_form_field_bi_editor .context-menu li:hover { .oe_form_field_bi_editor .context-menu li:hover {
background-color: #7C7BAD; background-color: #7c7bad;
color: #FFF; color: #fff;
} }
.oe_form_field_bi_editor .context-menu ul { .oe_form_field_bi_editor .context-menu ul {

View File

@ -1,57 +1,55 @@
/* Copyright 2015-2019 Onestein (<https://www.onestein.eu>) /* Copyright 2015-2019 Onestein (<https://www.onestein.eu>)
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
odoo.define('bi_view_editor.FieldList', function (require) { odoo.define("bi_view_editor.FieldList", function(require) {
"use strict"; "use strict";
var core = require('web.core'); var core = require("web.core");
var qweb = core.qweb; var qweb = core.qweb;
var Widget = require('web.Widget'); var Widget = require("web.Widget");
var FieldListContextMenu = Widget.extend({ var FieldListContextMenu = Widget.extend({
start: function () { start: function() {
var res = this._super.apply(this, arguments); var res = this._super.apply(this, arguments);
this.$el.mouseleave(function () { this.$el.mouseleave(function() {
$(this).addClass('d-none'); $(this).addClass("d-none");
}); });
return res; return res;
}, },
open: function (x, y) { open: function(x, y) {
this.$el.css({ this.$el.css({
'left': x + 'px', left: x + "px",
'top': y + 'px', top: y + "px",
}); });
this.$el.removeClass('d-none'); this.$el.removeClass("d-none");
return _.extend({}, window.Backbone.Events); return _.extend({}, window.Backbone.Events);
}, },
}); });
var FieldListFieldContextMenu = FieldListContextMenu.extend({ var FieldListFieldContextMenu = FieldListContextMenu.extend({
template: 'bi_view_editor.FieldList.FieldContextMenu', template: "bi_view_editor.FieldList.FieldContextMenu",
open: function (x, y, field) { open: function(x, y, field) {
this.$el.find('.checkbox-column').prop('checked', field.column); this.$el.find(".checkbox-column").prop("checked", field.column);
this.$el.find('.checkbox-row').prop('checked', field.row); this.$el.find(".checkbox-row").prop("checked", field.row);
this.$el.find('.checkbox-measure').prop('checked', field.measure); this.$el.find(".checkbox-measure").prop("checked", field.measure);
this.$el.find('.checkbox-list').prop('checked', field.list); this.$el.find(".checkbox-list").prop("checked", field.list);
var measureable = var measureable =
field.type === "float" || field.type === "float" ||
field.type === "integer" || field.type === "integer" ||
field.type === "monetary" field.type === "monetary";
; this.$el.find(".checkbox-column").attr("disabled", measureable);
this.$el.find(".checkbox-row").attr("disabled", measureable);
this.$el.find('.checkbox-column').attr('disabled', measureable); this.$el.find(".checkbox-measure").attr("disabled", !measureable);
this.$el.find('.checkbox-row').attr('disabled', measureable); this.$el.find(".checkbox-list").attr("disabled", false);
this.$el.find('.checkbox-measure').attr('disabled', !measureable);
this.$el.find('.checkbox-list').attr('disabled', false);
var events = this._super(x, y, field); var events = this._super(x, y, field);
this.$el.find('input').unbind('change'); this.$el.find("input").unbind("change");
this.$el.find('input').change(function () { this.$el.find("input").change(function() {
var $checkbox = $(this); var $checkbox = $(this);
var property = $checkbox.attr('data-for'); var property = $checkbox.attr("data-for");
field[property] = $checkbox.is(':checked'); field[property] = $checkbox.is(":checked");
events.trigger('change', field); events.trigger("change", field);
}); });
return events; return events;
@ -59,67 +57,71 @@ odoo.define('bi_view_editor.FieldList', function (require) {
}); });
var FieldListJoinContextMenu = FieldListContextMenu.extend({ var FieldListJoinContextMenu = FieldListContextMenu.extend({
template: 'bi_view_editor.FieldList.JoinContextMenu', template: "bi_view_editor.FieldList.JoinContextMenu",
open: function (x, y, node) { open: function(x, y, node) {
this.$el.find('.checkbox-join-left').prop('checked', node.join_left); this.$el.find(".checkbox-join-left").prop("checked", node.join_left);
var events = this._super(x, y, node); var events = this._super(x, y, node);
this.$el.find('input').unbind('change'); this.$el.find("input").unbind("change");
this.$el.find('input').change(function () { this.$el.find("input").change(function() {
var $checkbox = $(this); var $checkbox = $(this);
var property = $checkbox.attr('data-for'); var property = $checkbox.attr("data-for");
node[property] = $checkbox.is(':checked'); node[property] = $checkbox.is(":checked");
events.trigger('change', node); events.trigger("change", node);
}); });
return events; return events;
}, },
}); });
var FieldList = Widget.extend({ var FieldList = Widget.extend({
template: 'bi_view_editor.FieldList', template: "bi_view_editor.FieldList",
events: { events: {
'click .delete-button': 'removeClicked', "click .delete-button": "removeClicked",
'keyup input[name="description"]': 'keyupDescription', 'keyup input[name="description"]': "keyupDescription",
}, },
start: function () { start: function() {
var res = this._super.apply(this, arguments); var res = this._super.apply(this, arguments);
this.contextmenu = new FieldListFieldContextMenu(this); this.contextmenu = new FieldListFieldContextMenu(this);
this.contextmenu.appendTo(this.$el); this.contextmenu.appendTo(this.$el);
this.contextmenu_join = new FieldListJoinContextMenu(this); this.contextmenu_join = new FieldListJoinContextMenu(this);
this.contextmenu_join.appendTo(this.$el); this.contextmenu_join.appendTo(this.$el);
this.$table = this.$el.find('tbody'); this.$table = this.$el.find("tbody");
this.mode = null; this.mode = null;
return res; return res;
}, },
setMode: function (mode) { setMode: function(mode) {
if (mode === 'readonly') { if (mode === "readonly") {
this.$el.find('input[type="text"]').attr('disabled', true); this.$el.find('input[type="text"]').attr("disabled", true);
this.$el.find(".delete-button").addClass('d-none'); this.$el.find(".delete-button").addClass("d-none");
} else { } else {
this.$el.find('input[type="text"]').removeAttr('disabled'); this.$el.find('input[type="text"]').removeAttr("disabled");
this.$el.find(".delete-button").removeClass('d-none'); this.$el.find(".delete-button").removeClass("d-none");
} }
this.mode = mode; this.mode = mode;
}, },
get: function () { get: function() {
return $.makeArray(this.$el.find("tbody tr").map(function () { return $.makeArray(
var field = $(this).data('field'); this.$el.find("tbody tr").map(function() {
field.description = $(this).find('input[name="description"]').val(); var field = $(this).data("field");
return field; field.description = $(this)
})); .find('input[name="description"]')
.val();
return field;
})
);
}, },
getModelIds: function () { getModelIds: function() {
var model_ids = {}; var model_ids = {};
this.$el.find("tbody tr").each(function () { this.$el.find("tbody tr").each(function() {
var data = $(this).data('field'); var data = $(this).data("field");
model_ids[data.table_alias] = data.model_id; model_ids[data.table_alias] = data.model_id;
}); });
return model_ids; return model_ids;
}, },
getModelData: function () { getModelData: function() {
var model_data = {}; var model_data = {};
this.$el.find("tbody tr").each(function () { this.$el.find("tbody tr").each(function() {
var data = $(this).data('field'); var data = $(this).data("field");
model_data[data.table_alias] = { model_data[data.table_alias] = {
model_id: data.model_id, model_id: data.model_id,
model_name: data.model_name, model_name: data.model_name,
@ -127,95 +129,114 @@ odoo.define('bi_view_editor.FieldList', function (require) {
}); });
return model_data; return model_data;
}, },
add: function (field) { add: function(field) {
var self = this; var self = this;
field.row = typeof field.row === 'undefined' ? false : field.row; field.row = typeof field.row === "undefined" ? false : field.row;
field.column = typeof field.column === 'undefined' ? false : field.column; field.column = typeof field.column === "undefined" ? false : field.column;
field.measure = typeof field.measure === 'undefined' ? false : field.measure; field.measure =
field.list = typeof field.list === 'undefined' ? true : field.list; typeof field.measure === "undefined" ? false : field.measure;
field._id = typeof field._id === 'undefined' ? _.uniqueId('node_') : field._id; field.list = typeof field.list === "undefined" ? true : field.list;
field._id =
typeof field._id === "undefined" ? _.uniqueId("node_") : field._id;
if (field.join_node) { if (field.join_node) {
field.join_left = typeof field.join_left === 'undefined' ? false : field.join_left; field.join_left =
typeof field.join_left === "undefined" ? false : field.join_left;
} }
var i = 0; var i = 0;
var name = field.name; var name = field.name;
while (this.get().filter(function (item) { while (
return item.name === field.name; this.get().filter(function(item) {
}).length > 0) { return item.name === field.name;
field.name = name + '_' + i; }).length > 0
) {
field.name = name + "_" + i;
i++; i++;
} }
// Render table row // Render table row
var $html = $(qweb.render(field.join_node ? 'bi_view_editor.JoinListItem' : 'bi_view_editor.FieldListItem', { var $html = $(
'field': field, qweb.render(
})).data('field', field).contextmenu(function (e) { field.join_node
var $item = $(this); ? "bi_view_editor.JoinListItem"
if (self.mode === 'readonly') { : "bi_view_editor.FieldListItem",
return; {
} field: field,
e.preventDefault(); }
self.openContextMenu($item, e.pageX, e.pageY); )
}); )
.data("field", field)
.contextmenu(function(e) {
var $item = $(this);
if (self.mode === "readonly") {
return;
}
e.preventDefault();
self.openContextMenu($item, e.pageX, e.pageY);
});
this.$el.find('tbody').append($html); this.$el.find("tbody").append($html);
}, },
remove: function (id) { remove: function(id) {
var $item = this.$el.find('tr[data-id="' + id + '"]'); var $item = this.$el.find('tr[data-id="' + id + '"]');
$item.remove(); $item.remove();
this.trigger('removed', id); this.trigger("removed", id);
}, },
set: function (fields) { set: function(fields) {
var set_fields = fields; var set_fields = fields;
if (!set_fields) { if (!set_fields) {
set_fields = []; set_fields = [];
} }
this.$el.find('tbody tr').remove(); this.$el.find("tbody tr").remove();
for (var i = 0; i < set_fields.length; i++) { for (var i = 0; i < set_fields.length; i++) {
this.add(set_fields[i]); this.add(set_fields[i]);
} }
}, },
openContextMenu: function ($item, x, y) { openContextMenu: function($item, x, y) {
var field = $item.data('field'); var field = $item.data("field");
var contextmenu = field.join_node ? this.contextmenu_join : this.contextmenu; var contextmenu = field.join_node
? this.contextmenu_join
: this.contextmenu;
// Temporary disable contextmenu for join node (until left join is implemented) // Temporary disable contextmenu for join node (until left join is implemented)
if (field.join_node) { if (field.join_node) {
return; return;
} }
contextmenu.open(x - 20, y - 20, $item.data('field')).on('change', function (f) { contextmenu.open(x - 20, y - 20, $item.data("field")).on(
$item.data('field', f); "change",
this.refreshItem($item); function(f) {
this.trigger('updated'); $item.data("field", f);
}.bind(this)); this.refreshItem($item);
this.trigger("updated");
}.bind(this)
);
}, },
refreshItem: function ($item) { refreshItem: function($item) {
var data = $item.data('field'); var data = $item.data("field");
var $attributes = $item.find('span[data-for], img[data-for]'); var $attributes = $item.find("span[data-for], img[data-for]");
$.each($attributes, function () { $.each($attributes, function() {
var $attribute = $(this); var $attribute = $(this);
var value = data[$attribute.attr('data-for')]; var value = data[$attribute.attr("data-for")];
if (value) { if (value) {
$attribute.removeClass('d-none'); $attribute.removeClass("d-none");
} else { } else {
$attribute.addClass('d-none'); $attribute.addClass("d-none");
} }
}); });
}, },
removeClicked: function (e) { removeClicked: function(e) {
var $button = $(e.currentTarget); var $button = $(e.currentTarget);
var id = $button.attr('data-id'); var id = $button.attr("data-id");
this.remove(id); this.remove(id);
}, },
keyupDescription: function () { keyupDescription: function() {
this.trigger('updated'); this.trigger("updated");
}, },
}); });
return { return {
'FieldList': FieldList, FieldList: FieldList,
'FieldListContextMenu': FieldListContextMenu, FieldListContextMenu: FieldListContextMenu,
'FieldListFieldContextMenu': FieldListFieldContextMenu, FieldListFieldContextMenu: FieldListFieldContextMenu,
'FieldListJoinContextMenu': FieldListJoinContextMenu, FieldListJoinContextMenu: FieldListJoinContextMenu,
}; };
}); });

View File

@ -1,48 +1,51 @@
/* Copyright 2015-2019 Onestein (<https://www.onestein.eu>) /* Copyright 2015-2019 Onestein (<https://www.onestein.eu>)
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
odoo.define('bi_view_editor.JoinNodeDialog', function (require) { odoo.define("bi_view_editor.JoinNodeDialog", function(require) {
"use strict"; "use strict";
var Dialog = require("web.Dialog"); var Dialog = require("web.Dialog");
var core = require('web.core'); var core = require("web.core");
var qweb = core.qweb; var qweb = core.qweb;
var _t = core._t; var _t = core._t;
var JoinNodeDialog = Dialog.extend({ var JoinNodeDialog = Dialog.extend({
xmlDependencies: Dialog.prototype.xmlDependencies.concat([ xmlDependencies: Dialog.prototype.xmlDependencies.concat([
'/bi_view_editor/static/src/xml/bi_view_editor.xml', "/bi_view_editor/static/src/xml/bi_view_editor.xml",
]), ]),
events: { events: {
"click li": "choiceClicked", "click li": "choiceClicked",
}, },
init: function (parent, options, choices, model_data) { init: function(parent, options, choices, model_data) {
this.choices = choices; this.choices = choices;
// Prepare data for view // Prepare data for view
for (var i = 0; i < choices.length; i++) { for (var i = 0; i < choices.length; i++) {
if (choices[i].join_node !== -1 && choices[i].table_alias !== -1) { if (choices[i].join_node !== -1 && choices[i].table_alias !== -1) {
choices[i].model_name = model_data[choices[i].table_alias].model_name; choices[i].model_name =
model_data[choices[i].table_alias].model_name;
} }
choices[i].index = i; choices[i].index = i;
} }
var defaults = _.defaults(options || {}, { var defaults = _.defaults(options || {}, {
title: _t("Join..."), title: _t("Join..."),
dialogClass: 'oe_act_window', dialogClass: "oe_act_window",
$content: qweb.render('bi_view_editor.JoinNodeDialog', { $content: qweb.render("bi_view_editor.JoinNodeDialog", {
'choices': choices, choices: choices,
}), }),
buttons: [{ buttons: [
text: _t("Cancel"), {
classes: "btn-default o_form_button_cancel", text: _t("Cancel"),
close: true, classes: "btn-default o_form_button_cancel",
}], close: true,
},
],
}); });
this._super(parent, defaults); this._super(parent, defaults);
}, },
choiceClicked: function (e) { choiceClicked: function(e) {
this.trigger('chosen', { this.trigger("chosen", {
choice: this.choices[$(e.currentTarget).attr('data-index')], choice: this.choices[$(e.currentTarget).attr("data-index")],
}); });
this.close(); this.close();
}, },

View File

@ -1,87 +1,92 @@
/* Copyright 2015-2019 Onestein (<https://www.onestein.eu>) /* Copyright 2015-2019 Onestein (<https://www.onestein.eu>)
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
odoo.define('bi_view_editor.ModelList', function (require) { odoo.define("bi_view_editor.ModelList", function(require) {
"use strict"; "use strict";
var Widget = require('web.Widget'); var Widget = require("web.Widget");
var core = require('web.core'); var core = require("web.core");
var qweb = core.qweb; var qweb = core.qweb;
var ModelList = Widget.extend({ var ModelList = Widget.extend({
template: 'bi_view_editor.ModelList', template: "bi_view_editor.ModelList",
events: { events: {
'keyup .search-bar > input': 'filterChanged', "keyup .search-bar > input": "filterChanged",
}, },
init: function (parent) { init: function(parent) {
var res = this._super(parent); var res = this._super(parent);
this.active_models = []; this.active_models = [];
this.cache_fields = {}; this.cache_fields = {};
this.current_filter = ''; this.current_filter = "";
this.mode = null; this.mode = null;
return res; return res;
}, },
setMode: function (mode) { setMode: function(mode) {
if (mode === 'readonly') { if (mode === "readonly") {
this.$el.find('.search-bar').attr('disabled', true); this.$el.find(".search-bar").attr("disabled", true);
this.$el.find('.class-list, .class').addClass('readonly'); this.$el.find(".class-list, .class").addClass("readonly");
} else { } else {
this.$el.find('.search-bar').attr('disabled', false); this.$el.find(".search-bar").attr("disabled", false);
this.$el.find('.class-list, .class').removeClass('readonly'); this.$el.find(".class-list, .class").removeClass("readonly");
} }
this.mode = mode; this.mode = mode;
}, },
isActive: function (id) { isActive: function(id) {
return this.active_models.indexOf(id) !== -1; return this.active_models.indexOf(id) !== -1;
}, },
removeAsActive: function (id) { removeAsActive: function(id) {
var i = this.active_models.indexOf(id); var i = this.active_models.indexOf(id);
this.active_models.splice(i, 1); this.active_models.splice(i, 1);
}, },
addAsActive: function (id) { addAsActive: function(id) {
this.active_models.push(id); this.active_models.push(id);
}, },
loadModels: function (model_ids) { loadModels: function(model_ids) {
return this._rpc({ return this._rpc({
model: 'ir.model', model: "ir.model",
method: 'get_models', method: "get_models",
args: model_ids ? [model_ids] : [], args: model_ids ? [model_ids] : [],
}); });
}, },
loadFields: function (model_id) { loadFields: function(model_id) {
if (!(model_id in this.cache_fields)) { if (!(model_id in this.cache_fields)) {
var deferred = this._rpc({ var deferred = this._rpc({
model: 'ir.model', model: "ir.model",
method: 'get_fields', method: "get_fields",
args: [model_id], args: [model_id],
}); });
this.cache_fields[model_id] = deferred; this.cache_fields[model_id] = deferred;
} }
return this.cache_fields[model_id]; return this.cache_fields[model_id];
}, },
populateModels: function (models) { populateModels: function(models) {
var self = this; var self = this;
this.$el.find(".class-list").html(''); this.$el.find(".class-list").html("");
_.each(models, function (model) { _.each(models, function(model) {
var $html = $(qweb.render('bi_view_editor.ModelListItem', { var $html = $(
'id': model.id, qweb.render("bi_view_editor.ModelListItem", {
'model': model.model, id: model.id,
'name': model.name, model: model.model,
})); name: model.name,
$html.find('.class').data('model', model).click(function () { })
self.modelClicked($(this)); );
}); $html
.find(".class")
.data("model", model)
.click(function() {
self.modelClicked($(this));
});
self.$el.find(".class-list").append($html); self.$el.find(".class-list").append($html);
if (self.isActive(model.id)) { if (self.isActive(model.id)) {
self.loadFields(model.id).done(function (fields) { self.loadFields(model.id).done(function(fields) {
self.populateFields(fields, model.id); self.populateFields(fields, model.id);
}); });
} }
}); });
}, },
populateFields: function (fields, model_id) { populateFields: function(fields, model_id) {
var self = this; var self = this;
if (!model_id && fields.length === 0) { if (!model_id && fields.length === 0) {
return; return;
@ -91,59 +96,72 @@ odoo.define('bi_view_editor.ModelList', function (require) {
data_model_id = fields[0].model_id; data_model_id = fields[0].model_id;
} }
var $model_item = this.$el.find(".class[data-id='" + data_model_id + "']"); var $model_item = this.$el.find(".class[data-id='" + data_model_id + "']");
_.each(fields, function (field) { _.each(fields, function(field) {
var $field = $(qweb.render('bi_view_editor.ModelListFieldItem', { var $field = $(
name: field.name, qweb.render("bi_view_editor.ModelListFieldItem", {
description: field.description, name: field.name,
})).data('field', field).click(function () { description: field.description,
self.fieldClicked($(this)); })
}).draggable({ )
'revert': 'invalid', .data("field", field)
'scroll': false, .click(function() {
'helper': 'clone', self.fieldClicked($(this));
'appendTo': 'body', })
'containment': 'window', .draggable({
}); revert: "invalid",
scroll: false,
helper: "clone",
appendTo: "body",
containment: "window",
});
$model_item.after($field); $model_item.after($field);
}); });
}, },
modelClicked: function ($el) { modelClicked: function($el) {
if (this.mode === 'readonly') { if (this.mode === "readonly") {
return; return;
} }
var model = $el.data('model'); var model = $el.data("model");
$el.parent().find('.field').remove(); $el.parent()
.find(".field")
.remove();
if (this.isActive(model.id)) { if (this.isActive(model.id)) {
this.removeAsActive(model.id); this.removeAsActive(model.id);
} else { } else {
this.addAsActive(model.id); this.addAsActive(model.id);
this.loadFields(model.id).done(function (fields) { this.loadFields(model.id).done(
this.populateFields(fields, model.id); function(fields) {
}.bind(this)); this.populateFields(fields, model.id);
}.bind(this)
);
} }
}, },
fieldClicked: function ($el) { fieldClicked: function($el) {
if (this.mode === 'readonly') { if (this.mode === "readonly") {
return; return;
} }
this.trigger('field_clicked', $el.data('field')); this.trigger("field_clicked", $el.data("field"));
}, },
filterChanged: function (e) { filterChanged: function(e) {
var $input = $(e.target); var $input = $(e.target);
this.filter($input.val()); this.filter($input.val());
}, },
filter: function (value) { filter: function(value) {
this.active_models = []; this.active_models = [];
this.$el.find('.field').remove(); this.$el.find(".field").remove();
var val = typeof value === 'undefined' ? this.current_filter : value.toLowerCase(); var val =
this.$el.find(".class").each(function () { typeof value === "undefined"
var data = $(this).data('model'); ? this.current_filter
if (data.name.toLowerCase().indexOf(val) === -1 && : value.toLowerCase();
data.model.toLowerCase().indexOf(val) === -1) { this.$el.find(".class").each(function() {
$(this).addClass('d-none'); var data = $(this).data("model");
if (
data.name.toLowerCase().indexOf(val) === -1 &&
data.model.toLowerCase().indexOf(val) === -1
) {
$(this).addClass("d-none");
} else { } else {
$(this).removeClass('d-none'); $(this).removeClass("d-none");
} }
}); });
this.current_filter = val; this.current_filter = val;
@ -151,5 +169,4 @@ odoo.define('bi_view_editor.ModelList', function (require) {
}); });
return ModelList; return ModelList;
}); });

View File

@ -1,48 +1,48 @@
/* Copyright 2015-2019 Onestein (<https://www.onestein.eu>) /* Copyright 2015-2019 Onestein (<https://www.onestein.eu>)
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
odoo.define('bi_view_editor', function (require) { odoo.define("bi_view_editor", function(require) {
"use strict"; "use strict";
var JoinNodeDialog = require('bi_view_editor.JoinNodeDialog'); var JoinNodeDialog = require("bi_view_editor.JoinNodeDialog");
var ModelList = require('bi_view_editor.ModelList'); var ModelList = require("bi_view_editor.ModelList");
var FieldList = require('bi_view_editor.FieldList').FieldList; var FieldList = require("bi_view_editor.FieldList").FieldList;
var AbstractField = require('web.AbstractField'); var AbstractField = require("web.AbstractField");
var Data = require('web.data'); var Data = require("web.data");
var field_registry = require('web.field_registry'); var field_registry = require("web.field_registry");
var BiViewEditor = AbstractField.extend({ var BiViewEditor = AbstractField.extend({
template: "bi_view_editor.Frame", template: "bi_view_editor.Frame",
events: { events: {
"click .clear-btn": "clear", "click .clear-btn": "clear",
}, },
start: function () { start: function() {
var self = this; var self = this;
var res = this._super.apply(this, arguments); var res = this._super.apply(this, arguments);
// Init ModelList // Init ModelList
this.model_list = new ModelList(this); this.model_list = new ModelList(this);
this.model_list.appendTo(this.$(".body > .left")); this.model_list.appendTo(this.$(".body > .left"));
this.model_list.on('field_clicked', this, function (field) { this.model_list.on("field_clicked", this, function(field) {
self.addField(_.extend({}, field)); self.addField(_.extend({}, field));
}); });
// Init FieldList // Init FieldList
this.field_list = new FieldList(this); this.field_list = new FieldList(this);
this.field_list.appendTo(this.$(".body > .right")); this.field_list.appendTo(this.$(".body > .right"));
this.field_list.on('removed', this, this.fieldListRemoved); this.field_list.on("removed", this, this.fieldListRemoved);
this.field_list.on('updated', this, this.fieldListChanged); this.field_list.on("updated", this, this.fieldListChanged);
this.$el.find(".body > .right").droppable({ this.$el.find(".body > .right").droppable({
accept: "div.class-list div.field", accept: "div.class-list div.field",
drop: function (event, ui) { drop: function(event, ui) {
self.addField(_.extend({}, ui.draggable.data('field'))); self.addField(_.extend({}, ui.draggable.data("field")));
ui.draggable.draggable('option', 'revert', false); ui.draggable.draggable("option", "revert", false);
}, },
}); });
this.on("change:effective_readonly", this, function () { this.on("change:effective_readonly", this, function() {
this.updateMode(); this.updateMode();
}); });
this.renderValue(); this.renderValue();
@ -50,61 +50,65 @@ odoo.define('bi_view_editor', function (require) {
this.updateMode(); this.updateMode();
return res; return res;
}, },
clear: function () { clear: function() {
if (this.mode !== 'readonly') { if (this.mode !== "readonly") {
this.field_list.set([]); this.field_list.set([]);
this.loadAndPopulateModelList(); this.loadAndPopulateModelList();
this._setValue(this.field_list.get()); this._setValue(this.field_list.get());
} }
}, },
fieldListChanged: function () { fieldListChanged: function() {
this._setValue(this.field_list.get()); this._setValue(this.field_list.get());
}, },
fieldListRemoved: function () { fieldListRemoved: function() {
console.log(this.field_list.get()); console.log(this.field_list.get());
this._setValue(this.field_list.get()); this._setValue(this.field_list.get());
var model = new Data.DataSet(this, "bve.view"); var model = new Data.DataSet(this, "bve.view");
model.call('get_clean_list', [this.value]).then(function (result) { model.call("get_clean_list", [this.value]).then(
this.field_list.set(JSON.parse(result)); function(result) {
this._setValue(this.field_list.get()); this.field_list.set(JSON.parse(result));
}.bind(this)); this._setValue(this.field_list.get());
}.bind(this)
);
this.loadAndPopulateModelList(); this.loadAndPopulateModelList();
}, },
renderValue: function () { renderValue: function() {
this.field_list.set(JSON.parse(this.value)); this.field_list.set(JSON.parse(this.value));
}, },
updateMode: function () { updateMode: function() {
if (this.mode === 'readonly') { if (this.mode === "readonly") {
this.$el.find('.clear-btn').addClass('d-none'); this.$el.find(".clear-btn").addClass("d-none");
this.$el.find(".body .right").droppable("option", "disabled", true); this.$el.find(".body .right").droppable("option", "disabled", true);
} else { } else {
this.$el.find('.clear-btn').removeClass('d-none'); this.$el.find(".clear-btn").removeClass("d-none");
this.$el.find('.body .right').droppable('option', 'disabled', false); this.$el.find(".body .right").droppable("option", "disabled", false);
} }
this.field_list.setMode(this.mode); this.field_list.setMode(this.mode);
this.model_list.setMode(this.mode); this.model_list.setMode(this.mode);
}, },
loadAndPopulateModelList: function () { loadAndPopulateModelList: function() {
var model_ids = null; var model_ids = null;
if (this.field_list.get().length > 0) { if (this.field_list.get().length > 0) {
model_ids = this.field_list.getModelIds(); model_ids = this.field_list.getModelIds();
} }
this.model_list.loadModels(model_ids).done(function (models) { this.model_list.loadModels(model_ids).done(
this.model_list.populateModels(models); function(models) {
}.bind(this)); this.model_list.populateModels(models);
}.bind(this)
);
}, },
getTableAlias: function (field) { getTableAlias: function(field) {
if (typeof field.table_alias === 'undefined') { if (typeof field.table_alias === "undefined") {
var model_ids = this.field_list.getModelIds(); var model_ids = this.field_list.getModelIds();
var n = 1; var n = 1;
while (typeof model_ids["t" + n] !== 'undefined') { while (typeof model_ids["t" + n] !== "undefined") {
n++; n++;
} }
return "t" + n; return "t" + n;
} }
return field.table_alias; return field.table_alias;
}, },
addFieldAndJoinNode: function (field, join_node) { addFieldAndJoinNode: function(field, join_node) {
if (join_node.join_node === -1 || join_node.table_alias === -1) { if (join_node.join_node === -1 || join_node.table_alias === -1) {
field.table_alias = this.getTableAlias(field); field.table_alias = this.getTableAlias(field);
if (join_node.join_node === -1) { if (join_node.join_node === -1) {
@ -121,31 +125,37 @@ odoo.define('bi_view_editor', function (require) {
this.loadAndPopulateModelList(); this.loadAndPopulateModelList();
this._setValue(this.field_list.get()); this._setValue(this.field_list.get());
}, },
addField: function (field) { addField: function(field) {
var data = _.extend({}, field); var data = _.extend({}, field);
var model = new Data.DataSet(this, "ir.model"); var model = new Data.DataSet(this, "ir.model");
var field_data = this.field_list.get(); var field_data = this.field_list.get();
model.call('get_join_nodes', [field_data, data]).then(function (result) { model.call("get_join_nodes", [field_data, data]).then(
if (result.length === 1) { function(result) {
this.addFieldAndJoinNode(data, result[0]); if (result.length === 1) {
} else if (result.length > 1) { this.addFieldAndJoinNode(data, result[0]);
var dialog = new JoinNodeDialog(this, {}, result, this.field_list.getModelData()); } else if (result.length > 1) {
dialog.open().on('chosen', this, function (e) { var dialog = new JoinNodeDialog(
this.addFieldAndJoinNode(data, e.choice); this,
}); {},
} else { result,
data.table_alias = this.getTableAlias(data); this.field_list.getModelData()
this.field_list.add(data); );
this.loadAndPopulateModelList(); dialog.open().on("chosen", this, function(e) {
this._setValue(this.field_list.get()); this.addFieldAndJoinNode(data, e.choice);
} });
}.bind(this)); } else {
data.table_alias = this.getTableAlias(data);
this.field_list.add(data);
this.loadAndPopulateModelList();
this._setValue(this.field_list.get());
}
}.bind(this)
);
}, },
_parseValue: function (value) { _parseValue: function(value) {
return JSON.stringify(value); return JSON.stringify(value);
}, },
}); });
field_registry.add('BVEEditor', BiViewEditor); field_registry.add("BVEEditor", BiViewEditor);
}); });

View File

@ -1,81 +1,98 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8" ?>
<template> <template>
<t t-name="bi_view_editor.Frame"> <t t-name="bi_view_editor.Frame">
<div class="oe_form_field_bi_editor"> <div class="oe_form_field_bi_editor">
<div class="body"> <div class="body">
<div class="left"> <div class="left">
</div> </div>
<div class="right"> <div class="right">
</div> </div>
<div class="clear"></div> <div class="clear" />
</div> </div>
<div class="footer"> <div class="footer">
<div class="left"></div> <div class="left" />
<div class="right"><button class="clear-btn d-none"><span class="fa fa-eraser"></span> Clear</button></div> <div class="right">
<button class="clear-btn d-none"><span
class="fa fa-eraser"
/> Clear</button>
</div>
</div> </div>
</div> </div>
</t> </t>
<!-- Join Node Dialog --> <!-- Join Node Dialog -->
<t t-name="bi_view_editor.JoinNodeDialog"> <t t-name="bi_view_editor.JoinNodeDialog">
<div class="oe_bi_view_editor_join_node_dialog"> <div class="oe_bi_view_editor_join_node_dialog">
<ul class="list-group" > <ul class="list-group">
<t t-foreach="choices" t-as="choice"> <t t-foreach="choices" t-as="choice">
<t t-if="choice.join_node !== -1 and choice.table_alias !== -1"> <t t-if="choice.join_node !== -1 and choice.table_alias !== -1">
<li class="list-group-item list-group-item-action text-primary" t-attf-data-index="#{choice.index}"> <li
class="list-group-item list-group-item-action text-primary"
t-attf-data-index="#{choice.index}"
>
<b>Use the existing node</b> <b>Use the existing node</b>
</li> </li>
</t> </t>
<t t-elif="choice.join_node !== -1"> <t t-elif="choice.join_node !== -1">
<li class="list-group-item list-group-item-action text-success" t-attf-data-index="#{choice.index}"> <li
class="list-group-item list-group-item-action text-success"
t-attf-data-index="#{choice.index}"
>
Use the field Use the field
<b><t t-esc="choice.model_name"/></b> <b>
<i class="fa fa-caret-right"/> <t t-esc="choice.model_name" />
<b><t t-esc="choice.description"/></b> </b>
<i class="fa fa-caret-right" />
<b>
<t t-esc="choice.description" />
</b>
<span class="badge">new</span> <span class="badge">new</span>
</li> </li>
</t> </t>
<t t-else=""> <t t-else="">
<li class="list-group-item list-group-item-action" t-attf-data-index="#{choice.index}"> <li
class="list-group-item list-group-item-action"
t-attf-data-index="#{choice.index}"
>
Use the field Use the field
<b><t t-esc="choice.model_name"/></b> <b>
<i class="fa fa-caret-right"/> <t t-esc="choice.model_name" />
<b><t t-esc="choice.description"/></b> </b>
<i class="fa fa-caret-right" />
<b>
<t t-esc="choice.description" />
</b>
</li> </li>
</t> </t>
</t> </t>
</ul> </ul>
</div> </div>
</t> </t>
<!-- ModelList --> <!-- ModelList -->
<t t-name="bi_view_editor.ModelList"> <t t-name="bi_view_editor.ModelList">
<div> <div>
<div class="search-bar"> <div class="search-bar">
<span class="fa fa-search"></span> <span class="fa fa-search" />
<input type="text" class="search-bar" /> <input type="text" class="search-bar" />
</div> </div>
<div class="class-list"> <div class="class-list">
</div> </div>
</div> </div>
</t> </t>
<!-- ModelListItem --> <!-- ModelListItem -->
<t t-name="bi_view_editor.ModelListItem"> <t t-name="bi_view_editor.ModelListItem">
<div class="class-container"> <div class="class-container">
<div class="class" t-attf-title="#{model}" t-attf-data-id="#{id}"><t t-esc="name"/></div> <div class="class" t-attf-title="#{model}" t-attf-data-id="#{id}">
<t t-esc="name" />
</div>
</div> </div>
</t> </t>
<!-- ModelListFieldItem--> <!-- ModelListFieldItem-->
<t t-name="bi_view_editor.ModelListFieldItem"> <t t-name="bi_view_editor.ModelListFieldItem">
<div class="field" t-attf-title="#{name}" t-attf-data-id="#{name}"><t t-esc="description"/></div> <div class="field" t-attf-title="#{name}" t-attf-data-id="#{name}">
<t t-esc="description" />
</div>
</t> </t>
<!-- FieldList --> <!-- FieldList -->
<t t-name="bi_view_editor.FieldList"> <t t-name="bi_view_editor.FieldList">
<div> <div>
@ -85,7 +102,7 @@
<th>Name</th> <th>Name</th>
<th>Model</th> <th>Model</th>
<th>Options</th> <th>Options</th>
<th></th> <th />
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -93,90 +110,150 @@
</table> </table>
</div> </div>
</t> </t>
<!-- FieldContextMenu --> <!-- FieldContextMenu -->
<t t-name="bi_view_editor.FieldList.FieldContextMenu"> <t t-name="bi_view_editor.FieldList.FieldContextMenu">
<ul class="context-menu d-none"> <ul class="context-menu d-none">
<li> <li>
<div class="checkbox"> <div class="checkbox">
<label> <label>
<input type="checkbox" data-for="column" class="checkbox-column"/> Column <input
type="checkbox"
data-for="column"
class="checkbox-column"
/> Column
</label> </label>
</div> </div>
</li> </li>
<li> <li>
<div class="checkbox"> <div class="checkbox">
<label> <label>
<input type="checkbox" data-for="row" class="checkbox-row"/> Row <input
type="checkbox"
data-for="row"
class="checkbox-row"
/> Row
</label> </label>
</div> </div>
</li> </li>
<li> <li>
<div class="checkbox"> <div class="checkbox">
<label> <label>
<input type="checkbox" data-for="measure" class="checkbox-measure"/> Measure <input
type="checkbox"
data-for="measure"
class="checkbox-measure"
/> Measure
</label> </label>
</div> </div>
</li> </li>
<li> <li>
<div class="checkbox"> <div class="checkbox">
<label> <label>
<input type="checkbox" data-for="list" class="checkbox-list"/> List <input
type="checkbox"
data-for="list"
class="checkbox-list"
/> List
</label> </label>
</div> </div>
</li> </li>
</ul> </ul>
</t> </t>
<!-- JoinContextMenu --> <!-- JoinContextMenu -->
<t t-name="bi_view_editor.FieldList.JoinContextMenu"> <t t-name="bi_view_editor.FieldList.JoinContextMenu">
<ul class="context-menu d-none"> <ul class="context-menu d-none">
<li> <li>
<div class="checkbox"> <div class="checkbox">
<label> <label>
<input type="checkbox" data-for="join_left" class="checkbox-join-left"/> Join Left <input
type="checkbox"
data-for="join_left"
class="checkbox-join-left"
/> Join Left
</label> </label>
</div> </div>
</li> </li>
</ul> </ul>
</t> </t>
<!-- FieldListItem --> <!-- FieldListItem -->
<t t-name="bi_view_editor.FieldListItem"> <t t-name="bi_view_editor.FieldListItem">
<tr t-attf-data-id="#{field._id}" class="field-node"> <tr t-attf-data-id="#{field._id}" class="field-node">
<td> <td>
<input t-attf-data-id="#{field._id}" t-attf-title="#{field.model_name} (#{field.model})" class="form-control input-sm" type="text" name="description" t-attf-value="#{field.description}"/> <input
t-attf-data-id="#{field._id}"
t-attf-title="#{field.model_name} (#{field.model})"
class="form-control input-sm"
type="text"
name="description"
t-attf-value="#{field.description}"
/>
</td> </td>
<td> <td>
<t t-esc="field.model_name" /> <t t-esc="field.model_name" />
</td> </td>
<td> <td>
<span data-for="column" t-attf-class="#{field.column and 'fa fa-columns' or 'fa fa-columns d-none'}" title='Column'></span> <span
<span data-for="row" t-attf-class="#{field.row and 'fa fa-bars' or 'fa fa-bars d-none'}" title='Row'></span> data-for="column"
<span data-for="measure" t-attf-class="#{field.measure and 'fa fa-bar-chart-o' or 'fa fa-bar-chart-o d-none'}" title='Measure'></span> t-attf-class="#{field.column and 'fa fa-columns' or 'fa fa-columns d-none'}"
<span data-for="list" t-attf-class="#{field.list and 'fa fa-list' or 'fa fa-list d-none'}" title='List'></span> title='Column'
/>
<span
data-for="row"
t-attf-class="#{field.row and 'fa fa-bars' or 'fa fa-bars d-none'}"
title='Row'
/>
<span
data-for="measure"
t-attf-class="#{field.measure and 'fa fa-bar-chart-o' or 'fa fa-bar-chart-o d-none'}"
title='Measure'
/>
<span
data-for="list"
t-attf-class="#{field.list and 'fa fa-list' or 'fa fa-list d-none'}"
title='List'
/>
</td> </td>
<td> <td>
<span t-attf-data-id="#{field._id}" class="delete-button fa fa-trash-o"/> <span
t-attf-data-id="#{field._id}"
class="delete-button fa fa-trash-o"
/>
</td> </td>
</tr> </tr>
</t> </t>
<t t-name="bi_view_editor.JoinListItem"> <t t-name="bi_view_editor.JoinListItem">
<tr t-attf-data-id="#{field._id}" class="join-node"> <tr t-attf-data-id="#{field._id}" class="join-node">
<td colspan="4"> <td colspan="4">
<input class="d-none" type="text" name="description" t-attf-value="#{field.description}"/> <input
class="d-none"
type="text"
name="description"
t-attf-value="#{field.description}"
/>
<t t-if="field.join_node > field.table_alias"> <t t-if="field.join_node > field.table_alias">
<b><t t-esc="field.model_name" /></b> <b>
<i class="fa fa-caret-right"/> <t t-esc="field.model_name" />
<small><t t-esc="field.description" /></small> </b>
<i class="fa fa-caret-right" />
<small>
<t t-esc="field.description" />
</small>
</t> </t>
<t t-else=""> <t t-else="">
<small><t t-esc="field.description" /></small> <small>
<i class="fa fa-caret-left"/> <t t-esc="field.description" />
<b><t t-esc="field.model_name" /></b> </small>
<i class="fa fa-caret-left" />
<b>
<t t-esc="field.model_name" />
</b>
</t> </t>
<span t-attf-class="#{!field.join_left and 'd-none' or ''}" data-for="join_left"><i>(join left)</i></span> <span
t-attf-class="#{!field.join_left and 'd-none' or ''}"
data-for="join_left"
>
<i>(join left)</i>
</span>
</td> </td>
</tr> </tr>
</t> </t>

View File

@ -1,15 +1,28 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8" ?>
<odoo> <odoo>
<template
<template id="assets_backend" name="bi_view_editor assets" inherit_id="web.assets_backend"> id="assets_backend"
name="bi_view_editor assets"
inherit_id="web.assets_backend"
>
<xpath expr="." position="inside"> <xpath expr="." position="inside">
<link rel="stylesheet" href="/bi_view_editor/static/src/css/bve.css"/> <link rel="stylesheet" href="/bi_view_editor/static/src/css/bve.css" />
<script
<script type="text/javascript" src="/bi_view_editor/static/src/js/bi_view_editor.js"></script> type="text/javascript"
<script type="text/javascript" src="/bi_view_editor/static/src/js/bi_view_editor.JoinNodeDialog.js"></script> src="/bi_view_editor/static/src/js/bi_view_editor.js"
<script type="text/javascript" src="/bi_view_editor/static/src/js/bi_view_editor.ModelList.js"></script> />
<script type="text/javascript" src="/bi_view_editor/static/src/js/bi_view_editor.FieldList.js"></script> <script
type="text/javascript"
src="/bi_view_editor/static/src/js/bi_view_editor.JoinNodeDialog.js"
/>
<script
type="text/javascript"
src="/bi_view_editor/static/src/js/bi_view_editor.ModelList.js"
/>
<script
type="text/javascript"
src="/bi_view_editor/static/src/js/bi_view_editor.FieldList.js"
/>
</xpath> </xpath>
</template> </template>
</odoo> </odoo>

View File

@ -4,181 +4,186 @@
import json import json
import odoo import odoo
from odoo.exceptions import UserError, ValidationError
from odoo.tests.common import TransactionCase from odoo.tests.common import TransactionCase
from odoo.tools import mute_logger from odoo.tools import mute_logger
from odoo.exceptions import UserError, ValidationError
from ..hooks import post_load, uninstall_hook from ..hooks import post_load, uninstall_hook
class TestBiViewEditor(TransactionCase): class TestBiViewEditor(TransactionCase):
def setUp(self): def setUp(self):
def _get_models(model_name_list): def _get_models(model_name_list):
return (self.env['ir.model'].search([ return (
('model', '=', name) self.env["ir.model"].search([("model", "=", name)])
]) for name in model_name_list) for name in model_name_list
)
def _get_fields(model_field_list): def _get_fields(model_field_list):
return (self.env['ir.model.fields'].search([ return (
('model', '=', model_field[0]), self.env["ir.model.fields"].search(
('name', '=', model_field[1]) [("model", "=", model_field[0]), ("name", "=", model_field[1])],
], limit=1) for model_field in model_field_list) limit=1,
)
for model_field in model_field_list
)
def get_new_field(self): def get_new_field(self):
return { return {
'model_id': self.partner_model.id, "model_id": self.partner_model.id,
'name': self.partner_field_name, "name": self.partner_field_name,
'id': self.partner_field.id, "id": self.partner_field.id,
'model': self.partner_model_name, "model": self.partner_model_name,
'type': self.partner_field.ttype, "type": self.partner_field.ttype,
'model_name': self.partner_model.name, "model_name": self.partner_model.name,
'description': self.partner_field.field_description "description": self.partner_field.field_description,
} }
super().setUp() super().setUp()
self.partner_model_name = 'res.partner' self.partner_model_name = "res.partner"
self.partner_field_name = 'name' self.partner_field_name = "name"
self.partner_company_field_name = 'company_id' self.partner_company_field_name = "company_id"
self.company_model_name = 'res.company' self.company_model_name = "res.company"
self.company_field_name = 'name' self.company_field_name = "name"
self.bi_view1 = None self.bi_view1 = None
self.partner_model, self.company_model = _get_models( self.partner_model, self.company_model = _get_models(
[self.partner_model_name, self.company_model_name]) [self.partner_model_name, self.company_model_name]
)
(self.partner_field, (
self.partner_company_field, self.partner_field,
self.company_field) = _get_fields([ self.partner_company_field,
(self.partner_model_name, self.partner_field_name), self.company_field,
(self.partner_model_name, self.partner_company_field_name), ) = _get_fields(
(self.company_model_name, self.company_field_name)]) [
(self.partner_model_name, self.partner_field_name),
(self.partner_model_name, self.partner_company_field_name),
(self.company_model_name, self.company_field_name),
]
)
self.data = [{ self.data = [
'model_id': self.partner_model.id, {
'model_name': self.partner_model.name, "model_id": self.partner_model.id,
'model': self.partner_model_name, "model_name": self.partner_model.name,
'type': self.partner_field.ttype, "model": self.partner_model_name,
'id': self.partner_field.id, "type": self.partner_field.ttype,
'description': self.partner_field.field_description, "id": self.partner_field.id,
'table_alias': 't0', "description": self.partner_field.field_description,
'row': 0, "table_alias": "t0",
'column': 1, "row": 0,
'list': 1, "column": 1,
'measure': 0 "list": 1,
}, { "measure": 0,
'model_id': self.partner_model.id, },
'table_alias': 't0', {
'relation': self.company_model_name, "model_id": self.partner_model.id,
'model': self.partner_model_name, "table_alias": "t0",
'model_name': self.partner_model.name, "relation": self.company_model_name,
'type': self.partner_company_field.ttype, "model": self.partner_model_name,
'id': self.partner_company_field.id, "model_name": self.partner_model.name,
'join_node': 't1', "type": self.partner_company_field.ttype,
'description': self.partner_company_field.field_description, "id": self.partner_company_field.id,
'row': 0, "join_node": "t1",
'column': 0, "description": self.partner_company_field.field_description,
'list': 1, "row": 0,
'measure': 0 "column": 0,
}, { "list": 1,
'model_id': self.company_model.id, "measure": 0,
'model_name': self.company_model.name, },
'model': self.company_model_name, {
'type': self.company_field.ttype, "model_id": self.company_model.id,
'id': self.company_field.id, "model_name": self.company_model.name,
'description': self.company_field.field_description, "model": self.company_model_name,
'table_alias': 't1', "type": self.company_field.ttype,
'row': 1, "id": self.company_field.id,
'column': 0, "description": self.company_field.field_description,
'list': 0, "table_alias": "t1",
'measure': 0 "row": 1,
}] "column": 0,
self.bi_view1_vals = { "list": 0,
'state': 'draft', "measure": 0,
'data': json.dumps(self.data) },
} ]
self.bi_view1_vals = {"state": "draft", "data": json.dumps(self.data)}
self.new_field = get_new_field(self) self.new_field = get_new_field(self)
def test_01_get_fields(self): def test_01_get_fields(self):
fields = self.env['ir.model'].get_fields(self.partner_model.id) fields = self.env["ir.model"].get_fields(self.partner_model.id)
self.assertIsInstance(fields, list) self.assertIsInstance(fields, list)
self.assertGreater(len(fields), 0) self.assertGreater(len(fields), 0)
def test_02_get_join_nodes(self): def test_02_get_join_nodes(self):
field_res_users = self.env['ir.model.fields'].search([ field_res_users = self.env["ir.model.fields"].search(
('name', '=', 'login'), [("name", "=", "login"), ("model", "=", "res.users")], limit=1
('model', '=', 'res.users') )
], limit=1) field_data = [
field_data = [{ {
'model_id': field_res_users.model_id.id, "model_id": field_res_users.model_id.id,
'name': 'login', "name": "login",
'column': False, "column": False,
'table_alias': 't0', "table_alias": "t0",
'measure': False, "measure": False,
'id': field_res_users.id, "id": field_res_users.id,
'model': 'res.users', "model": "res.users",
'row': False, "row": False,
'type': 'char', "type": "char",
'model_name': 'Users', "model_name": "Users",
'description': 'Login' "description": "Login",
}] }
]
new_field = self.new_field new_field = self.new_field
nodes = self.env['ir.model'].get_join_nodes(field_data, new_field) nodes = self.env["ir.model"].get_join_nodes(field_data, new_field)
self.assertIsInstance(nodes, list) self.assertIsInstance(nodes, list)
self.assertGreater(len(nodes), 0) self.assertGreater(len(nodes), 0)
def test_03_get_join_nodes(self): def test_03_get_join_nodes(self):
new_field = self.new_field new_field = self.new_field
nodes = self.env['ir.model'].get_join_nodes([], new_field) nodes = self.env["ir.model"].get_join_nodes([], new_field)
self.assertIsInstance(nodes, list) self.assertIsInstance(nodes, list)
self.assertEqual(len(nodes), 0) self.assertEqual(len(nodes), 0)
def test_04_get_related_models(self): def test_04_get_related_models(self):
all_models = self.env['ir.model'].get_models() all_models = self.env["ir.model"].get_models()
self.assertIsInstance(all_models, list) self.assertIsInstance(all_models, list)
self.assertGreater(len(all_models), 0) self.assertGreater(len(all_models), 0)
related_models = self.env['ir.model'].get_models({ related_models = self.env["ir.model"].get_models(
't0': self.partner_model.id, {"t0": self.partner_model.id, "t1": self.company_model.id}
't1': self.company_model.id )
})
self.assertIsInstance(related_models, list) self.assertIsInstance(related_models, list)
self.assertGreater(len(related_models), 0) self.assertGreater(len(related_models), 0)
def test_05_create_copy_view(self): def test_05_create_copy_view(self):
vals = self.bi_view1_vals vals = self.bi_view1_vals
vals.update({'name': 'Test View1'}) vals.update({"name": "Test View1"})
# create # create
bi_view1 = self.env['bve.view'].create(vals) bi_view1 = self.env["bve.view"].create(vals)
self.assertIsNotNone(bi_view1) self.assertIsNotNone(bi_view1)
self.assertEqual(len(bi_view1), 1) self.assertEqual(len(bi_view1), 1)
self.assertEqual(bi_view1.state, 'draft') self.assertEqual(bi_view1.state, "draft")
# copy # copy
bi_view2 = bi_view1.copy() bi_view2 = bi_view1.copy()
self.assertEqual(bi_view2.name, 'Test View1 (copy)') self.assertEqual(bi_view2.name, "Test View1 (copy)")
def test_06_create_group_bve_object(self): def test_06_create_group_bve_object(self):
vals = self.bi_view1_vals vals = self.bi_view1_vals
employees_group = self.env.ref('base.group_user') employees_group = self.env.ref("base.group_user")
vals.update({ vals.update(
'name': 'Test View2', {"name": "Test View2", "group_ids": [(6, 0, [employees_group.id])],}
'group_ids': [(6, 0, [employees_group.id])], )
})
bi_view2 = self.env['bve.view'].create(vals) bi_view2 = self.env["bve.view"].create(vals)
self.assertEqual(len(bi_view2.user_ids), len(employees_group.users)) self.assertEqual(len(bi_view2.user_ids), len(employees_group.users))
def test_07_check_empty_data(self): def test_07_check_empty_data(self):
vals = { vals = {"name": "Test View Empty", "state": "draft", "data": ""}
'name': 'Test View Empty', bi_view4 = self.env["bve.view"].create(vals)
'state': 'draft',
'data': ''
}
bi_view4 = self.env['bve.view'].create(vals)
self.assertEqual(len(bi_view4), 1) self.assertEqual(len(bi_view4), 1)
self.assertTrue(bi_view4.er_diagram_image) self.assertTrue(bi_view4.er_diagram_image)
@ -187,19 +192,18 @@ class TestBiViewEditor(TransactionCase):
bi_view4.action_create() bi_view4.action_create()
def test_08_get_models(self): def test_08_get_models(self):
models = self.env['ir.model'].get_models() models = self.env["ir.model"].get_models()
self.assertIsInstance(models, list) self.assertIsInstance(models, list)
self.assertGreater(len(models), 0) self.assertGreater(len(models), 0)
@odoo.tests.tagged('post_install', '-at_install') @odoo.tests.tagged("post_install", "-at_install")
def test_09_create_open_bve_object(self): def test_09_create_open_bve_object(self):
vals = self.bi_view1_vals vals = self.bi_view1_vals
employees_group = self.env.ref('base.group_user') employees_group = self.env.ref("base.group_user")
vals.update({ vals.update(
'name': 'Test View4', {"name": "Test View4", "group_ids": [(6, 0, [employees_group.id])],}
'group_ids': [(6, 0, [employees_group.id])], )
}) bi_view = self.env["bve.view"].create(vals)
bi_view = self.env['bve.view'].create(vals)
self.assertEqual(len(bi_view), 1) self.assertEqual(len(bi_view), 1)
self.assertEqual(len(bi_view.line_ids), 3) self.assertEqual(len(bi_view.line_ids), 3)
self.assertTrue(bi_view.er_diagram_image) self.assertTrue(bi_view.er_diagram_image)
@ -223,16 +227,15 @@ class TestBiViewEditor(TransactionCase):
# create bve object # create bve object
bi_view.action_create() bi_view.action_create()
model = self.env['ir.model'].search([ model = self.env["ir.model"].search(
('model', '=', 'x_bve.testview4'), [("model", "=", "x_bve.testview4"), ("name", "=", "Test View4")]
('name', '=', 'Test View4') )
])
self.assertEqual(len(model), 1) self.assertEqual(len(model), 1)
# open view # open view
open_action = bi_view.open_view() open_action = bi_view.open_view()
self.assertEqual(isinstance(open_action, dict), True) self.assertEqual(isinstance(open_action, dict), True)
self.assertEqual(bi_view.state, 'created') self.assertEqual(bi_view.state, "created")
# try to remove view # try to remove view
with self.assertRaises(UserError): with self.assertRaises(UserError):
@ -240,65 +243,65 @@ class TestBiViewEditor(TransactionCase):
# reset to draft # reset to draft
bi_view.action_reset() bi_view.action_reset()
self.assertEqual(bi_view.state, 'draft') self.assertEqual(bi_view.state, "draft")
# remove view # remove view
bi_view.unlink() bi_view.unlink()
@odoo.tests.tagged('post_install', '-at_install') @odoo.tests.tagged("post_install", "-at_install")
def test_10_create_open_bve_object_apostrophe(self): def test_10_create_open_bve_object_apostrophe(self):
vals = self.bi_view1_vals vals = self.bi_view1_vals
vals.update({ vals.update(
'name': "Test View5", {"name": "Test View5",}
}) )
data_list = list() data_list = list()
for r in json.loads(vals['data']): for r in json.loads(vals["data"]):
r['model_name'] = "model'name" r["model_name"] = "model'name"
data_list.append(r) data_list.append(r)
new_format_data = json.dumps(data_list) new_format_data = json.dumps(data_list)
vals.update({'data': new_format_data}) vals.update({"data": new_format_data})
bi_view = self.env['bve.view'].create(vals) bi_view = self.env["bve.view"].create(vals)
self.assertEqual(len(bi_view), 1) self.assertEqual(len(bi_view), 1)
# create bve object # create bve object
bi_view.action_create() bi_view.action_create()
def test_11_clean_nodes(self): def test_11_clean_nodes(self):
data_dict1 = { data_dict1 = {
'sequence': 1, "sequence": 1,
'model_id': 74, "model_id": 74,
'id': 858, "id": 858,
'name': 'name', "name": "name",
'model_name': 'Contact', "model_name": "Contact",
'model': 'res.partner', "model": "res.partner",
'type': 'char', "type": "char",
'table_alias': 't74', "table_alias": "t74",
'description': 'Name', "description": "Name",
'row': False, "row": False,
'column': False, "column": False,
'measure': False, "measure": False,
'list': True, "list": True,
} }
data_dict2 = { data_dict2 = {
'sequence': 2, "sequence": 2,
'model_id': 74, "model_id": 74,
'id': 896, "id": 896,
'name': 'company_id', "name": "company_id",
'model_name': 'Contact', "model_name": "Contact",
'model': 'res.partner', "model": "res.partner",
'type': 'many2one', "type": "many2one",
'table_alias': 't74', "table_alias": "t74",
'description': 'Company', "description": "Company",
'row': False, "row": False,
'column': False, "column": False,
'measure': False, "measure": False,
'list': True, "list": True,
'join_node': 't83', "join_node": "t83",
'relation': 'res.company', "relation": "res.company",
'join_left': False "join_left": False,
} }
old_data = json.dumps([data_dict1, data_dict2]) old_data = json.dumps([data_dict1, data_dict2])
new_data = self.env['bve.view'].get_clean_list(old_data) new_data = self.env["bve.view"].get_clean_list(old_data)
new_data_dict = json.loads(new_data) new_data_dict = json.loads(new_data)
self.assertEqual(len(new_data_dict), 1) self.assertEqual(len(new_data_dict), 1)
for key in data_dict1.keys(): for key in data_dict1.keys():
@ -306,27 +309,25 @@ class TestBiViewEditor(TransactionCase):
def test_12_check_groups(self): def test_12_check_groups(self):
vals = self.bi_view1_vals vals = self.bi_view1_vals
group_system = self.env.ref('base.group_system') group_system = self.env.ref("base.group_system")
vals.update({ vals.update(
'name': 'Test View1', {"name": "Test View1", "group_ids": [(6, 0, [group_system.id])],}
'group_ids': [(6, 0, [group_system.id])], )
}) bi_view1 = self.env["bve.view"].create(vals)
bi_view1 = self.env['bve.view'].create(vals)
with self.assertRaises(UserError): with self.assertRaises(UserError):
bi_view1.action_create() bi_view1.action_create()
def test_13_check_lines_missing_model(self): def test_13_check_lines_missing_model(self):
vals = self.bi_view1_vals vals = self.bi_view1_vals
group_user = self.env.ref('base.group_user') group_user = self.env.ref("base.group_user")
vals.update({ vals.update(
'name': 'Test View1', {"name": "Test View1", "group_ids": [(6, 0, [group_user.id])],}
'group_ids': [(6, 0, [group_user.id])], )
}) bi_view1 = self.env["bve.view"].create(vals)
bi_view1 = self.env['bve.view'].create(vals)
for line in bi_view1.line_ids: for line in bi_view1.line_ids:
self.assertTrue(line.model_id) self.assertTrue(line.model_id)
self.assertTrue(line.model_name) self.assertTrue(line.model_name)
self.env.cr.execute('UPDATE bve_view_line SET model_id = null') self.env.cr.execute("UPDATE bve_view_line SET model_id = null")
bi_view1.invalidate_cache() bi_view1.invalidate_cache()
for line in bi_view1.line_ids: for line in bi_view1.line_ids:
self.assertFalse(line.model_id) self.assertFalse(line.model_id)
@ -336,16 +337,15 @@ class TestBiViewEditor(TransactionCase):
def test_14_check_lines_missing_fieldl(self): def test_14_check_lines_missing_fieldl(self):
vals = self.bi_view1_vals vals = self.bi_view1_vals
group_user = self.env.ref('base.group_user') group_user = self.env.ref("base.group_user")
vals.update({ vals.update(
'name': 'Test View1', {"name": "Test View1", "group_ids": [(6, 0, [group_user.id])],}
'group_ids': [(6, 0, [group_user.id])], )
}) bi_view1 = self.env["bve.view"].create(vals)
bi_view1 = self.env['bve.view'].create(vals)
for line in bi_view1.line_ids: for line in bi_view1.line_ids:
self.assertTrue(line.field_id) self.assertTrue(line.field_id)
self.assertTrue(line.field_name) self.assertTrue(line.field_name)
self.env.cr.execute('UPDATE bve_view_line SET field_id = null') self.env.cr.execute("UPDATE bve_view_line SET field_id = null")
bi_view1.invalidate_cache() bi_view1.invalidate_cache()
for line in bi_view1.line_ids: for line in bi_view1.line_ids:
self.assertFalse(line.field_id) self.assertFalse(line.field_id)
@ -355,8 +355,8 @@ class TestBiViewEditor(TransactionCase):
def test_15_create_lines(self): def test_15_create_lines(self):
vals = self.bi_view1_vals vals = self.bi_view1_vals
vals.update({'name': 'Test View1'}) vals.update({"name": "Test View1"})
bi_view1 = self.env['bve.view'].create(vals) bi_view1 = self.env["bve.view"].create(vals)
bi_view1._compute_serialized_data() bi_view1._compute_serialized_data()
data = json.loads(bi_view1.data) data = json.loads(bi_view1.data)
self.assertTrue(data) self.assertTrue(data)
@ -369,10 +369,10 @@ class TestBiViewEditor(TransactionCase):
uninstall_hook(self.cr, self.env) uninstall_hook(self.cr, self.env)
def test_18_action_translations(self): def test_18_action_translations(self):
self.env['res.lang'].load_lang('it_IT') self.env["res.lang"].load_lang("it_IT")
vals = self.bi_view1_vals vals = self.bi_view1_vals
vals.update({'name': 'Test View1'}) vals.update({"name": "Test View1"})
bi_view1 = self.env['bve.view'].create(vals) bi_view1 = self.env["bve.view"].create(vals)
res = bi_view1.action_translations() res = bi_view1.action_translations()
self.assertFalse(res) self.assertFalse(res)
@ -380,36 +380,38 @@ class TestBiViewEditor(TransactionCase):
res = bi_view1.action_translations() res = bi_view1.action_translations()
self.assertTrue(res) self.assertTrue(res)
@odoo.tests.tagged('post_install', '-at_install') @odoo.tests.tagged("post_install", "-at_install")
def test_19_field_selection(self): def test_19_field_selection(self):
field = self.env['ir.model.fields'].search([ field = self.env["ir.model.fields"].search(
('model', '=', self.company_model_name), [
('name', '=', 'base_onboarding_company_state') ("model", "=", self.company_model_name),
], limit=1) ("name", "=", "base_onboarding_company_state"),
selection_data = [{ ],
'model_id': self.company_model.id, limit=1,
'model_name': self.company_model.name, )
'model': self.company_model_name, selection_data = [
'type': field.ttype, {
'id': field.id, "model_id": self.company_model.id,
'description': 'State of the onboarding company step', "model_name": self.company_model.name,
'table_alias': 't1', "model": self.company_model_name,
'row': 0, "type": field.ttype,
'column': 0, "id": field.id,
'list': 1, "description": "State of the onboarding company step",
'measure': 0 "table_alias": "t1",
}] "row": 0,
vals = { "column": 0,
'state': 'draft', "list": 1,
'data': json.dumps(self.data + selection_data) "measure": 0,
} }
]
vals = {"state": "draft", "data": json.dumps(self.data + selection_data)}
vals.update({'name': 'Test View6'}) vals.update({"name": "Test View6"})
bi_view1 = self.env['bve.view'].create(vals) bi_view1 = self.env["bve.view"].create(vals)
bi_view1.action_create() bi_view1.action_create()
self.assertEqual(len(bi_view1.line_ids), 4) self.assertEqual(len(bi_view1.line_ids), 4)
@mute_logger('odoo.sql_db') @mute_logger("odoo.sql_db")
def test_20_broken_view(self): def test_20_broken_view(self):
""" """
Create a broken query, a nice UserError should be raised. Create a broken query, a nice UserError should be raised.
@ -417,15 +419,14 @@ class TestBiViewEditor(TransactionCase):
ERROR: bad_query line in the logs. ERROR: bad_query line in the logs.
""" """
vals = self.bi_view1_vals vals = self.bi_view1_vals
vals.update({ vals.update(
'name': 'Test View broken', {"name": "Test View broken", "over_condition": "bad SQL code",}
'over_condition': 'bad SQL code', )
}) bi_view = self.env["bve.view"].create(vals)
bi_view = self.env['bve.view'].create(vals)
with self.assertRaises(UserError) as ue: with self.assertRaises(UserError) as ue:
bi_view.action_create() bi_view.action_create()
self.assertEqual(bi_view.state, 'draft') self.assertEqual(bi_view.state, "draft")
self.assertIn(bi_view.over_condition, str(ue.exception)) self.assertIn(bi_view.over_condition, str(ue.exception))
# remove view # remove view
bi_view.unlink() bi_view.unlink()

View File

@ -1,107 +1,179 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8" ?>
<odoo> <odoo>
<record id="action_bi_view_editor_translations" model="ir.actions.act_window"> <record id="action_bi_view_editor_translations" model="ir.actions.act_window">
<field name="name">Translations</field> <field name="name">Translations</field>
<field name="type">ir.actions.act_window</field> <field name="type">ir.actions.act_window</field>
<field name="res_model">ir.translation</field> <field name="res_model">ir.translation</field>
<field name="view_type">form</field> <field name="view_type">form</field>
<field name="domain">[('res_id', '=', active_record.), ('name', '=', 'ir.model.fields,field_description')]</field> <field
<field name="view_id" ref="base.view_translation_dialog_tree"/> name="domain"
>[('res_id', '=', active_record.), ('name', '=', 'ir.model.fields,field_description')]</field>
<field name="view_id" ref="base.view_translation_dialog_tree" />
</record> </record>
<record id="view_bi_view_editor_view_tree" model="ir.ui.view"> <record id="view_bi_view_editor_view_tree" model="ir.ui.view">
<field name="model">bve.view</field> <field name="model">bve.view</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<tree string="Custom BI View"> <tree string="Custom BI View">
<field name="name"/> <field name="name" />
</tree> </tree>
</field> </field>
</record> </record>
<record id="view_bi_view_editor_view_form" model="ir.ui.view"> <record id="view_bi_view_editor_view_form" model="ir.ui.view">
<field name="model">bve.view</field> <field name="model">bve.view</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<form string="Custom Object"> <form string="Custom Object">
<header> <header>
<button name="action_reset" type="object" states="created" string="Reset to Draft"/> <button
<button name="action_create" type="object" states="draft" string="Generate BI View" class="oe_highlight"/> name="action_reset"
<button name="open_view" type="object" states="created" string="Open BI View" class="oe_highlight"/> type="object"
<button name="%(base.act_menu_create)d" type="action" states="created" groups="base.group_no_one" icon="fa-align-justify" string="Create a Menu" target="new"/> states="created"
<field name="state" widget="statusbar" statusbar_visible="draft,created" statusbar_colors='{"draft":"blue","created":"blue"}'/> string="Reset to Draft"
/>
<button
name="action_create"
type="object"
states="draft"
string="Generate BI View"
class="oe_highlight"
/>
<button
name="open_view"
type="object"
states="created"
string="Open BI View"
class="oe_highlight"
/>
<button
name="%(base.act_menu_create)d"
type="action"
states="created"
groups="base.group_no_one"
icon="fa-align-justify"
string="Create a Menu"
target="new"
/>
<field
name="state"
widget="statusbar"
statusbar_visible="draft,created"
statusbar_colors='{"draft":"blue","created":"blue"}'
/>
</header> </header>
<sheet> <sheet>
<div class="oe_button_box" name="button_box"> <div class="oe_button_box" name="button_box">
<button name="action_translations" <button
type="object" name="action_translations"
states="created" type="object"
icon="fa-globe" states="created"
string="Translations" icon="fa-globe"
string="Translations"
/> />
</div> </div>
<h1> <h1>
<field name="name" attrs="{'readonly': [('state','=','created')]}" colspan="4"/> <field
name="name"
attrs="{'readonly': [('state','=','created')]}"
colspan="4"
/>
</h1> </h1>
<notebook> <notebook>
<page string="Query Builder"> <page string="Query Builder">
<group> <group>
<field name="data" widget="BVEEditor" nolabel="1" attrs="{'readonly': [('state','=','created')]}"/> <field
name="data"
widget="BVEEditor"
nolabel="1"
attrs="{'readonly': [('state','=','created')]}"
/>
</group> </group>
</page> </page>
<page string="ER Diagram" attrs="{'invisible': [('er_diagram_image','=',False)]}"> <page
string="ER Diagram"
attrs="{'invisible': [('er_diagram_image','=',False)]}"
>
<group> <group>
<field nolabel="1" name="er_diagram_image" widget="image"/> <field
nolabel="1"
name="er_diagram_image"
widget="image"
/>
</group> </group>
</page> </page>
<page string="Details"> <page string="Details">
<group> <group>
<field name="field_ids" attrs="{'readonly': [('state','=','created')]}"> <field
<tree editable="bottom" decoration-muted="in_list == False"> name="field_ids"
<field name="sequence" widget="handle"/> attrs="{'readonly': [('state','=','created')]}"
<field name="description" string="Field"/> >
<field name="model_id" readonly="1"/> <tree
<field name="table_alias"/> editable="bottom"
<field name="ttype" invisible="1"/> decoration-muted="in_list == False"
<field name="row" widget="toggle_button" attrs="{'invisible': [('ttype','in',('float', 'integer', 'monetary'))]}"/> >
<field name="column" widget="toggle_button" attrs="{'invisible': [('ttype','in',('float', 'integer', 'monetary'))]}"/> <field name="sequence" widget="handle" />
<field name="measure" widget="toggle_button" attrs="{'invisible': [('ttype','not in',('float', 'integer', 'monetary'))]}"/> <field name="description" string="Field" />
<field name="in_list" widget="boolean_toggle"/> <field name="model_id" readonly="1" />
<field name="list_attr" attrs="{'invisible': ['|',('in_list','=',False),('ttype','not in',('float', 'integer'))]}"/> <field name="table_alias" />
<field name="ttype" invisible="1" />
<field
name="row"
widget="toggle_button"
attrs="{'invisible': [('ttype','in',('float', 'integer', 'monetary'))]}"
/>
<field
name="column"
widget="toggle_button"
attrs="{'invisible': [('ttype','in',('float', 'integer', 'monetary'))]}"
/>
<field
name="measure"
widget="toggle_button"
attrs="{'invisible': [('ttype','not in',('float', 'integer', 'monetary'))]}"
/>
<field name="in_list" widget="boolean_toggle" />
<field
name="list_attr"
attrs="{'invisible': ['|',('in_list','=',False),('ttype','not in',('float', 'integer'))]}"
/>
</tree> </tree>
</field> </field>
</group> </group>
<group> <group>
<field name="relation_ids" attrs="{'readonly': [('state','=','created')]}"> <field
name="relation_ids"
attrs="{'readonly': [('state','=','created')]}"
>
<tree editable="bottom"> <tree editable="bottom">
<field name="sequence" widget="handle"/> <field name="sequence" widget="handle" />
<field name="description" string="Field"/> <field name="description" string="Field" />
<field name="model_id" readonly="1"/> <field name="model_id" readonly="1" />
<field name="table_alias"/> <field name="table_alias" />
<field name="join_model_id" readonly="1"/> <field name="join_model_id" readonly="1" />
<field name="join_node"/> <field name="join_node" />
<field name="left_join" widget="toggle_button"/> <field
name="left_join"
widget="toggle_button"
/>
</tree> </tree>
</field> </field>
</group> </group>
</page> </page>
<page string="SQL" groups="base.group_no_one"> <page string="SQL" groups="base.group_no_one">
<field name="query"/> <field name="query" />
<group> <group>
<field name="over_condition"/> <field name="over_condition" />
</group> </group>
</page> </page>
<page string="Security"> <page string="Security">
<field nolabel="1" name="group_ids" /> <field nolabel="1" name="group_ids" />
</page> </page>
<page string="Notes"> <page string="Notes">
<field name="note" nolabel="1" colspan="4"/> <field name="note" nolabel="1" colspan="4" />
</page> </page>
</notebook> </notebook>
</sheet> </sheet>
</form> </form>
</field> </field>
</record> </record>
<record id="action_bi_view_editor_view_form" model="ir.actions.act_window"> <record id="action_bi_view_editor_view_form" model="ir.actions.act_window">
<field name="name">Custom BI Views</field> <field name="name">Custom BI Views</field>
<field name="res_model">bve.view</field> <field name="res_model">bve.view</field>
@ -116,13 +188,15 @@
</p> </p>
</field> </field>
</record> </record>
<menuitem
<menuitem id="menu_bi_view_editor_custom_reports" id="menu_bi_view_editor_custom_reports"
name="Custom Reports" name="Custom Reports"
parent="base.menu_board_root" parent="base.menu_board_root"
sequence="0"/> sequence="0"
<menuitem id="menu_bi_view_editor_view" />
parent="menu_bi_view_editor_custom_reports" <menuitem
action="action_bi_view_editor_view_form"/> id="menu_bi_view_editor_view"
parent="menu_bi_view_editor_custom_reports"
action="action_bi_view_editor_view_form"
/>
</odoo> </odoo>

View File

@ -5,33 +5,37 @@ from odoo import api, models
class WizardModelMenuCreate(models.TransientModel): class WizardModelMenuCreate(models.TransientModel):
_inherit = 'wizard.ir.model.menu.create' _inherit = "wizard.ir.model.menu.create"
def menu_create(self): def menu_create(self):
if self.env.context.get('active_model') == 'bve.view': if self.env.context.get("active_model") == "bve.view":
self.ensure_one() self.ensure_one()
active_id = self.env.context.get('active_id') active_id = self.env.context.get("active_id")
bve_view = self.env['bve.view'].browse(active_id) bve_view = self.env["bve.view"].browse(active_id)
menu = self.env['ir.ui.menu'].create({ menu = self.env["ir.ui.menu"].create(
'name': self.name, {
'parent_id': self.menu_id.id, "name": self.name,
'action': 'ir.actions.act_window,%d' % (bve_view.action_id,) "parent_id": self.menu_id.id,
}) "action": "ir.actions.act_window,%d" % (bve_view.action_id,),
self.env['ir.model.data'].create({ }
'name': bve_view.name + ', id=' + str(menu.id), )
'noupdate': True, self.env["ir.model.data"].create(
'module': 'bi_view_editor', {
'model': 'ir.ui.menu', "name": bve_view.name + ", id=" + str(menu.id),
'res_id': menu.id, "noupdate": True,
}) "module": "bi_view_editor",
return {'type': 'ir.actions.client', 'tag': 'reload'} "model": "ir.ui.menu",
"res_id": menu.id,
}
)
return {"type": "ir.actions.client", "tag": "reload"}
return super().menu_create() return super().menu_create()
@api.model @api.model
def default_get(self, fields_list): def default_get(self, fields_list):
defaults = super().default_get(fields_list) defaults = super().default_get(fields_list)
if self.env.context.get('active_model') == 'bve.view': if self.env.context.get("active_model") == "bve.view":
active_id = self.env.context.get('active_id') active_id = self.env.context.get("active_id")
bve_view = self.env['bve.view'].browse(active_id) bve_view = self.env["bve.view"].browse(active_id)
defaults.setdefault('name', bve_view.name) defaults.setdefault("name", bve_view.name)
return defaults return defaults