[FIX] base_comment_template: Consider multi-company and partners

In a multi-company context, if you put a template with company_id set,
this template is trying to be assigned to any document, no matter which
company it belongs, as it runs in a sudo environment, or at least, you
can have several companies seen at the same time.

We add the company discriminant for avoiding this problem.

Furthermore, the previous code was adding a domain where the right
part may be `False` with the operator `in`, provoking an unexpected
behavior in the search.

This has been fixed stating directly the expected domain.

TT42591
This commit is contained in:
Pedro M. Baeza
2023-04-17 20:16:14 +02:00
parent cdb2f9b52d
commit bbd3e0a2cb
2 changed files with 18 additions and 14 deletions

View File

@@ -2,6 +2,7 @@
# Copyright 2013-2014 Nicolas Bessi (Camptocamp SA)
# Copyright 2020 NextERP Romania SRL
# Copyright 2021 Tecnativa - Víctor Martínez
# Copyright 2023 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import api, fields, models
from odoo.tools.safe_eval import safe_eval
@@ -31,17 +32,25 @@ class CommentTemplate(models.AbstractModel):
def _compute_comment_template_ids(self):
for record in self:
partner = record[self._comment_template_partner_field_name]
record.comment_template_ids = [(5,)]
templates = self.env["base.comment.template"].search(
[
("id", "in", partner.base_comment_template_ids.ids),
("model_ids.model", "=", self._name),
domain = [
"|",
("partner_ids", "=", False),
("partner_ids", "=", partner.id),
("model_ids.model", "=", self._name),
]
if "company_id" in self._fields:
domain += [
"|",
("company_id", "=", False),
("company_id", "=", record.company_id.id),
]
)
templates = self.env["base.comment.template"].search(domain)
for template in templates:
domain = safe_eval(template.domain)
if not domain or record.filtered_domain(domain):
record.comment_template_ids = [(4, template.id)]
if not templates:
record.comment_template_ids = False
def render_comment(
self, comment, engine="jinja", add_context=None, post_process=False