Skip to content

Commit 897e5b5

Browse files
committed
[FIX] base_comment_template: Search by model_ids.model
1 parent 571bc0e commit 897e5b5

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

base_comment_template/models/base_comment_template.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
# Copyright 2013-2014 Nicolas Bessi (Camptocamp SA)
33
# Copyright 2020 NextERP Romania SRL
44
# Copyright 2021-2022 Tecnativa - Víctor Martínez
5+
# Copyright 2024 Simone Rubino - Aion Tech
56
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
67
from odoo import _, api, fields, models
78
from odoo.exceptions import ValidationError
9+
from odoo.tools import Query
810

911

1012
class BaseCommentTemplate(models.Model):
@@ -128,9 +130,16 @@ def name_get(self):
128130

129131
def _search_model_ids(self, operator, value):
130132
# We cannot use model_ids.model in search() method to avoid access errors
131-
allowed_items = (
132-
self.sudo()
133-
.search([])
134-
.filtered(lambda x: value in x.model_ids.mapped("model"))
135-
)
133+
if isinstance(value, Query):
134+
found_models = self.env["ir.model"].sudo().browse(value)
135+
model_names = found_models.mapped("model")
136+
else:
137+
model_names = [value]
138+
139+
all_items = self.sudo().search([])
140+
allowed_items = self.browse()
141+
for model_name in model_names:
142+
allowed_items |= all_items.filtered(
143+
lambda x: model_name in x.models.split(",")
144+
)
136145
return [("id", "in", allowed_items.ids)]

base_comment_template/tests/test_base_comment_template.py

+27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright 2020 NextERP Romania SRL
22
# Copyright 2021 Tecnativa - Víctor Martínez
3+
# Copyright 2024 Simone Rubino - Aion Tech
34
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
5+
46
from odoo import Command
57
from odoo.exceptions import ValidationError
68
from odoo.tests import common
@@ -184,3 +186,28 @@ def test_partner_commercial_fields(self):
184186
self.assertTrue(
185187
"base_comment_template_ids" in self.env["res.partner"]._commercial_fields()
186188
)
189+
190+
def test_search_model_ids_model(self):
191+
"""Searching by "model_ids.model"
192+
finds the comments matching the search criteria."""
193+
# Arrange
194+
comment_template_model = self.env["base.comment.template"]
195+
user_ir_model = self.user_obj
196+
user_model_name = user_ir_model.model
197+
user_comments_by_model_ids = comment_template_model.search(
198+
[
199+
("model_ids", "=", user_model_name),
200+
]
201+
)
202+
# pre-condition
203+
self.assertTrue(user_comments_by_model_ids)
204+
205+
# Act
206+
user_comments_by_models_path = comment_template_model.search(
207+
[
208+
("model_ids.model", "=", user_model_name),
209+
]
210+
)
211+
212+
# Assert
213+
self.assertEqual(user_comments_by_model_ids, user_comments_by_models_path)

0 commit comments

Comments
 (0)