Skip to content

Commit

Permalink
[FIX] mail_debrand: fix translated branding
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanRijnhart committed Dec 28, 2023
1 parent 241ab89 commit e0b6c8b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 43 deletions.
34 changes: 12 additions & 22 deletions mail_debrand/models/mail_render_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"<a\s(.*)dev\.odoo\.com", value, flags=re.IGNORECASE
)
Expand All @@ -40,18 +42,20 @@ def remove_href_odoo(self, value, remove_parent=True, to_keep=None):
# anchor <a href odoo has a parent powered by that must be removed
parent.getparent().remove(parent)
else:
# also here can be powered by
if parent.tag == "td" and parent.getparent():
parent.getparent().remove(parent)
else:
parent.remove(elem)
previous = elem.getprevious()
if previous is not None:
# Remove "Powered by", "using" etc.
previous.tail = ''
parent.remove(elem)
value = etree.tostring(
tree, pretty_print=True, method="html", encoding="unicode"
)
if to_keep:
value = value.replace("<body_msg></body_msg>", to_keep)
if back_to_bytes:
value = value.encode()
elif back_to_markup:
value = Markup(value)
return value

@api.model
Expand Down Expand Up @@ -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</a>)""", "<div>&nbsp;</div>", message
)

return wrapper(message)
2 changes: 2 additions & 0 deletions mail_debrand/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@

* Pedro M. Baeza
* João Marques

* Stefan Rijnhart <stefan@opener.amsterdam>
71 changes: 50 additions & 21 deletions mail_debrand/tests/test_mail_debrand.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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"]
Expand Down

0 comments on commit e0b6c8b

Please sign in to comment.