mail_inline_css: fix transform hook

The mail module offers a better hook to manipulate html: `render_post_process`.
By using this, we make sure the transform is applied properly
before html tags sanitizing happens,
which can alter - if not screw - the final result.

Finally, it simplifies code :)
pull/608/head
Simone Orsi 2020-04-08 08:50:06 +02:00
parent 56e0a29347
commit 67e4fc673a
1 changed files with 7 additions and 13 deletions

View File

@ -17,20 +17,14 @@ except (ImportError, IOError) as err: # pragma: no cover
class MailTemplate(models.Model): class MailTemplate(models.Model):
_inherit = "mail.template" _inherit = "mail.template"
def generate_email(self, res_ids, fields=None): def render_post_process(self, html):
"""Use `premailer` to convert styles to inline styles.""" html = super().render_post_process(html)
result = super().generate_email(res_ids, fields=fields) return self._premailer_apply_transform(html)
if isinstance(res_ids, int):
result["body_html"] = self._premailer_apply_transform(result["body_html"])
else:
for __, data in result.items():
data["body_html"] = self._premailer_apply_transform(data["body_html"])
return result
def _premailer_apply_transform(self, data_html): def _premailer_apply_transform(self, html):
if not data_html: if not html.strip():
return data_html return html
premailer = Premailer(html=data_html, **self._get_premailer_options()) premailer = Premailer(html=html, **self._get_premailer_options())
return premailer.transform() return premailer.transform()
def _get_premailer_options(self): def _get_premailer_options(self):