diff --git a/mail_debrand/models/mail_render_mixin.py b/mail_debrand/models/mail_render_mixin.py index c9cb74a4fe..27e8fbd70f 100644 --- a/mail_debrand/models/mail_render_mixin.py +++ b/mail_debrand/models/mail_render_mixin.py @@ -17,12 +17,14 @@ class MailRenderMixin(models.AbstractModel): def remove_href_odoo(self, value, remove_parent=True, to_keep=None): if len(value) < 20: return value - # value can be bytes type; ensure we get a proper string + # value can be bytes or markup; ensure we get a proper string and preserve type + back_to_bytes = False + back_to_markup = False if type(value) is bytes: back_to_bytes = True value = value.decode() - else: - back_to_bytes = False + if type(value) is Markup: + back_to_markup = True has_dev_odoo_link = re.search( r"", to_keep) if back_to_bytes: value = value.encode() + elif back_to_markup: + value = Markup(value) return value @api.model @@ -94,17 +98,3 @@ def _render_template( orginal_rendered[key] = self.remove_href_odoo(orginal_rendered[key]) return orginal_rendered - - def _replace_local_links(self, html, base_url=None): - message = super()._replace_local_links(html, base_url=base_url) - - wrapper = Markup if isinstance(message, Markup) else str - message = tools.ustr(message) - if isinstance(message, Markup): - wrapper = Markup - - message = re.sub( - r"""(Powered by\s(.*)Odoo)""", "
 
", message - ) - - return wrapper(message) diff --git a/mail_debrand/readme/CONTRIBUTORS.rst b/mail_debrand/readme/CONTRIBUTORS.rst index ba4dd9db08..1ff383edeb 100644 --- a/mail_debrand/readme/CONTRIBUTORS.rst +++ b/mail_debrand/readme/CONTRIBUTORS.rst @@ -4,3 +4,5 @@ * Pedro M. Baeza * João Marques + +* Stefan Rijnhart diff --git a/mail_debrand/tests/test_mail_debrand.py b/mail_debrand/tests/test_mail_debrand.py index 6814ca8b95..ab77995b50 100644 --- a/mail_debrand/tests/test_mail_debrand.py +++ b/mail_debrand/tests/test_mail_debrand.py @@ -8,9 +8,18 @@ class TestMailDebrand(common.TransactionCase): - def setUp(self): - super().setUp() - self.default_template = self.env.ref("mail.mail_notification_layout") + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.mail = cls.env["mail.mail"].create({ + "email_from": "customer@example.com", + "subject": "Hello", + "email_to": "contact@example.com", + "reply_to": "contact@example.com", + }) + lang_nl = cls.env.ref("base.lang_nl") + if not lang_nl.active: + lang_nl.toggle_active() def test_debrand_binary_value(self): """ @@ -23,28 +32,48 @@ def test_debrand_binary_value(self): except TypeError: self.fail("Debranding binary string raised TypeError") + def _test_debrand_by_lang(self, template_ref, lang, term): + body = self.env['ir.qweb']._render( + template_ref, + { + "message": self.mail, + "company": self.env.company, + }, + lang=lang.code, + minimal_qcontext=True, + ) + self.assertIn(term, body) + body_cleaned = self.env["mail.render.mixin"].remove_href_odoo(body) + self.assertNotIn(term, body_cleaned) + def test_default_debrand(self): - self.assertIn("Powered by", self.default_template.arch) - model = "mail.template" - res_ids = self.env[model].search([], limit=1).ids - res = self.env[model]._render_template( - self.default_template, - "ir.ui.view", - res_ids, + self._test_debrand_by_lang( + "mail.mail_notification_layout", + self.env.ref("base.lang_en"), + "Powered by", ) - self.assertNotIn("Powered by", res) - def test_plaintext_email(self): - MailMessage = self.env["mail.mail"] - email_values = { - "email_from": "customer@example.com", - "subject": "Hello", - "email_to": "contact@example.com", - "reply_to": "contact@example.com", - } - # No exception expected - MailMessage.create(email_values) + def test_default_debrand_translated(self): + self._test_debrand_by_lang( + "mail.mail_notification_layout", + self.env.ref("base.lang_nl"), + "Aangeboden door", + ) + + def test_light_debrand(self): + self._test_debrand_by_lang( + "mail.mail_notification_light", + self.env.ref("base.lang_en"), + "Powered by", + ) + def test_light_debrand_translated(self): + self._test_debrand_by_lang( + "mail.mail_notification_light", + self.env.ref("base.lang_nl"), + "Aangeboden door", + ) + def test_body_intact(self): """The message body should never be changed""" MailMessage = self.env["mail.mail"]