From 897e5b5ba9dd43672ff7fb44018b89dae4944acf Mon Sep 17 00:00:00 2001 From: Simone Rubino Date: Fri, 15 Mar 2024 11:52:37 +0100 Subject: [PATCH] [FIX] base_comment_template: Search by model_ids.model --- .../models/base_comment_template.py | 19 +++++++++---- .../tests/test_base_comment_template.py | 27 +++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/base_comment_template/models/base_comment_template.py b/base_comment_template/models/base_comment_template.py index 37c0a5f54a..59e2c11dc3 100644 --- a/base_comment_template/models/base_comment_template.py +++ b/base_comment_template/models/base_comment_template.py @@ -2,9 +2,11 @@ # Copyright 2013-2014 Nicolas Bessi (Camptocamp SA) # Copyright 2020 NextERP Romania SRL # Copyright 2021-2022 Tecnativa - Víctor Martínez +# Copyright 2024 Simone Rubino - Aion Tech # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from odoo import _, api, fields, models from odoo.exceptions import ValidationError +from odoo.tools import Query class BaseCommentTemplate(models.Model): @@ -128,9 +130,16 @@ def name_get(self): def _search_model_ids(self, operator, value): # We cannot use model_ids.model in search() method to avoid access errors - allowed_items = ( - self.sudo() - .search([]) - .filtered(lambda x: value in x.model_ids.mapped("model")) - ) + if isinstance(value, Query): + found_models = self.env["ir.model"].sudo().browse(value) + model_names = found_models.mapped("model") + else: + model_names = [value] + + all_items = self.sudo().search([]) + allowed_items = self.browse() + for model_name in model_names: + allowed_items |= all_items.filtered( + lambda x: model_name in x.models.split(",") + ) return [("id", "in", allowed_items.ids)] diff --git a/base_comment_template/tests/test_base_comment_template.py b/base_comment_template/tests/test_base_comment_template.py index 7ad31cdc7b..ed1611f8ad 100644 --- a/base_comment_template/tests/test_base_comment_template.py +++ b/base_comment_template/tests/test_base_comment_template.py @@ -1,6 +1,8 @@ # Copyright 2020 NextERP Romania SRL # Copyright 2021 Tecnativa - Víctor Martínez +# Copyright 2024 Simone Rubino - Aion Tech # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + from odoo import Command from odoo.exceptions import ValidationError from odoo.tests import common @@ -184,3 +186,28 @@ def test_partner_commercial_fields(self): self.assertTrue( "base_comment_template_ids" in self.env["res.partner"]._commercial_fields() ) + + def test_search_model_ids_model(self): + """Searching by "model_ids.model" + finds the comments matching the search criteria.""" + # Arrange + comment_template_model = self.env["base.comment.template"] + user_ir_model = self.user_obj + user_model_name = user_ir_model.model + user_comments_by_model_ids = comment_template_model.search( + [ + ("model_ids", "=", user_model_name), + ] + ) + # pre-condition + self.assertTrue(user_comments_by_model_ids) + + # Act + user_comments_by_models_path = comment_template_model.search( + [ + ("model_ids.model", "=", user_model_name), + ] + ) + + # Assert + self.assertEqual(user_comments_by_model_ids, user_comments_by_models_path)