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 0340b56305..53e169109d 100644 --- a/mail_notification_custom_subject/models/mail_message_custom_subject.py +++ b/mail_notification_custom_subject/models/mail_message_custom_subject.py @@ -21,6 +21,9 @@ class MailMessageCustomSubject(models.Model): string="Applied Subtypes", required=True, ) + subject_to_replace = fields.Char( + help="Subject to replace (placeholders may be used here)" + ) subject_template = fields.Char( required=True, translate=True, @@ -31,8 +34,9 @@ class MailMessageCustomSubject(models.Model): ("append_before", "Append Before"), ("append_after", "Append After"), ("replace", "Replace"), + ("inside_replace", "Inside Replace"), ], default="replace", - help="Whether to replace, append at beggining or append at end to other" - " templates that apply to a given context", + help="Whether to replace, inside Replace, append at beggining or append at" + " end to other templates that apply to a given context", ) diff --git a/mail_notification_custom_subject/models/mail_thread.py b/mail_notification_custom_subject/models/mail_thread.py index 08a26b32db..ddd6954f64 100644 --- a/mail_notification_custom_subject/models/mail_thread.py +++ b/mail_notification_custom_subject/models/mail_thread.py @@ -68,6 +68,23 @@ def message_post( subject = rendered_subject_template + subject elif template.position == "append_after": subject += rendered_subject_template + elif template.position == "inside_replace": + try: + rendered_subject_to_replace = self.env[ + "mail.template" + ]._render_template( + template_src=template.subject_to_replace, + model=self._name, + res_ids=[self.id], + )[self.id] + if rendered_subject_to_replace: + # To avoid empty string replacements + subject = subject.replace( + rendered_subject_to_replace, + rendered_subject_template, + ) + except Exception: + rendered_subject_to_replace = False except Exception: rendered_subject_template = False return super().message_post( 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 08c37249af..a1e2bfe740 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 @@ -55,6 +55,27 @@ def test_email_subject_template_overrides(self): # Get message and check subject self.assertEqual(mail_message_3.subject, "Test") + def test_email_subject_template_inside_replace(self): + with self.with_user("boss"): + self.env["mail.message.custom.subject"].create( + { + "name": "Test template", + "model_id": self.env.ref("base.model_res_partner").id, + "subtype_ids": [(6, 0, [self.env.ref("mail.mt_comment").id])], + "subject_to_replace": "{{object.company_id.name}}", + "subject_template": "CLN", + "position": "inside_replace", + } + ) + self.partner_1.company_id = self.env.company + self.partner_1.company_id.name = "COMPANY_LONG_NAME" + mail_message_1 = self.partner_1.message_post( + subject="COMPANY_LONG_NAME: Custom", + body="Test", + subtype_xmlid="mail.mt_comment", + ) + self.assertEqual(mail_message_1.subject, "CLN: Custom") + def test_email_subject_template_normal(self): with self.with_user("boss"): self.env["mail.message.custom.subject"].create( 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 3267af9fbb..ee91309f49 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 @@ -11,6 +11,11 @@