From de64c8d6102375f61cdbc9a1e18909cb4fd8af46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Marques?= Date: Tue, 14 Sep 2021 09:38:47 +0100 Subject: [PATCH] [IMP] mail_debrand: Keep message body intact while removing branding Partially fixes https://github.com/OCA/social/issues/713 TT31737 --- mail_debrand/README.rst | 12 +++-- mail_debrand/__manifest__.py | 5 +- mail_debrand/models/mail_mail.py | 4 +- mail_debrand/models/mail_render_mixin.py | 63 ++++++++++++---------- mail_debrand/readme/CONTRIBUTORS.rst | 5 +- mail_debrand/static/description/index.html | 12 +++-- mail_debrand/tests/test_mail_debrand.py | 40 ++++++++++++++ 7 files changed, 102 insertions(+), 39 deletions(-) diff --git a/mail_debrand/README.rst b/mail_debrand/README.rst index 8fc2b8464..8ce988f6e 100644 --- a/mail_debrand/README.rst +++ b/mail_debrand/README.rst @@ -75,9 +75,12 @@ Authors Contributors ~~~~~~~~~~~~ -* Pedro M. Baeza * Lois Rilo * Graeme Gellatly +* `Tecnativa `__: + + * Pedro M. Baeza + * João Marques Maintainers ~~~~~~~~~~~ @@ -95,10 +98,13 @@ promote its widespread use. .. |maintainer-pedrobaeza| image:: https://github.com/pedrobaeza.png?size=40px :target: https://github.com/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 `__: +Current `maintainers `__: -|maintainer-pedrobaeza| +|maintainer-pedrobaeza| |maintainer-joao-p-marques| This module is part of the `OCA/social `_ project on GitHub. diff --git a/mail_debrand/__manifest__.py b/mail_debrand/__manifest__.py index 26ecfa263..9a999f92c 100644 --- a/mail_debrand/__manifest__.py +++ b/mail_debrand/__manifest__.py @@ -3,6 +3,7 @@ # Copyright 2019 Eficent Business and IT Consulting Services S.L. # - Lois Rilo # 2020 NextERP Romania +# Copyright 2021 Tecnativa - João Marques # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). { @@ -12,7 +13,7 @@ ( for powerd by) form all the templates removes any 'odoo' that are in tempalte texts > 20characters """, - "version": "14.0.2.0.1", + "version": "14.0.2.0.2", "category": "Social Network", "website": "https://github.com/OCA/social", "author": """Tecnativa, Eficent, Onestein, Sodexis, Nexterp Romania, @@ -21,5 +22,5 @@ "installable": True, "depends": ["mail"], "development_status": "Production/Stable", - "maintainers": ["pedrobaeza"], + "maintainers": ["pedrobaeza", "joao-p-marques"], } diff --git a/mail_debrand/models/mail_mail.py b/mail_debrand/models/mail_mail.py index f6802fbf7..4cd35bf17 100644 --- a/mail_debrand/models/mail_mail.py +++ b/mail_debrand/models/mail_mail.py @@ -9,7 +9,7 @@ class MailMail(models.AbstractModel): _inherit = "mail.mail" 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( - body or "", remove_parent=0, remove_before=1 + body_html or "", remove_parent=0, remove_before=1, to_keep=self.body ) diff --git a/mail_debrand/models/mail_render_mixin.py b/mail_debrand/models/mail_render_mixin.py index 1fc9fcb98..1dab961c7 100644 --- a/mail_debrand/models/mail_render_mixin.py +++ b/mail_debrand/models/mail_render_mixin.py @@ -1,6 +1,7 @@ # Copyright 2019 O4SB - Graeme Gellatly # Copyright 2019 Tecnativa - Ernesto Tejeda # Copyright 2020 Onestein - Andrea Stirpe +# Copyright 2021 Tecnativa - João Marques # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). import re @@ -12,7 +13,9 @@ from odoo import api, models class MailRenderMixin(models.AbstractModel): _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: return value # value can be bytes type; ensure we get a proper string @@ -20,35 +23,41 @@ class MailRenderMixin(models.AbstractModel): value = value.decode() has_odoo_link = re.search(r" - bytes_text = etree.tostring( - 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()): - # anchor + previous.tail = "" + if remove_parent and len(parent.getparent()): + # anchor * Lois Rilo * Graeme Gellatly +* `Tecnativa `__: + + * Pedro M. Baeza + * João Marques diff --git a/mail_debrand/static/description/index.html b/mail_debrand/static/description/index.html index 8e9ae8eb9..c42da8079 100644 --- a/mail_debrand/static/description/index.html +++ b/mail_debrand/static/description/index.html @@ -3,7 +3,7 @@ - + Mail Debrand