From 5b3dca4550136cb839e90835904ccec1ebdb0ad3 Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Mon, 23 Jul 2018 17:24:30 +0200 Subject: [PATCH] 10.0 imp bi sql editor (#1) * [IMP] is_materialized field non readonly on sql_valid state ; [FIX] block possibility to set indexes on non materialized view * [FIX] set domain_force, group_ids readonly if state > sql_valid * [IMP] better display of the field group_ids * [IMP] possibility to reorder menu items from sql views * [IMP] Do not warn user when setting sql view to draft if state is sql_valid * [REF] * [FIX] Set Date of the first execution in the action name --- bi_sql_editor/models/bi_sql_view.py | 74 +++++++++++++++++------ bi_sql_editor/models/bi_sql_view_field.py | 12 +++- bi_sql_editor/views/view_bi_sql_view.xml | 13 ++-- 3 files changed, 75 insertions(+), 24 deletions(-) diff --git a/bi_sql_editor/models/bi_sql_view.py b/bi_sql_editor/models/bi_sql_view.py index 60b937d3..8eeefbf1 100644 --- a/bi_sql_editor/models/bi_sql_view.py +++ b/bi_sql_editor/models/bi_sql_view.py @@ -31,6 +31,7 @@ class BaseModel(models.AbstractModel): class BiSQLView(models.Model): _name = 'bi.sql.view' + _order = 'sequence' _inherit = ['sql.request.mixin'] _sql_prefix = 'x_bi_sql_view_' @@ -64,7 +65,10 @@ class BiSQLView(models.Model): is_materialized = fields.Boolean( string='Is Materialized View', default=True, readonly=True, - states={'draft': [('readonly', False)]}) + states={ + 'draft': [('readonly', False)], + 'sql_valid': [('readonly', False)], + }) materialized_text = fields.Char( compute='_compute_materialized_text', store=True) @@ -94,13 +98,17 @@ class BiSQLView(models.Model): "FROM my_table") domain_force = fields.Text( - string='Extra Rule Definition', default="[]", help="Define here" - " access restriction to data.\n" + string='Extra Rule Definition', default="[]", readonly=True, + help="Define here access restriction to data.\n" " Take care to use field name prefixed by 'x_'." " A global 'ir.rule' will be created." " A typical Multi Company rule is for exemple \n" " ['|', ('x_company_id','child_of', [user.company_id.id])," - "('x_company_id','=',False)].") + "('x_company_id','=',False)].", + states={ + 'draft': [('readonly', False)], + 'sql_valid': [('readonly', False)], + }) has_group_changed = fields.Boolean(copy=False) @@ -137,6 +145,23 @@ class BiSQLView(models.Model): rule_id = fields.Many2one( string='Odoo Rule', comodel_name='ir.rule', readonly=True) + group_ids = fields.Many2many( + comodel_name='res.groups', readonly=True, states={ + 'draft': [('readonly', False)], + 'sql_valid': [('readonly', False)], + }) + + sequence = fields.Integer(string='sequence') + + # Constrains Section + @api.constrains('is_materialized') + @api.multi + def _check_index_materialized(self): + for rec in self.filtered(lambda x: not x.is_materialized): + if rec.bi_sql_view_field_ids.filtered(lambda x: x.is_index): + raise UserError(_( + 'You can not create indexes on non materialized views')) + @api.constrains('view_order') @api.multi def _check_view_order(self): @@ -175,6 +200,14 @@ class BiSQLView(models.Model): self.has_group_changed = True # Overload Section + @api.multi + def write(self, vals): + res = super(BiSQLView, self).write(vals) + if vals.get('sequence', False): + for rec in self.filtered(lambda x: x.menu_id): + rec.menu_id.sequence = rec.sequence + return res + @api.multi def unlink(self): if any(view.state not in ('draft', 'sql_valid') for view in self): @@ -399,7 +432,7 @@ class BiSQLView(models.Model): else: view_id = self.graph_view_id.id return { - 'name': self.name, + 'name': self._prepare_action_name(), 'res_model': self.model_id.model, 'type': 'ir.actions.act_window', 'view_mode': view_mode, @@ -407,6 +440,15 @@ class BiSQLView(models.Model): 'search_view_id': self.search_view_id.id, } + @api.multi + def _prepare_action_name(self): + self.ensure_one() + if not self.is_materialized: + return self.name + return "%s (%s)" % ( + self.name, + datetime.utcnow().strftime(_("%m/%d/%Y %H:%M:%S UTC"))) + @api.multi def _prepare_menu(self): self.ensure_one() @@ -414,6 +456,7 @@ class BiSQLView(models.Model): 'name': self.name, 'parent_id': self.env.ref('bi_sql_editor.menu_bi_sql_editor').id, 'action': 'ir.actions.act_window,%s' % (self.action_id.id), + 'sequence': self.sequence, } # Custom Section @@ -570,18 +613,15 @@ class BiSQLView(models.Model): @api.multi def _refresh_materialized_view(self): - for sql_view in self: - if sql_view.is_materialized: - req = "REFRESH %s VIEW %s" % ( - sql_view.materialized_text, sql_view.view_name) - self._log_execute(req) - sql_view._refresh_size() - if sql_view.action_id: - # Alter name of the action, to display last refresh - # datetime of the materialized view - sql_view.action_id.name = "%s (%s)" % ( - self.name, - datetime.utcnow().strftime(_("%m/%d/%Y %H:%M:%S UTC"))) + for sql_view in self.filtered(lambda x: x.is_materialized): + req = "REFRESH %s VIEW %s" % ( + sql_view.materialized_text, sql_view.view_name) + self._log_execute(req) + sql_view._refresh_size() + if sql_view.action_id: + # Alter name of the action, to display last refresh + # datetime of the materialized view + sql_view.action_id.name = sql_view._prepare_action_name() @api.multi def _refresh_size(self): diff --git a/bi_sql_editor/models/bi_sql_view_field.py b/bi_sql_editor/models/bi_sql_view_field.py index 3e41de09..966ad6a6 100644 --- a/bi_sql_editor/models/bi_sql_view_field.py +++ b/bi_sql_editor/models/bi_sql_view_field.py @@ -5,7 +5,8 @@ import re -from odoo import api, fields, models +from odoo import _, api, fields, models +from odoo.exceptions import UserError class BiSQLViewField(models.Model): @@ -100,6 +101,15 @@ class BiSQLViewField(models.Model): help="For 'Many2one' Odoo field.\n" " Comodel of the field.") + # Constrains Section + @api.constrains('is_index') + @api.multi + def _check_index_materialized(self): + for rec in self.filtered(lambda x: x.is_index): + if not rec.bi_sql_view_id.is_materialized: + raise UserError(_( + 'You can not create indexes on non materialized views')) + # Compute Section @api.multi def _compute_index_name(self): diff --git a/bi_sql_editor/views/view_bi_sql_view.xml b/bi_sql_editor/views/view_bi_sql_view.xml index 8c76cdd7..b27e73b8 100644 --- a/bi_sql_editor/views/view_bi_sql_view.xml +++ b/bi_sql_editor/views/view_bi_sql_view.xml @@ -11,6 +11,7 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). bi.sql.view + @@ -26,7 +27,9 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).