Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][IMP] bi_sql_editor #823

Merged
merged 10 commits into from
Dec 20, 2023
Merged
Prev Previous commit
Next Next commit
[FIX] bi_sql_editor : do not allow to create model (and sql views) if…
… related model are not set on many2one fields. It prevents the error 'AttributeError: '_unknown' object has no attribute 'id''
  • Loading branch information
legalsylvain committed Dec 5, 2023
commit bbe268320abc52cd91395dc898043b5f22133383
11 changes: 10 additions & 1 deletion bi_sql_editor/models/bi_sql_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from psycopg2 import ProgrammingError

from odoo import SUPERUSER_ID, _, api, fields, models
from odoo.exceptions import UserError
from odoo.exceptions import UserError, ValidationError
from odoo.tools import sql, table_columns
from odoo.tools.safe_eval import safe_eval

Expand Down Expand Up @@ -278,6 +278,15 @@ def copy(self, default=None):
# Action Section
def button_create_sql_view_and_model(self):
for sql_view in self.filtered(lambda x: x.state == "sql_valid"):
# Check if many2one fields are correctly
bad_fields = sql_view.bi_sql_view_field_ids.filtered(
lambda x: x.ttype == "many2one" and not x.many2one_model_id.id
)
if bad_fields:
raise ValidationError(
_("Please set related models on the following fields %s")
% ",".join(bad_fields.mapped("name"))
)
# Create ORM and access
sql_view._create_model_and_fields()
sql_view._create_model_access()
Expand Down
17 changes: 16 additions & 1 deletion bi_sql_editor/tests/test_bi_sql_view.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2017 Onestein (<http://www.onestein.eu>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo.exceptions import AccessError, UserError
from odoo.exceptions import AccessError, UserError, ValidationError
from odoo.tests import tagged
from odoo.tests.common import SingleTransactionCase

Expand Down Expand Up @@ -81,3 +81,18 @@ def test_unlink(self):
copy_view.unlink()
res = self.bi_sql_view.search([("name", "=", view_name)])
self.assertEqual(len(res), 0, "View not deleted")

def test_many2one_not_found(self):
copy_view = self.view.copy(
default={"technical_name": "test_many2one_not_found"}
)

copy_view.query = "SELECT parent_id as x_weird_name_id FROM res_partner;"
copy_view.button_validate_sql_expression()
field_lines = copy_view.bi_sql_view_field_ids
self.assertEqual(len(field_lines), 1)
self.assertEqual(field_lines[0].ttype, "many2one")
self.assertEqual(field_lines[0].many2one_model_id.id, False)

with self.assertRaises(ValidationError):
copy_view.button_create_sql_view_and_model()