Browse Source

bi_view_editor: Better error message if the query is not correct

myc-14.0-py3o
SimoRubi 5 years ago
committed by Andrea
parent
commit
2d17441547
  1. 12
      bi_view_editor/models/bve_view.py
  2. 1
      bi_view_editor/readme/ROADMAP.rst
  3. 22
      bi_view_editor/tests/test_bi_view.py

12
bi_view_editor/models/bve_view.py

@ -295,8 +295,16 @@ class BveView(models.Model):
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
self.env.cr.execute('CREATE or REPLACE VIEW %s as (%s)', (
AsIs(view_name), AsIs(query), ))
try:
with self.env.cr.savepoint():
self.env.cr.execute('CREATE or REPLACE VIEW %s as (%s)', (
AsIs(view_name), AsIs(query), ))
except Exception as e:
raise UserError(
_("Error creating the view '{query}':\n{error}")
.format(
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):

1
bi_view_editor/readme/ROADMAP.rst

@ -5,4 +5,3 @@
* Data the user has no access to (e.g. in a multi company situation) can be * Data the user has no access to (e.g. in a multi company situation) can be
viewed by making a view. Would be nice if models available to select when viewed by making a view. Would be nice if models available to select when
creating a view are limited to the ones that have intersecting groups. creating a view are limited to the ones that have intersecting groups.
* Raise a warning in case the SQL query is not correct.

22
bi_view_editor/tests/test_bi_view.py

@ -5,6 +5,7 @@ import json
import odoo import odoo
from odoo.tests.common import TransactionCase from odoo.tests.common import TransactionCase
from odoo.tools import mute_logger
from odoo.exceptions import UserError, ValidationError from odoo.exceptions import UserError, ValidationError
from ..hooks import post_load, uninstall_hook from ..hooks import post_load, uninstall_hook
@ -407,3 +408,24 @@ class TestBiViewEditor(TransactionCase):
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')
def test_20_broken_view(self):
"""
Create a broken query, a nice UserError should be raised.
odoo.sql_db logger is muted to avoid the
ERROR: bad_query line in the logs.
"""
vals = self.bi_view1_vals
vals.update({
'name': 'Test View broken',
'over_condition': 'bad SQL code',
})
bi_view = self.env['bve.view'].create(vals)
with self.assertRaises(UserError) as ue:
bi_view.action_create()
self.assertEqual(bi_view.state, 'draft')
self.assertIn(bi_view.over_condition, str(ue.exception))
# remove view
bi_view.unlink()
Loading…
Cancel
Save