From 14cc2d99bed46d47b1a4c189e942525f8c91650d Mon Sep 17 00:00:00 2001 From: Naglis Jonaitis Date: Thu, 12 Aug 2021 13:58:35 +0300 Subject: [PATCH] [MIG] mail_notification_custom_subject: Migration to 14.0 --- .../__manifest__.py | 2 +- .../models/mail_message_custom_subject.py | 1 + .../models/mail_thread.py | 42 ++++++----- .../readme/CONTRIBUTORS.rst | 3 + .../test_mail_notification_custom_subject.py | 71 ++++++++++--------- ...mail_notification_custom_subject_views.xml | 4 +- 6 files changed, 71 insertions(+), 52 deletions(-) diff --git a/mail_notification_custom_subject/__manifest__.py b/mail_notification_custom_subject/__manifest__.py index 10429be..11b9545 100644 --- a/mail_notification_custom_subject/__manifest__.py +++ b/mail_notification_custom_subject/__manifest__.py @@ -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)", diff --git a/mail_notification_custom_subject/models/mail_message_custom_subject.py b/mail_notification_custom_subject/models/mail_message_custom_subject.py index 19a48f6..fe6fda0 100644 --- a/mail_notification_custom_subject/models/mail_message_custom_subject.py +++ b/mail_notification_custom_subject/models/mail_message_custom_subject.py @@ -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", diff --git a/mail_notification_custom_subject/models/mail_thread.py b/mail_notification_custom_subject/models/mail_thread.py index 96d9abe..6c97088 100644 --- a/mail_notification_custom_subject/models/mail_thread.py +++ b/mail_notification_custom_subject/models/mail_thread.py @@ -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, ) diff --git a/mail_notification_custom_subject/readme/CONTRIBUTORS.rst b/mail_notification_custom_subject/readme/CONTRIBUTORS.rst index e7d295b..4231ca1 100644 --- a/mail_notification_custom_subject/readme/CONTRIBUTORS.rst +++ b/mail_notification_custom_subject/readme/CONTRIBUTORS.rst @@ -3,3 +3,6 @@ * Pedro M. Baeza * João Marques * Carlos Roca + +* Versada + * Naglis Jonaitis diff --git a/mail_notification_custom_subject/tests/test_mail_notification_custom_subject.py b/mail_notification_custom_subject/tests/test_mail_notification_custom_subject.py index 68cbc7f..bdc7367 100644 --- a/mail_notification_custom_subject/tests/test_mail_notification_custom_subject.py +++ b/mail_notification_custom_subject/tests/test_mail_notification_custom_subject.py @@ -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") diff --git a/mail_notification_custom_subject/views/mail_notification_custom_subject_views.xml b/mail_notification_custom_subject/views/mail_notification_custom_subject_views.xml index 14ecd80..6ab1631 100644 --- a/mail_notification_custom_subject/views/mail_notification_custom_subject_views.xml +++ b/mail_notification_custom_subject/views/mail_notification_custom_subject_views.xml @@ -16,7 +16,7 @@ name="subject_template" placeholder="Subject (placeholders may be used here)" /> - + @@ -29,7 +29,7 @@ mail.message.custom.subject.tree mail.message.custom.subject - +