[IMP] mail_debrand: Keep message body intact while removing branding
Partially fixes https://github.com/OCA/social/issues/713 TT31737
This commit is contained in:
parent
bc616132b5
commit
de64c8d610
@ -75,9 +75,12 @@ Authors
|
|||||||
Contributors
|
Contributors
|
||||||
~~~~~~~~~~~~
|
~~~~~~~~~~~~
|
||||||
|
|
||||||
* Pedro M. Baeza <pedro.baeza@tecnativa.com>
|
|
||||||
* Lois Rilo <lois.rilo@eficent.com>
|
* Lois Rilo <lois.rilo@eficent.com>
|
||||||
* Graeme Gellatly <graeme@o4sb.com>
|
* Graeme Gellatly <graeme@o4sb.com>
|
||||||
|
* `Tecnativa <https://www.tecnativa.com>`__:
|
||||||
|
|
||||||
|
* Pedro M. Baeza
|
||||||
|
* João Marques
|
||||||
|
|
||||||
Maintainers
|
Maintainers
|
||||||
~~~~~~~~~~~
|
~~~~~~~~~~~
|
||||||
@ -95,10 +98,13 @@ promote its widespread use.
|
|||||||
.. |maintainer-pedrobaeza| image:: https://github.com/pedrobaeza.png?size=40px
|
.. |maintainer-pedrobaeza| image:: https://github.com/pedrobaeza.png?size=40px
|
||||||
:target: https://github.com/pedrobaeza
|
:target: https://github.com/pedrobaeza
|
||||||
:alt: pedrobaeza
|
:alt: pedrobaeza
|
||||||
|
.. |maintainer-joao-p-marques| image:: https://github.com/joao-p-marques.png?size=40px
|
||||||
|
:target: https://github.com/joao-p-marques
|
||||||
|
:alt: joao-p-marques
|
||||||
|
|
||||||
Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:
|
Current `maintainers <https://odoo-community.org/page/maintainer-role>`__:
|
||||||
|
|
||||||
|maintainer-pedrobaeza|
|
|maintainer-pedrobaeza| |maintainer-joao-p-marques|
|
||||||
|
|
||||||
This module is part of the `OCA/social <https://github.com/OCA/social/tree/14.0/mail_debrand>`_ project on GitHub.
|
This module is part of the `OCA/social <https://github.com/OCA/social/tree/14.0/mail_debrand>`_ project on GitHub.
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
# Copyright 2019 Eficent Business and IT Consulting Services S.L.
|
# Copyright 2019 Eficent Business and IT Consulting Services S.L.
|
||||||
# - Lois Rilo <lois.rilo@eficent.com>
|
# - Lois Rilo <lois.rilo@eficent.com>
|
||||||
# 2020 NextERP Romania
|
# 2020 NextERP Romania
|
||||||
|
# Copyright 2021 Tecnativa - João Marques
|
||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -12,7 +13,7 @@
|
|||||||
( for powerd by) form all the templates
|
( for powerd by) form all the templates
|
||||||
removes any 'odoo' that are in tempalte texts > 20characters
|
removes any 'odoo' that are in tempalte texts > 20characters
|
||||||
""",
|
""",
|
||||||
"version": "14.0.2.0.1",
|
"version": "14.0.2.0.2",
|
||||||
"category": "Social Network",
|
"category": "Social Network",
|
||||||
"website": "https://github.com/OCA/social",
|
"website": "https://github.com/OCA/social",
|
||||||
"author": """Tecnativa, Eficent, Onestein, Sodexis, Nexterp Romania,
|
"author": """Tecnativa, Eficent, Onestein, Sodexis, Nexterp Romania,
|
||||||
@ -21,5 +22,5 @@
|
|||||||
"installable": True,
|
"installable": True,
|
||||||
"depends": ["mail"],
|
"depends": ["mail"],
|
||||||
"development_status": "Production/Stable",
|
"development_status": "Production/Stable",
|
||||||
"maintainers": ["pedrobaeza"],
|
"maintainers": ["pedrobaeza", "joao-p-marques"],
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ class MailMail(models.AbstractModel):
|
|||||||
_inherit = "mail.mail"
|
_inherit = "mail.mail"
|
||||||
|
|
||||||
def _send_prepare_body(self):
|
def _send_prepare_body(self):
|
||||||
body = super()._send_prepare_body()
|
body_html = super()._send_prepare_body()
|
||||||
return self.env["mail.render.mixin"].remove_href_odoo(
|
return self.env["mail.render.mixin"].remove_href_odoo(
|
||||||
body or "", remove_parent=0, remove_before=1
|
body_html or "", remove_parent=0, remove_before=1, to_keep=self.body
|
||||||
)
|
)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
# Copyright 2019 O4SB - Graeme Gellatly
|
# Copyright 2019 O4SB - Graeme Gellatly
|
||||||
# Copyright 2019 Tecnativa - Ernesto Tejeda
|
# Copyright 2019 Tecnativa - Ernesto Tejeda
|
||||||
# Copyright 2020 Onestein - Andrea Stirpe
|
# Copyright 2020 Onestein - Andrea Stirpe
|
||||||
|
# Copyright 2021 Tecnativa - João Marques
|
||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||||
import re
|
import re
|
||||||
|
|
||||||
@ -12,7 +13,9 @@ from odoo import api, models
|
|||||||
class MailRenderMixin(models.AbstractModel):
|
class MailRenderMixin(models.AbstractModel):
|
||||||
_inherit = "mail.render.mixin"
|
_inherit = "mail.render.mixin"
|
||||||
|
|
||||||
def remove_href_odoo(self, value, remove_parent=True, remove_before=False):
|
def remove_href_odoo(
|
||||||
|
self, value, remove_parent=True, remove_before=False, to_keep=None
|
||||||
|
):
|
||||||
if len(value) < 20:
|
if len(value) < 20:
|
||||||
return value
|
return value
|
||||||
# value can be bytes type; ensure we get a proper string
|
# value can be bytes type; ensure we get a proper string
|
||||||
@ -20,35 +23,41 @@ class MailRenderMixin(models.AbstractModel):
|
|||||||
value = value.decode()
|
value = value.decode()
|
||||||
has_odoo_link = re.search(r"<a\s(.*)odoo\.com", value, flags=re.IGNORECASE)
|
has_odoo_link = re.search(r"<a\s(.*)odoo\.com", value, flags=re.IGNORECASE)
|
||||||
if has_odoo_link:
|
if has_odoo_link:
|
||||||
tree = etree.HTML(
|
# We don't want to change what was explicitly added in the message body,
|
||||||
value
|
# so we will only change what is before and after it.
|
||||||
) # html with broken links tree = etree.fromstring(value) just xml
|
if to_keep:
|
||||||
odoo_achors = tree.xpath('//a[contains(@href,"odoo.com")]')
|
to_change = value.split(to_keep)
|
||||||
for elem in odoo_achors:
|
else:
|
||||||
|
to_change = [value]
|
||||||
|
to_keep = ""
|
||||||
|
new_parts = []
|
||||||
|
for part in to_change:
|
||||||
|
tree = html.fromstring(part)
|
||||||
|
if tree is None:
|
||||||
|
new_parts.append(part)
|
||||||
|
continue
|
||||||
|
odoo_anchors = tree.xpath('//a[contains(@href,"odoo.com")]')
|
||||||
|
for elem in odoo_anchors:
|
||||||
parent = elem.getparent()
|
parent = elem.getparent()
|
||||||
previous = elem.getprevious()
|
previous = elem.getprevious()
|
||||||
|
|
||||||
if remove_before and not remove_parent and previous is not None:
|
if remove_before and not remove_parent and previous is not None:
|
||||||
# remove 'using' that is before <a and after </span>
|
# remove 'using' that is before <a and after </span>
|
||||||
bytes_text = etree.tostring(
|
previous.tail = ""
|
||||||
previous, pretty_print=True, method="html"
|
|
||||||
)
|
|
||||||
only_what_is_in_tags = bytes_text[: bytes_text.rfind(b">") + 1]
|
|
||||||
data_formatted = html.fromstring(only_what_is_in_tags)
|
|
||||||
parent.replace(previous, data_formatted)
|
|
||||||
if remove_parent and len(parent.getparent()):
|
if remove_parent and len(parent.getparent()):
|
||||||
# anchor <a href odoo has a parent powered by that must be removed
|
# anchor <a href odoo has a parent powered by that must be removed
|
||||||
parent.getparent().remove(parent)
|
parent.getparent().remove(parent)
|
||||||
else:
|
else:
|
||||||
if parent.tag == "td": # also here can be powerd by
|
if parent.tag == "td": # also here can be powered by
|
||||||
parent.getparent().remove(parent)
|
parent.getparent().remove(parent)
|
||||||
else:
|
else:
|
||||||
parent.remove(elem)
|
parent.remove(elem)
|
||||||
value = etree.tostring(tree, pretty_print=True, method="html")
|
part = etree.tostring(
|
||||||
# etree can return bytes; ensure we get a proper string
|
tree, pretty_print=True, method="html", encoding="unicode"
|
||||||
if type(value) is bytes:
|
)
|
||||||
value = value.decode()
|
new_parts.append(part)
|
||||||
return re.sub("[^(<)(</)]odoo", "", value, flags=re.IGNORECASE)
|
value = to_keep.join(new_parts)
|
||||||
|
return value
|
||||||
|
|
||||||
@api.model
|
@api.model
|
||||||
def _render_template(
|
def _render_template(
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
* Pedro M. Baeza <pedro.baeza@tecnativa.com>
|
|
||||||
* Lois Rilo <lois.rilo@eficent.com>
|
* Lois Rilo <lois.rilo@eficent.com>
|
||||||
* Graeme Gellatly <graeme@o4sb.com>
|
* Graeme Gellatly <graeme@o4sb.com>
|
||||||
|
* `Tecnativa <https://www.tecnativa.com>`__:
|
||||||
|
|
||||||
|
* Pedro M. Baeza
|
||||||
|
* João Marques
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||||
<meta name="generator" content="Docutils 0.15.1: http://docutils.sourceforge.net/" />
|
<meta name="generator" content="Docutils: http://docutils.sourceforge.net/" />
|
||||||
<title>Mail Debrand</title>
|
<title>Mail Debrand</title>
|
||||||
<style type="text/css">
|
<style type="text/css">
|
||||||
|
|
||||||
@ -428,9 +428,13 @@ If you spotted it first, help us smashing it by providing a detailed and welcome
|
|||||||
<div class="section" id="contributors">
|
<div class="section" id="contributors">
|
||||||
<h2><a class="toc-backref" href="#id8">Contributors</a></h2>
|
<h2><a class="toc-backref" href="#id8">Contributors</a></h2>
|
||||||
<ul class="simple">
|
<ul class="simple">
|
||||||
<li>Pedro M. Baeza <<a class="reference external" href="mailto:pedro.baeza@tecnativa.com">pedro.baeza@tecnativa.com</a>></li>
|
|
||||||
<li>Lois Rilo <<a class="reference external" href="mailto:lois.rilo@eficent.com">lois.rilo@eficent.com</a>></li>
|
<li>Lois Rilo <<a class="reference external" href="mailto:lois.rilo@eficent.com">lois.rilo@eficent.com</a>></li>
|
||||||
<li>Graeme Gellatly <<a class="reference external" href="mailto:graeme@o4sb.com">graeme@o4sb.com</a>></li>
|
<li>Graeme Gellatly <<a class="reference external" href="mailto:graeme@o4sb.com">graeme@o4sb.com</a>></li>
|
||||||
|
<li><a class="reference external" href="https://www.tecnativa.com">Tecnativa</a>:<ul>
|
||||||
|
<li>Pedro M. Baeza</li>
|
||||||
|
<li>João Marques</li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<div class="section" id="maintainers">
|
<div class="section" id="maintainers">
|
||||||
@ -440,8 +444,8 @@ If you spotted it first, help us smashing it by providing a detailed and welcome
|
|||||||
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
|
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
|
||||||
mission is to support the collaborative development of Odoo features and
|
mission is to support the collaborative development of Odoo features and
|
||||||
promote its widespread use.</p>
|
promote its widespread use.</p>
|
||||||
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainer</a>:</p>
|
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainers</a>:</p>
|
||||||
<p><a class="reference external" href="https://github.com/pedrobaeza"><img alt="pedrobaeza" src="https://github.com/pedrobaeza.png?size=40px" /></a></p>
|
<p><a class="reference external" href="https://github.com/pedrobaeza"><img alt="pedrobaeza" src="https://github.com/pedrobaeza.png?size=40px" /></a> <a class="reference external" href="https://github.com/joao-p-marques"><img alt="joao-p-marques" src="https://github.com/joao-p-marques.png?size=40px" /></a></p>
|
||||||
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/social/tree/14.0/mail_debrand">OCA/social</a> project on GitHub.</p>
|
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/social/tree/14.0/mail_debrand">OCA/social</a> project on GitHub.</p>
|
||||||
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
|
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
# Copyright 2017 Tecnativa - Pedro M. Baeza
|
# Copyright 2017 Tecnativa - Pedro M. Baeza
|
||||||
# Copyright 2020 Onestein - Andrea Stirpe
|
# Copyright 2020 Onestein - Andrea Stirpe
|
||||||
# Copyright 2021 Sodexis
|
# Copyright 2021 Sodexis
|
||||||
|
# Copyright 2021 Tecnativa - João Marques
|
||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||||
|
|
||||||
from odoo.tests import common
|
from odoo.tests import common
|
||||||
@ -65,3 +66,42 @@ class TestMailDebrand(common.TransactionCase):
|
|||||||
}
|
}
|
||||||
# No exception expected
|
# No exception expected
|
||||||
MailMessage.create(email_values)
|
MailMessage.create(email_values)
|
||||||
|
|
||||||
|
def test_body_intact(self):
|
||||||
|
"""The message body should never be changed"""
|
||||||
|
MailMessage = self.env["mail.mail"]
|
||||||
|
original_body = (
|
||||||
|
"<p>And if I send odoo.example.com<br><br>And odoo.com"
|
||||||
|
'<br><br>And <a target="_blank" rel="noreferrer noopener" '
|
||||||
|
'href="https://odoo.com">https://odoo.com</a><br><br>And '
|
||||||
|
'<a target="_blank" rel="noreferrer noopener" '
|
||||||
|
'href="https://odoo.example.com">https://odoo.example.com</a></p>'
|
||||||
|
)
|
||||||
|
email_values = {
|
||||||
|
"email_from": "customer@example.com",
|
||||||
|
"subject": "Hello",
|
||||||
|
"email_to": "contact@example.com",
|
||||||
|
"reply_to": "contact@example.com",
|
||||||
|
"body": original_body,
|
||||||
|
"body_html": (
|
||||||
|
"\n<div>\n\n\n<div><p>And if I send odoo.example.com<br><br>"
|
||||||
|
'And odoo.com<br><br>And <a target="_blank" '
|
||||||
|
'rel="noreferrer noopener" href="https://odoo.com">'
|
||||||
|
'https://odoo.com</a><br><br>And <a target="_blank" '
|
||||||
|
'rel="noreferrer noopener" href="https://odoo.example.com">'
|
||||||
|
"https://odoo.example.com</a></p></div>\n\n"
|
||||||
|
'<div style="font-size: 13px;"><span data-o-mail-quote="1">-- '
|
||||||
|
'<br data-o-mail-quote="1">\nAdministrator</span></div>\n'
|
||||||
|
'<p style="color: #555555; margin-top:32px;">\n Sent\n '
|
||||||
|
'<span>\n by\n <a style="text-decoration:none; '
|
||||||
|
'color: #875A7B;" href="http://www.example.com">\n '
|
||||||
|
"<span>YourCompany</span>\n </a>\n \n </span>\n "
|
||||||
|
'using\n <a target="_blank" '
|
||||||
|
'href="https://www.odoo.com?utm_source=db&utm_medium=email"'
|
||||||
|
' style="text-decoration:none; color: #875A7B;">Odoo'
|
||||||
|
"</a>.\n</p>\n</div>\n "
|
||||||
|
),
|
||||||
|
}
|
||||||
|
# No exception expected
|
||||||
|
message = MailMessage.create(email_values)
|
||||||
|
self.assertTrue(original_body in message._send_prepare_body())
|
||||||
|
Loading…
Reference in New Issue
Block a user