[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''

This commit is contained in:
Sylvain LE GAL
2023-12-05 21:34:29 +01:00
committed by thien
parent db6c8968a2
commit 875515cdaa
2 changed files with 26 additions and 2 deletions

View File

@@ -8,7 +8,7 @@ from datetime import datetime, timedelta
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
@@ -272,6 +272,15 @@ class BiSQLView(models.Model):
# 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()

View File

@@ -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
@@ -81,3 +81,18 @@ class TestBiSqlViewEditor(SingleTransactionCase):
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()