2021-01-05 10:18:36 +01:00
|
|
|
# Copyright 2020-2021 Tecnativa - João Marques
|
|
|
|
# Copyright 2021 Tecnativa - Pedro M. Baeza
|
2022-12-14 11:32:59 +01:00
|
|
|
# Copyright 2022 Moduon - Eduardo de Miguel
|
2021-01-05 10:18:36 +01:00
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
|
|
|
|
|
|
|
from odoo import api, models
|
|
|
|
|
|
|
|
|
|
|
|
class MailThread(models.AbstractModel):
|
|
|
|
_inherit = "mail.thread"
|
|
|
|
|
|
|
|
@api.returns("mail.message", lambda value: value.id)
|
|
|
|
def message_post(
|
|
|
|
self,
|
2021-08-12 12:58:35 +02:00
|
|
|
*,
|
2021-01-05 10:18:36 +01:00
|
|
|
body="",
|
|
|
|
subject=None,
|
|
|
|
message_type="notification",
|
2021-08-12 12:58:35 +02:00
|
|
|
email_from=None,
|
|
|
|
author_id=None,
|
2021-01-05 10:18:36 +01:00
|
|
|
parent_id=False,
|
2021-08-12 12:58:35 +02:00
|
|
|
subtype_xmlid=None,
|
|
|
|
subtype_id=False,
|
|
|
|
partner_ids=None,
|
2021-01-05 10:18:36 +01:00
|
|
|
attachments=None,
|
2021-08-12 12:58:35 +02:00
|
|
|
attachment_ids=None,
|
2021-01-05 10:18:36 +01:00
|
|
|
add_sign=True,
|
2021-08-12 12:58:35 +02:00
|
|
|
record_name=False,
|
2021-01-05 10:18:36 +01:00
|
|
|
**kwargs
|
|
|
|
):
|
2022-12-14 11:32:59 +01:00
|
|
|
if subtype_xmlid:
|
2022-10-19 12:45:37 +02:00
|
|
|
subtype_id = self.env["ir.model.data"]._xmlid_to_res_id(
|
2021-08-12 12:58:35 +02:00
|
|
|
subtype_xmlid,
|
2021-08-12 11:57:11 +02:00
|
|
|
raise_if_not_found=False,
|
2021-01-05 10:18:36 +01:00
|
|
|
)
|
2022-12-14 11:32:59 +01:00
|
|
|
if not subtype_id:
|
|
|
|
subtype_id = self.env["ir.model.data"]._xmlid_to_res_id("mail.mt_note")
|
2021-01-05 10:18:36 +01:00
|
|
|
if subtype_id:
|
2022-12-02 09:57:08 +01:00
|
|
|
custom_subjects = (
|
|
|
|
self.env["mail.message.custom.subject"]
|
|
|
|
.sudo()
|
|
|
|
.search(
|
|
|
|
[
|
|
|
|
("model_id.model", "=", self._name),
|
|
|
|
("subtype_ids", "=", subtype_id),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
.sudo(False)
|
2021-01-05 10:18:36 +01:00
|
|
|
)
|
|
|
|
if not subject:
|
2022-01-24 10:32:01 +01:00
|
|
|
subject = "Re: %s" % self.env["mail.message"].with_context(
|
|
|
|
default_model=self._name,
|
|
|
|
default_res_id=self.id,
|
|
|
|
)._get_record_name({})
|
2021-01-05 10:18:36 +01:00
|
|
|
for template in custom_subjects:
|
|
|
|
try:
|
|
|
|
rendered_subject_template = self.env[
|
|
|
|
"mail.template"
|
|
|
|
]._render_template(
|
2021-08-12 12:58:35 +02:00
|
|
|
template_src=template.subject_template,
|
2021-01-05 10:18:36 +01:00
|
|
|
model=self._name,
|
2021-08-12 12:58:35 +02:00
|
|
|
res_ids=[self.id],
|
|
|
|
)[
|
|
|
|
self.id
|
|
|
|
]
|
2021-01-05 10:18:36 +01:00
|
|
|
if template.position == "replace":
|
|
|
|
subject = rendered_subject_template
|
|
|
|
elif template.position == "append_before":
|
|
|
|
subject = rendered_subject_template + subject
|
|
|
|
elif template.position == "append_after":
|
|
|
|
subject += rendered_subject_template
|
|
|
|
except Exception:
|
|
|
|
rendered_subject_template = False
|
|
|
|
return super().message_post(
|
|
|
|
body=body,
|
|
|
|
subject=subject,
|
|
|
|
message_type=message_type,
|
2021-08-12 12:58:35 +02:00
|
|
|
email_from=email_from,
|
|
|
|
author_id=author_id,
|
2021-01-05 10:18:36 +01:00
|
|
|
parent_id=parent_id,
|
2021-08-12 12:58:35 +02:00
|
|
|
subtype_xmlid=subtype_xmlid,
|
|
|
|
subtype_id=subtype_id,
|
|
|
|
partner_ids=partner_ids,
|
2021-01-05 10:18:36 +01:00
|
|
|
attachments=attachments,
|
2021-08-12 12:58:35 +02:00
|
|
|
attachment_ids=attachment_ids,
|
2021-01-05 10:18:36 +01:00
|
|
|
add_sign=add_sign,
|
2021-08-12 12:58:35 +02:00
|
|
|
record_name=record_name,
|
|
|
|
**kwargs,
|
2021-01-05 10:18:36 +01:00
|
|
|
)
|