Skip to content

Commit

Permalink
[FIX] base_comment_template: Search by model_ids.model
Browse files Browse the repository at this point in the history
  • Loading branch information
SirAionTech committed Mar 15, 2024
1 parent 571bc0e commit 1c521f3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
19 changes: 14 additions & 5 deletions base_comment_template/models/base_comment_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# 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):
Expand Down Expand Up @@ -128,9 +129,17 @@ 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"))
)
all_items = self.sudo().search([])
if isinstance(value, Query):
found_models = self.env["ir.model"].browse(value)
allowed_items = self.browse()
for found_model in found_models:
model_name = found_model.model
allowed_items |= all_items.filtered(
lambda x: model_name in x.model_ids.mapped("model")
)
else:
allowed_items = all_items.filtered(
lambda x: value in x.model_ids.mapped("model")
)
return [("id", "in", allowed_items.ids)]
24 changes: 24 additions & 0 deletions base_comment_template/tests/test_base_comment_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,27 @@ 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
user_ir_model = self.user_obj
user_model_name = user_ir_model.model
user_comments_by_model_ids = self.env["base.comment.template"].search(
[
("model_ids", "=", user_model_name),
]
)
# pre-condition
self.assertTrue(user_comments_by_model_ids)

# Act
user_comments_by_models_path = self.env["base.comment.template"].search(
[
("model_ids.model", "=", user_model_name),
]
)

# Assert
self.assertEqual(user_comments_by_model_ids, user_comments_by_models_path)

0 comments on commit 1c521f3

Please sign in to comment.