mirror of
https://github.com/OCA/reporting-engine.git
synced 2025-02-16 16:30:38 +02:00
[IMP] base_comment_template: Refactor code and convert to Many2Many
This commit is contained in:
45
base_comment_template/models/comment_template.py
Normal file
45
base_comment_template/models/comment_template.py
Normal file
@@ -0,0 +1,45 @@
|
||||
# Copyright 2014 Guewen Baconnier (Camptocamp SA)
|
||||
# Copyright 2013-2014 Nicolas Bessi (Camptocamp SA)
|
||||
# Copyright 2020 NextERP Romania SRL
|
||||
# Copyright 2021 Tecnativa - Víctor Martínez
|
||||
# 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
|
||||
|
||||
|
||||
class CommentTemplate(models.AbstractModel):
|
||||
_name = "comment.template"
|
||||
_description = (
|
||||
"base.comment.template to put header and footer "
|
||||
"in reports based on created comment templates"
|
||||
)
|
||||
# This field allows to set any given field that determines the source partner for
|
||||
# the comment templates downstream.
|
||||
# E.g.: other models where the partner field is called customer_id.
|
||||
_comment_template_partner_field_name = "partner_id"
|
||||
|
||||
comment_template_ids = fields.Many2many(
|
||||
compute="_compute_comment_template_ids",
|
||||
comodel_name="base.comment.template",
|
||||
string="Comment Template",
|
||||
domain=lambda self: [("model_ids.model", "=", self._name)],
|
||||
store=True,
|
||||
readonly=False,
|
||||
)
|
||||
|
||||
@api.depends(_comment_template_partner_field_name)
|
||||
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),
|
||||
]
|
||||
)
|
||||
for template in templates:
|
||||
if not template.domain or self in self.search(
|
||||
safe_eval(template.domain)
|
||||
):
|
||||
record.comment_template_ids = [(4, template.id)]
|
||||
Reference in New Issue
Block a user