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 57bd457 commit eb7d3ac
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 35 deletions.
56 changes: 31 additions & 25 deletions mail_notification_custom_subject/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,34 @@ the notification emails sent by Odoo
Configuration
=============

- Activate access to **Technical Features** (debug mode).

- Go to **Settings > Technical > Email > Subject Replacement Templates**

- Create a new template.

- The field **Model** specifies the model to which the subject
template should apply in the notification emails sent by Odoo.
- The field **Subject Template** accepts
`Jinja <https://jinja.palletsprojects.com/en/2.11.x/>`__
expressions.
- The field **Replace** specifies if the template should replace
existing content or append to it.
- Activate access to **Technical Features** (debug mode).

- Go to **Settings > Technical > Email > Subject Replacement
Templates**

- Create a new template.

- The field **Model** specifies the model to which the subject
template should apply in the notification emails sent by Odoo.
- The field **Subject Template** accepts
`Jinja <https://jinja.palletsprojects.com/en/2.11.x/>`__
expressions.
- The field **Subject to replace** accepts
`Jinja <https://jinja.palletsprojects.com/en/2.11.x/>`__
expressions.
- The field **Replace** specifies if the template should replace
existing content or append to it.
- The field **Partial Replacement** specifies if the template
should parcial replace existing content.

Usage
=====

To use this module, you need to:

- Open the chatter in Odoo (e.g. Open an Invoice).
- Send a message.
- Observe the rendered Subject template.
- Open the chatter in Odoo (e.g. Open an Invoice).
- Send a message.
- Observe the rendered Subject template.

Bug Tracker
===========
Expand All @@ -83,20 +89,20 @@ Authors
Contributors
------------

- Tecnativa <https://www.tecnativa.com>
- Tecnativa <https://www.tecnativa.com>

- Pedro M. Baeza
- João Marques
- Carlos Roca
- Víctor Martínez
- Pedro M. Baeza
- João Marques
- Carlos Roca
- Víctor Martínez

- Versada <https://versada.eu>
- Versada <https://versada.eu>

- Naglis Jonaitis
- Naglis Jonaitis

- Moduon <https://www.moduon.team>
- Moduon <https://www.moduon.team>

- Eduardo de Miguel
- Eduardo de Miguel

Maintainers
-----------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class MailMessageCustomSubject(models.Model):
string="Applied Subtypes",
required=True,
)
subject_to_replace = fields.Char(
help="The text that will be replaced. You can use placeholders."
" E.g.: {{ object.company_id.name }}"
)
subject_template = fields.Char(
required=True,
translate=True,
Expand All @@ -31,8 +35,9 @@ class MailMessageCustomSubject(models.Model):
("append_before", "Append Before"),
("append_after", "Append After"),
("replace", "Replace"),
("inside_replace", "Partial Replacement"),
],
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 completely, partially, append at beginning or append"
" at end to other templates that apply to a given context",
)
14 changes: 14 additions & 0 deletions mail_notification_custom_subject/models/mail_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ def message_post(
subject = rendered_subject_template + subject
elif template.position == "append_after":
subject += rendered_subject_template
elif template.position == "inside_replace":
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,
)
return super().message_post(
body=body,
subject=subject,
Expand Down
4 changes: 4 additions & 0 deletions mail_notification_custom_subject/readme/CONFIGURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
> template should apply in the notification emails sent by Odoo.
> - The field **Subject Template** accepts
> [Jinja](https://jinja.palletsprojects.com/en/2.11.x/) expressions.
> - The field **Subject to replace** accepts
> [Jinja](https://jinja.palletsprojects.com/en/2.11.x/) expressions.
> - The field **Replace** specifies if the template should replace
> existing content or append to it.
> - The field **Partial Replacement** specifies if the template should
> parcial replace existing content.
19 changes: 11 additions & 8 deletions mail_notification_custom_subject/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

/*
:Author: David Goodger ([email protected])
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Copyright: This stylesheet has been placed in the public domain.
Default cascading style sheet for the HTML output of Docutils.
Despite the name, some widely supported CSS2 features are used.
See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
Expand Down Expand Up @@ -275,7 +274,7 @@
margin-left: 2em ;
margin-right: 2em }

pre.code .ln { color: gray; } /* line numbers */
pre.code .ln { color: grey; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
Expand All @@ -301,7 +300,7 @@
span.pre {
white-space: pre }

span.problematic, pre.problematic {
span.problematic {
color: red }

span.section-subtitle {
Expand Down Expand Up @@ -391,7 +390,8 @@ <h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
<ul>
<li><p class="first">Activate access to <strong>Technical Features</strong> (debug mode).</p>
</li>
<li><p class="first">Go to <strong>Settings &gt; Technical &gt; Email &gt; Subject Replacement Templates</strong></p>
<li><p class="first">Go to <strong>Settings &gt; Technical &gt; Email &gt; Subject Replacement
Templates</strong></p>
</li>
<li><p class="first">Create a new template.</p>
<blockquote>
Expand All @@ -401,8 +401,13 @@ <h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
<li>The field <strong>Subject Template</strong> accepts
<a class="reference external" href="https://jinja.palletsprojects.com/en/2.11.x/">Jinja</a>
expressions.</li>
<li>The field <strong>Subject to replace</strong> accepts
<a class="reference external" href="https://jinja.palletsprojects.com/en/2.11.x/">Jinja</a>
expressions.</li>
<li>The field <strong>Replace</strong> specifies if the template should replace
existing content or append to it.</li>
<li>The field <strong>Partial Replacement</strong> specifies if the template
should parcial replace existing content.</li>
</ul>
</blockquote>
</li>
Expand Down Expand Up @@ -461,9 +466,7 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
</a>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,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 eb7d3ac

Please sign in to comment.