Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] base_comment_template: Search by model_ids.model #863

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions base_comment_template/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ Contributors

* Achraf Mhadhbi <[email protected]>

* `Aion Tech <https://aiontech.company/>`__:

* Simone Rubino <[email protected]>

Maintainers
~~~~~~~~~~~

Expand Down
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)]
4 changes: 4 additions & 0 deletions base_comment_template/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@
* `Bloopark systems <https://www.bloopark.de/>`_:

* Achraf Mhadhbi <[email protected]>

* `Aion Tech <https://aiontech.company/>`__:

* Simone Rubino <[email protected]>
6 changes: 6 additions & 0 deletions base_comment_template/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,13 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<li>Achraf Mhadhbi &lt;<a class="reference external" href="mailto:machraf&#64;bloopark.de">machraf&#64;bloopark.de</a>&gt;</li>
</ul>
</li>
<li><a class="reference external" href="https://aiontech.company/">Aion Tech</a>:</li>
</ul>
<blockquote>
<ul class="simple">
<li>Simone Rubino &lt;<a class="reference external" href="mailto:simone.rubino&#64;aion-tech.it">simone.rubino&#64;aion-tech.it</a>&gt;</li>
</ul>
</blockquote>
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h2>
Expand Down
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)
Loading