[MIG] mail_notification_custom_subject: Migration to 14.0

This commit is contained in:
Naglis Jonaitis 2021-08-12 13:58:35 +03:00 committed by Víctor Martínez
parent d5f0d97a9d
commit 14cc2d99be
6 changed files with 71 additions and 52 deletions

View File

@ -5,7 +5,7 @@
{
"name": "Mail Notification Custom Subject",
"summary": "Apply a custom subject to mail notifications",
"version": "13.0.1.0.0",
"version": "14.0.1.0.0",
"category": "Social Network",
"website": "https://github.com/OCA/social",
"author": "Tecnativa, Odoo Community Association (OCA)",

View File

@ -14,6 +14,7 @@ class MailMessageCustomSubject(models.Model):
string="Model",
required=True,
help="Model where this template applies",
ondelete="cascade",
)
subtype_ids = fields.Many2many(
comodel_name="mail.message.subtype",

View File

@ -11,25 +11,27 @@ class MailThread(models.AbstractModel):
@api.returns("mail.message", lambda value: value.id)
def message_post(
self,
*,
body="",
subject=None,
message_type="notification",
subtype=None,
email_from=None,
author_id=None,
parent_id=False,
subtype_xmlid=None,
subtype_id=False,
partner_ids=None,
channel_ids=None,
attachments=None,
notif_layout=False,
attachment_ids=None,
add_sign=True,
model_description=False,
mail_auto_delete=True,
record_name=False,
**kwargs
):
subtype_id = kwargs.get("subtype_id", False)
if not subtype_id:
subtype = subtype or "mt_note"
if "." not in subtype:
subtype = "mail.%s" % subtype
if not subtype_id and subtype_xmlid:
subtype_id = self.env["ir.model.data"].xmlid_to_res_id(
subtype,
subtype_xmlid,
raise_if_not_found=False,
)
if subtype_id:
@ -51,10 +53,12 @@ class MailThread(models.AbstractModel):
rendered_subject_template = self.env[
"mail.template"
]._render_template(
template_txt=template.subject_template,
template_src=template.subject_template,
model=self._name,
res_ids=self.id,
)
res_ids=[self.id],
)[
self.id
]
if template.position == "replace":
subject = rendered_subject_template
elif template.position == "append_before":
@ -67,12 +71,16 @@ class MailThread(models.AbstractModel):
body=body,
subject=subject,
message_type=message_type,
subtype=subtype,
email_from=email_from,
author_id=author_id,
parent_id=parent_id,
subtype_xmlid=subtype_xmlid,
subtype_id=subtype_id,
partner_ids=partner_ids,
channel_ids=channel_ids,
attachments=attachments,
notif_layout=notif_layout,
attachment_ids=attachment_ids,
add_sign=add_sign,
model_description=model_description,
mail_auto_delete=mail_auto_delete,
**kwargs
record_name=record_name,
**kwargs,
)

View File

@ -3,3 +3,6 @@
* Pedro M. Baeza
* João Marques
* Carlos Roca
* Versada <https://versada.eu>
* Naglis Jonaitis

View File

@ -2,16 +2,22 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
# pylint: disable=C8107
from odoo.tests import common
from odoo.tools import mute_logger
class TestMailNotificationCustomSubject(common.TransactionCase):
def setUp(self):
super().setUp()
self.partner_1 = self.env["res.partner"].create(
{"name": "Test partner 1", "email": "partner1@example.com"}
)
self.partner_2 = self.env["res.partner"].create(
{"name": "Test partner 2", "email": "partner2@example.com"}
class TestMailNotificationCustomSubject(common.SavepointCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.partner_1, cls.partner_2 = (
cls.env["res.partner"]
.with_context(tracking_disable=True)
.create(
[
{"name": "Test partner 1", "email": "partner1@example.com"},
{"name": "Test partner 2", "email": "partner2@example.com"},
]
)
)
def test_email_subject_template_overrides(self):
@ -25,24 +31,24 @@ class TestMailNotificationCustomSubject(common.TransactionCase):
)
# Send message in partner
mail_message_1 = self.partner_1.message_post(
body="Test", subtype="mail.mt_comment"
body="Test", subtype_xmlid="mail.mt_comment"
)
# Get message and check subject
self.assertEquals(mail_message_1.subject, "Test partner 1 and something more")
self.assertEqual(mail_message_1.subject, "Test partner 1 and something more")
# Send message in partner 2
mail_message_2 = self.partner_2.message_post(
body="Test", subtype="mail.mt_comment"
body="Test", subtype_xmlid="mail.mt_comment"
)
# Get message and check subject
self.assertEquals(mail_message_2.subject, "Test partner 2 and something more")
self.assertEqual(mail_message_2.subject, "Test partner 2 and something more")
# Explicit subject should also be overwritten
mail_message_3 = self.partner_2.message_post(
body="Test", subtype="mail.mt_comment", subject="Test"
body="Test", subtype_xmlid="mail.mt_comment", subject="Test"
)
# Get message and check subject
self.assertEquals(mail_message_3.subject, "Test partner 2 and something more")
self.assertEqual(mail_message_3.subject, "Test partner 2 and something more")
def test_email_subject_template_normal(self):
self.env["mail.message.custom.subject"].create(
@ -55,10 +61,10 @@ class TestMailNotificationCustomSubject(common.TransactionCase):
)
# Send note in partner
mail_message_1 = self.partner_1.message_post(
body="Test", subtype="mail.mt_note", subject="Test"
body="Test", subtype_xmlid="mail.mt_note", subject="Test"
)
# Get message and check subject. Subject Template should not apply
self.assertEquals(mail_message_1.subject, "Test")
self.assertEqual(mail_message_1.subject, "Test")
def test_email_subject_template_multi(self):
self.env["mail.message.custom.subject"].create(
@ -79,10 +85,10 @@ class TestMailNotificationCustomSubject(common.TransactionCase):
)
# Send message in partner
mail_message_1 = self.partner_1.message_post(
body="Test", subtype="mail.mt_comment"
body="Test", subtype_xmlid="mail.mt_comment"
)
# Get message and check subject
self.assertEquals(
self.assertEqual(
mail_message_1.subject, "Test partner 1 and something different"
)
self.env["mail.message.custom.subject"].create(
@ -96,10 +102,10 @@ class TestMailNotificationCustomSubject(common.TransactionCase):
)
# Send message in partner
mail_message_2 = self.partner_1.message_post(
body="Test", subtype="mail.mt_comment"
body="Test", subtype_xmlid="mail.mt_comment"
)
# Get message and check subject
self.assertEquals(
self.assertEqual(
mail_message_2.subject,
"Test partner 1 and something different and yet something else",
)
@ -114,10 +120,10 @@ class TestMailNotificationCustomSubject(common.TransactionCase):
)
# Send message in partner
mail_message_3 = self.partner_1.message_post(
body="Test", subtype="mail.mt_comment"
body="Test", subtype_xmlid="mail.mt_comment"
)
# Get message and check subject
self.assertEquals(
self.assertEqual(
mail_message_3.subject,
"Re: Test partner 1 and something different and yet something else",
)
@ -135,11 +141,11 @@ class TestMailNotificationCustomSubject(common.TransactionCase):
# Send message in partner
mail_message_1 = self.partner_1.message_post(
body="Test",
subtype="mail.mt_comment",
subtype_xmlid="mail.mt_comment",
subject="Test",
)
# Get message and check subject
self.assertEquals(mail_message_1.subject, "Test and something more")
self.assertEqual(mail_message_1.subject, "Test and something more")
def test_bad_template_does_not_break(self):
"""Create template with error (obaject) to test error."""
@ -153,20 +159,21 @@ class TestMailNotificationCustomSubject(common.TransactionCase):
}
)
# Send message in partner
mail_message_1 = self.partner_1.message_post(
body="Test",
subtype="mail.mt_comment",
subject="Test",
)
with mute_logger("odoo.addons.mail.models.mail_render_mixin"):
mail_message_1 = self.partner_1.message_post(
body="Test",
subtype_xmlid="mail.mt_comment",
subject="Test",
)
# Get message and check subject
# No exception should be raised but subject should remain as original.
self.assertEquals(mail_message_1.subject, "Test")
self.assertEqual(mail_message_1.subject, "Test")
def test_no_template_default_result(self):
# Send message in partner
mail_message_1 = self.partner_1.message_post(
body="Test", subtype="mail.mt_comment", subject="Test partner 1"
body="Test", subtype_xmlid="mail.mt_comment", subject="Test partner 1"
)
# Get message and check subject
# No exception should be raised but subject should remain as original.
self.assertEquals(mail_message_1.subject, "Test partner 1")
self.assertEqual(mail_message_1.subject, "Test partner 1")

View File

@ -16,7 +16,7 @@
name="subject_template"
placeholder="Subject (placeholders may be used here)"
/>
<field name="model_id" />
<field name="model_id" options="{'no_create': True}" />
<field name="subtype_ids" widget="many2many_tags" />
<field name="position" />
</group>
@ -29,7 +29,7 @@
<field name="name">mail.message.custom.subject.tree</field>
<field name="model">mail.message.custom.subject</field>
<field name="arch" type="xml">
<tree string="Templates">
<tree>
<field name="name" />
<field name="model_id" />
<field name="subtype_ids" />