Skip to content

Commit

Permalink
[IMP] mail_notification_custom_subject: Add inside_replace position
Browse files Browse the repository at this point in the history
Example use Case:
Our company is called: MY_COMPANY_NAME but is known as MCN,
we want to replace in some places the subject line so that it is sent with MCN

TT52259
  • Loading branch information
victoralmau committed Jan 17, 2025
1 parent 283069e commit 9469da8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
)
17 changes: 17 additions & 0 deletions mail_notification_custom_subject/models/mail_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 87 in mail_notification_custom_subject/models/mail_thread.py

View check run for this annotation

Codecov / codecov/patch

mail_notification_custom_subject/models/mail_thread.py#L86-L87

Added lines #L86 - L87 were not covered by tests
except Exception:
rendered_subject_template = False
return super().message_post(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
<h1 name="name"><field name="name" /></h1>
</div>
<group>
<field
name="subject_to_replace"
invisible="position!='inside_replace'"
required="position=='inside_replace'"
/>
<field
name="subject_template"
placeholder="Subject (placeholders may be used here)"
Expand Down

0 comments on commit 9469da8

Please sign in to comment.