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 30f7241
Show file tree
Hide file tree
Showing 2 changed files with 40 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 @@ -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):
Expand Down Expand Up @@ -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)]
26 changes: 26 additions & 0 deletions base_comment_template/tests/test_base_comment_template.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 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
Expand Down Expand Up @@ -184,3 +185,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)

0 comments on commit 30f7241

Please sign in to comment.