From 875515cdaaaeb111f7f5129e8f70d38f8292ef46 Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Tue, 5 Dec 2023 21:34:29 +0100 Subject: [PATCH] [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'' --- bi_sql_editor/models/bi_sql_view.py | 11 ++++++++++- bi_sql_editor/tests/test_bi_sql_view.py | 17 ++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/bi_sql_editor/models/bi_sql_view.py b/bi_sql_editor/models/bi_sql_view.py index 5183f3ac7..90f8d49a0 100644 --- a/bi_sql_editor/models/bi_sql_view.py +++ b/bi_sql_editor/models/bi_sql_view.py @@ -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() diff --git a/bi_sql_editor/tests/test_bi_sql_view.py b/bi_sql_editor/tests/test_bi_sql_view.py index 5eeb81a05..4bfee18cf 100644 --- a/bi_sql_editor/tests/test_bi_sql_view.py +++ b/bi_sql_editor/tests/test_bi_sql_view.py @@ -1,7 +1,7 @@ # Copyright 2017 Onestein () # 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()