Skip to content

Commit 8e0edda

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

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

base_comment_template/models/base_comment_template.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
66
from odoo import _, api, fields, models
77
from odoo.exceptions import ValidationError
8+
from odoo.tools import Query
89

910

1011
class BaseCommentTemplate(models.Model):
@@ -128,9 +129,15 @@ def name_get(self):
128129

129130
def _search_model_ids(self, operator, value):
130131
# 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-
)
132+
all_items = self.sudo().search([])
133+
if isinstance(value, Query):
134+
found_models = self.env["ir.model"].browse(value)
135+
allowed_items = self.browse()
136+
for found_model in found_models:
137+
model_name = found_model.model
138+
allowed_items |= all_items.filtered(
139+
lambda x: model_name in x.models.split(",")
140+
)
141+
else:
142+
allowed_items = all_items.filtered(lambda x: value in x.models.split(","))
136143
return [("id", "in", allowed_items.ids)]

base_comment_template/tests/test_base_comment_template.py

+24
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,27 @@ def test_partner_commercial_fields(self):
184184
self.assertTrue(
185185
"base_comment_template_ids" in self.env["res.partner"]._commercial_fields()
186186
)
187+
188+
def test_search_model_ids_model(self):
189+
"""Searching by "model_ids.model"
190+
finds the comments matching the search criteria."""
191+
# Arrange
192+
user_ir_model = self.user_obj
193+
user_model_name = user_ir_model.model
194+
user_comments_by_model_ids = self.env["base.comment.template"].search(
195+
[
196+
("model_ids", "=", user_model_name),
197+
]
198+
)
199+
# pre-condition
200+
self.assertTrue(user_comments_by_model_ids)
201+
202+
# Act
203+
user_comments_by_models_path = self.env["base.comment.template"].search(
204+
[
205+
("model_ids.model", "=", user_model_name),
206+
]
207+
)
208+
209+
# Assert
210+
self.assertEqual(user_comments_by_model_ids, user_comments_by_models_path)

0 commit comments

Comments
 (0)