From 10936acdfbc548c8c625da1fbf019cae45c5227d Mon Sep 17 00:00:00 2001 From: Graeme Gellatly Date: Thu, 27 Dec 2018 21:26:36 +1300 Subject: [PATCH 1/2] POC Depower Mail --- mail_debrand/__init__.py | 1 + mail_debrand/__manifest__.py | 5 +-- mail_debrand/models/__init__.py | 1 + mail_debrand/models/mail_template.py | 39 +++++++++++++++++++ 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, 68 insertions(+), 33 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 e69de29bb2..0650744f6b 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 9d5f58eec3..b434802b72 100644 --- a/mail_debrand/__manifest__.py +++ b/mail_debrand/__manifest__.py @@ -3,7 +3,7 @@ { "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/", "author": "Odoo Community Association (OCA)", @@ -12,7 +12,4 @@ "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 0000000000..44e83956eb --- /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 0000000000..1f9ea74df0 --- /dev/null +++ b/mail_debrand/models/mail_template.py @@ -0,0 +1,39 @@ +from lxml import etree +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 = etree.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 str(etree.tostring(root)) + + @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 0000000000..e7ef9cb49e --- /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 0000000000..517d3f339c --- /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 5a3a9476d6..0000000000 --- a/mail_debrand/views/mail_notification_view.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - - - - - - - From 5b5fd67c421039fb3515fe89563ce9e117b80764 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Gil=20Sorribes?= Date: Fri, 4 Jan 2019 16:43:45 +0100 Subject: [PATCH 2/2] [FIX] Module mail_debrand --- mail_debrand/__manifest__.py | 2 +- mail_debrand/models/__init__.py | 2 +- .../{mail_template.py => mail_thread.py} | 18 +++++++----- mail_debrand/tests/test_mail_debrand.py | 6 ++-- mail_debrand/views/mail_notification_view.xml | 29 +++++++++++++++++++ 5 files changed, 44 insertions(+), 13 deletions(-) rename mail_debrand/models/{mail_template.py => mail_thread.py} (68%) create mode 100644 mail_debrand/views/mail_notification_view.xml diff --git a/mail_debrand/__manifest__.py b/mail_debrand/__manifest__.py index b434802b72..dc2228be05 100644 --- a/mail_debrand/__manifest__.py +++ b/mail_debrand/__manifest__.py @@ -2,7 +2,7 @@ { "name": "Mail Debrand", - "summary": "Remove Odoo branding in sent emails", + "summary": "Remove Odoo branding in sent emails.", "version": "12.0.2.0.0", "category": "Social Network", "website": "https://odoo-community.org/", diff --git a/mail_debrand/models/__init__.py b/mail_debrand/models/__init__.py index 44e83956eb..b70a9f2d08 100644 --- a/mail_debrand/models/__init__.py +++ b/mail_debrand/models/__init__.py @@ -1 +1 @@ -from . import mail_template +from . import mail_thread diff --git a/mail_debrand/models/mail_template.py b/mail_debrand/models/mail_thread.py similarity index 68% rename from mail_debrand/models/mail_template.py rename to mail_debrand/models/mail_thread.py index 1f9ea74df0..5333a742d8 100644 --- a/mail_debrand/models/mail_template.py +++ b/mail_debrand/models/mail_thread.py @@ -1,10 +1,10 @@ -from lxml import etree +import lxml.html import re from odoo import _, api, models -class MailTemplate(models.Model): - _inherit = "mail.template" +class MailThread(models.AbstractModel): + _inherit = "mail.thread" @api.model def _debrand_body(self, html): @@ -16,7 +16,7 @@ def _debrand_body(self, html): powered_by = _("Powered by") if powered_by not in html: return html - root = etree.fromstring(html) + root = lxml.html.fromstring(html) powered_by_elements = root.xpath( "//*[text()[contains(.,'%s')]]" % powered_by ) @@ -31,9 +31,11 @@ def _debrand_body(self, html): for child in elem.getchildren(): elem.remove(child) elem.text = None - return str(etree.tostring(root)) + return lxml.html.tostring(root) @api.model - def render_post_process(self, html): - html = super().render_post_process(html) - return self._debrand_body(html) + def _replace_local_links(self, html, base_url=None): + if not isinstance(html, str): + html = html.decode("utf-8") + html = self._debrand_body(html) + return super()._replace_local_links(html, base_url) diff --git a/mail_debrand/tests/test_mail_debrand.py b/mail_debrand/tests/test_mail_debrand.py index 517d3f339c..f781180af4 100644 --- a/mail_debrand/tests/test_mail_debrand.py +++ b/mail_debrand/tests/test_mail_debrand.py @@ -16,10 +16,10 @@ def setUp(self): def test_default_debrand(self): self.assertIn('using', self.default_arch) - res = self.env["mail.template"]._debrand_body(self.default_arch) + res = self.env["mail.thread"]._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) + res = self.env["mail.thread"]._debrand_body(self.paynow_arch) + self.assertNotIn(b'Powered by', res) diff --git a/mail_debrand/views/mail_notification_view.xml b/mail_debrand/views/mail_notification_view.xml new file mode 100644 index 0000000000..5a3a9476d6 --- /dev/null +++ b/mail_debrand/views/mail_notification_view.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + +