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..dc2228be05 100644 --- a/mail_debrand/__manifest__.py +++ b/mail_debrand/__manifest__.py @@ -2,8 +2,8 @@ { "name": "Mail Debrand", - "summary": "Remove Odoo branding in sent emails", - "version": "12.0.1.0.0", + "summary": "Remove Odoo branding in sent emails.", + "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..b70a9f2d08 --- /dev/null +++ b/mail_debrand/models/__init__.py @@ -0,0 +1 @@ +from . import mail_thread diff --git a/mail_debrand/models/mail_thread.py b/mail_debrand/models/mail_thread.py new file mode 100644 index 0000000000..5333a742d8 --- /dev/null +++ b/mail_debrand/models/mail_thread.py @@ -0,0 +1,41 @@ +import lxml.html +import re +from odoo import _, api, models + + +class MailThread(models.AbstractModel): + _inherit = "mail.thread" + + @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 = lxml.html.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 lxml.html.tostring(root) + + @api.model + 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/__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..f781180af4 --- /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.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.thread"]._debrand_body(self.paynow_arch) + self.assertNotIn(b'Powered by', res)