Skip to content

Commit

Permalink
10.0 imp bi sql editor (OCA#1)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
legalsylvain authored and $name committed Oct 29, 2018
1 parent ac1996b commit 2a4ac51
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 24 deletions.
74 changes: 57 additions & 17 deletions bi_sql_editor/models/bi_sql_view.py
Expand Up @@ -31,6 +31,7 @@ def _auto_end(self):

class BiSQLView(models.Model):
_name = 'bi.sql.view'
_order = 'sequence'
_inherit = ['sql.request.mixin']

_sql_prefix = 'x_bi_sql_view_'
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -175,6 +200,14 @@ def onchange_group_ids(self):
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):
Expand Down Expand Up @@ -399,21 +432,31 @@ def _prepare_action(self):
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,
'view_id': view_id,
'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()
return {
'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
Expand Down Expand Up @@ -570,18 +613,15 @@ def _refresh_materialized_view_cron(self, view_ids):

@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):
Expand Down
12 changes: 11 additions & 1 deletion bi_sql_editor/models/bi_sql_view_field.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
13 changes: 7 additions & 6 deletions bi_sql_editor/views/view_bi_sql_view.xml
Expand Up @@ -11,6 +11,7 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
<field name="model">bi.sql.view</field>
<field name="arch" type="xml">
<tree decoration-info="state=='draft'" decoration-warning="state in ('sql_valid', 'model_valid')">
<field name="sequence" widget="handle"/>
<field name="name"/>
<field name="technical_name"/>
<field name="size"/>
Expand All @@ -26,7 +27,9 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
<header>
<button name="button_validate_sql_expression" type="object" states="draft"
string="Validate SQL Expression" class="oe_highlight"/>
<button name="button_set_draft" type="object" states="sql_valid,model_valid,ui_valid"
<button name="button_set_draft" type="object" states="sql_valid"
string="Set to Draft" groups="sql_request_abstract.group_sql_request_manager"/>
<button name="button_set_draft" type="object" states="model_valid,ui_valid"
string="Set to Draft" groups="sql_request_abstract.group_sql_request_manager"
confirm="Are you sure you want to set to draft this SQL View. It will delete the materialized view, and all the previous mapping realized with the columns"/>
<button name="button_preview_sql_expression" type="object" states="draft" string="Preview SQL Expression" />
Expand Down Expand Up @@ -98,11 +101,9 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
<group string="Rule Definition">
<field name="domain_force" nolabel="1" colspan="4"/>
</group>
<group>
<group string="Allowed Groups">
<field name="group_ids" nolabel="1"/>
<field name="has_group_changed" invisible="1"/>
</group>
<group string="Allowed Groups">
<field name="group_ids" nolabel="1" colspan="4"/>
<field name="has_group_changed" invisible="1"/>
</group>
</page>
<page string="Extras Information">
Expand Down

0 comments on commit 2a4ac51

Please sign in to comment.