[IMP] Add test + several fixes

This commit is contained in:
aheficent
2017-07-17 17:14:49 +02:00
committed by David James
parent 8425bcd972
commit 5c5eeac34a
13 changed files with 228 additions and 107 deletions

View File

@@ -27,14 +27,14 @@ class BiSQLView(models.Model):
_STATE_SQL_EDITOR = [
('model_valid', 'SQL View and Model Created'),
('ui_valid', 'Graph, action and Menu Created'),
('ui_valid', 'Views, Action and Menu Created'),
]
technical_name = fields.Char(
string='Technical Name', required=True,
help="Suffix of the SQL view. (SQL full name will be computed and"
" prefixed by 'x_bi_sql_view_'. Should have correct"
"syntax. For more information, see https://www.postgresql.org/"
help="Suffix of the SQL view. SQL full name will be computed and"
" prefixed by 'x_bi_sql_view_'. Syntax should follow: "
"https://www.postgresql.org/"
"docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS")
view_name = fields.Char(
@@ -59,6 +59,14 @@ class BiSQLView(models.Model):
state = fields.Selection(selection_add=_STATE_SQL_EDITOR)
view_order = fields.Char(string='View Order',
required=True,
readonly=False,
states={'ui_valid': [('readonly', True)]},
default="pivot,graph,tree",
help='Comma-separated text. Possible values:'
' "graph", "pivot" or "tree"')
query = fields.Text(
help="SQL Request that will be inserted as the view. Take care to :\n"
" * set a name for all your selected fields, specially if you use"
@@ -113,6 +121,16 @@ class BiSQLView(models.Model):
rule_id = fields.Many2one(
string='Odoo Rule', comodel_name='ir.rule', readonly=True)
@api.constrains('view_order')
@api.multi
def _check_view_order(self):
for rec in self:
if rec.view_order:
for vtype in rec.view_order.split(','):
if vtype not in ('graph', 'pivot', 'tree'):
raise UserError(_(
'Only graph, pivot or tree views are supported'))
# Compute Section
@api.depends('is_materialized')
@api.multi
@@ -148,7 +166,7 @@ class BiSQLView(models.Model):
('state', 'not in', ('draft', 'sql_valid'))])
if non_draft_views:
raise UserError(_("You can only unlink draft views"))
return self.unlink()
return super(BiSQLView, self).unlink()
@api.multi
def copy(self, default=None):
@@ -185,7 +203,8 @@ class BiSQLView(models.Model):
for sql_view in self:
if sql_view.state in ('model_valid', 'ui_valid'):
# Drop SQL View (and indexes by cascade)
sql_view._drop_view()
if sql_view.is_materialized:
sql_view._drop_view()
# Drop ORM
sql_view._drop_model_and_fields()
@@ -233,7 +252,7 @@ class BiSQLView(models.Model):
'type': 'ir.actions.act_window',
'res_model': self.model_id.model,
'search_view_id': self.search_view_id.id,
'view_mode': 'graph,pivot,tree',
'view_mode': self.action_id.view_mode,
}
# Prepare Function
@@ -357,12 +376,20 @@ class BiSQLView(models.Model):
@api.multi
def _prepare_action(self):
self.ensure_one()
view_mode = self.view_order
first_view = view_mode.split(',')[0]
if first_view == 'tree':
view_id = self.tree_view_id.id
elif first_view == 'pivot':
view_id = self.pivot_view_id.id
else:
view_id = self.graph_view_id.id
return {
'name': self.name,
'res_model': self.model_id.model,
'type': 'ir.actions.act_window',
'view_mode': 'graph,pivot,tree',
'view_id': self.graph_view_id.id,
'view_mode': view_mode,
'view_id': view_id,
'search_view_id': self.search_view_id.id,
}
@@ -438,7 +465,8 @@ class BiSQLView(models.Model):
@api.multi
def _drop_model_and_fields(self):
for sql_view in self:
sql_view.model_id.unlink()
if sql_view.model_id:
sql_view.model_id.unlink()
@api.multi
def _hook_executed_request(self):
@@ -522,16 +550,17 @@ class BiSQLView(models.Model):
@api.multi
def _refresh_materialized_view(self):
for sql_view in self:
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")))
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")))
@api.multi
def _refresh_size(self):

View File

@@ -84,7 +84,7 @@ class BiSQLViewField(models.Model):
ttype = fields.Selection(
string='Field Type', selection=_TTYPE_SELECTION, help="Type of the"
" Odoo field that will be created. Let empty if you don't want to"
" Odoo field that will be created. Keep empty if you don't want to"
" create a new field. If empty, this field will not be displayed"
" neither available for search or group by function")
@@ -98,7 +98,7 @@ class BiSQLViewField(models.Model):
many2one_model_id = fields.Many2one(
comodel_name='ir.model', string='Model',
help="For 'Many2one' Odoo field.\n"
" Co Model of the field.")
" Comodel of the field.")
# Compute Section
@api.multi
@@ -174,8 +174,6 @@ class BiSQLViewField(models.Model):
'selection': self.ttype == 'selection' and self.selection or False,
'relation': self.ttype == 'many2one' and
self.many2one_model_id.model or False,
'tree_visibility': self.field_description and
'available' or 'unavailable',
}
@api.multi