From c706391c2e1f6fc4224a79526d0adc789a43a623 Mon Sep 17 00:00:00 2001 From: Graeme Gellatly Date: Thu, 27 Dec 2018 21:26:36 +1300 Subject: [PATCH] [FIX] Parse generated html to remove to remove Odoo branding messages --- mail_debrand/__init__.py | 1 + mail_debrand/__manifest__.py | 7 +--- mail_debrand/models/__init__.py | 1 + mail_debrand/models/mail_template.py | 42 +++++++++++++++++++ mail_debrand/tests/__init__.py | 1 + mail_debrand/tests/test_mail_debrand.py | 25 +++++++++++ mail_debrand/views/mail_notification_view.xml | 29 ------------- 7 files changed, 72 insertions(+), 34 deletions(-) create mode 100644 mail_debrand/models/__init__.py create mode 100644 mail_debrand/models/mail_template.py create mode 100644 mail_debrand/tests/__init__.py create mode 100644 mail_debrand/tests/test_mail_debrand.py delete mode 100644 mail_debrand/views/mail_notification_view.xml diff --git a/mail_debrand/__init__.py b/mail_debrand/__init__.py index e69de29b..0650744f 100644 --- a/mail_debrand/__init__.py +++ b/mail_debrand/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/mail_debrand/__manifest__.py b/mail_debrand/__manifest__.py index 9d5f58ee..66d465d2 100644 --- a/mail_debrand/__manifest__.py +++ b/mail_debrand/__manifest__.py @@ -3,16 +3,13 @@ { "name": "Mail Debrand", "summary": "Remove Odoo branding in sent emails", - "version": "12.0.1.0.0", + "version": "12.0.2.0.0", "category": "Social Network", - "website": "https://odoo-community.org/", + "website": "https://github.com/OCA/social/", "author": "Odoo Community Association (OCA)", "license": "AGPL-3", "installable": True, "depends": [ "mail", ], - "data": [ - 'views/mail_notification_view.xml' - ] } diff --git a/mail_debrand/models/__init__.py b/mail_debrand/models/__init__.py new file mode 100644 index 00000000..44e83956 --- /dev/null +++ b/mail_debrand/models/__init__.py @@ -0,0 +1 @@ +from . import mail_template diff --git a/mail_debrand/models/mail_template.py b/mail_debrand/models/mail_template.py new file mode 100644 index 00000000..0525f4f1 --- /dev/null +++ b/mail_debrand/models/mail_template.py @@ -0,0 +1,42 @@ +# Copyright 2019 O4SB - Graeme Gellatly +# Copyright 2019 Tecnativa - Ernesto Tejeda +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from lxml import html as htmltree +import re +from odoo import _, api, models + + +class MailTemplate(models.Model): + _inherit = "mail.template" + + @api.model + def _debrand_body(self, html): + using_word = _('using') + odoo_word = _('Odoo') + html = re.sub( + using_word + "(.*)[\r\n]*(.*)>" + odoo_word + r"", "", html, + ) + powered_by = _("Powered by") + if powered_by not in html: + return html + root = htmltree.fromstring(html) + powered_by_elements = root.xpath( + "//*[text()[contains(.,'%s')]]" % powered_by + ) + for elem in powered_by_elements: + # make sure it isn't a spurious powered by + if any( + [ + "www.odoo.com" in child.get("href", "") + for child in elem.getchildren() + ] + ): + for child in elem.getchildren(): + elem.remove(child) + elem.text = None + return htmltree.tostring(root).decode("utf-8") + + @api.model + def render_post_process(self, html): + html = super().render_post_process(html) + return self._debrand_body(html) diff --git a/mail_debrand/tests/__init__.py b/mail_debrand/tests/__init__.py new file mode 100644 index 00000000..e7ef9cb4 --- /dev/null +++ b/mail_debrand/tests/__init__.py @@ -0,0 +1 @@ +from . import test_mail_debrand diff --git a/mail_debrand/tests/test_mail_debrand.py b/mail_debrand/tests/test_mail_debrand.py new file mode 100644 index 00000000..517d3f33 --- /dev/null +++ b/mail_debrand/tests/test_mail_debrand.py @@ -0,0 +1,25 @@ +# Copyright 2017 Tecnativa - Pedro M. Baeza +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo.tests import common + + +class TestMailDebrand(common.TransactionCase): + def setUp(self): + super().setUp() + self.default_arch = self.env.ref( + 'mail.message_notification_email' + ).arch + self.paynow_arch = self.env.ref( + 'mail.mail_notification_paynow' + ).arch + + def test_default_debrand(self): + self.assertIn('using', self.default_arch) + res = self.env["mail.template"]._debrand_body(self.default_arch) + self.assertNotIn('using', res) + + def test_paynow_debrand(self): + self.assertIn('Powered by', self.paynow_arch) + res = self.env["mail.template"]._debrand_body(self.paynow_arch) + self.assertNotIn('Powered by', res) diff --git a/mail_debrand/views/mail_notification_view.xml b/mail_debrand/views/mail_notification_view.xml deleted file mode 100644 index 5a3a9476..00000000 --- a/mail_debrand/views/mail_notification_view.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - - - - - -