Skip to content

Commit

Permalink
mail_digest: improve message body rendering
Browse files Browse the repository at this point in the history
Message's body can contains styles and other stuff
that can screw the look and feel of digests' mails.

Now we sanitize it if `sanitize_msg_body` is set on the digest (default on).
  • Loading branch information
simahawk committed May 11, 2018
1 parent 64c95b6 commit 45cff79
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
24 changes: 22 additions & 2 deletions mail_digest/models/mail_digest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
# Copyright 2017 Simone Orsi <[email protected]>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from odoo import fields, models, api, exceptions, _

from odoo import fields, models, api, exceptions, tools, _
import logging

logger = logging.getLogger('[mail_digest]')
Expand Down Expand Up @@ -54,6 +53,14 @@ class MailDigest(models.Model):
domain=[('type', '=', 'qweb')],
oldname='template_id',
)
sanitize_msg_body = fields.Boolean(
string='Sanitize message body',
help='Collected messages can have different styles applied '
'on each element. If this flag is enabled (default) '
'each message content will be sanitized '
'before generating the email.',
default=True,
)

def _default_digest_template_id(self):
"""Retrieve default template to render digest."""
Expand Down Expand Up @@ -129,6 +136,19 @@ def _message_group_by(self):
grouped.setdefault(self._message_group_by_key(msg), []).append(msg)
return grouped

@api.model
def message_body(self, msg, strip_style=True):
"""Return body message prepared for email content.
Message's body can contains styles and other stuff
that can screw the look and feel of digests' mails.
Here we sanitize it if `sanitize_msg_body` is set on the digest.
"""
if not self.sanitize_msg_body:
return msg.body
return tools.html_sanitize(msg.body or '', strip_style=strip_style)

def _get_site_name(self):
"""Retrieve site name for meaningful mail subject.
Expand Down
2 changes: 1 addition & 1 deletion mail_digest/templates/digest_default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<t t-foreach="messages" t-as="msg">
<div style="margin:20px">
<h3 t-esc="msg.subject" />
<t t-raw="msg.body" />
<t t-raw="digest.message_body(msg)" />
</div>
</t>
</t>
Expand Down
25 changes: 25 additions & 0 deletions mail_digest/tests/test_digest.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,28 @@ def test_digest_template(self):
# raise error if no template found
with self.assertRaises(exceptions.UserError):
dig._get_email_values()

def test_digest_message_body_sanitize(self):
dig = self._create_for_partner(self.partner1)
message = self.message_model.create({
'body': '<p style="font-weight:bold">Body!</p>',
'subtype_id': self.subtype1.id,
'res_id': self.partner3.id,
'model': 'res.partner',
'partner_ids': [(4, self.partner1.id)]
})
body = dig.message_body(message)
self.assertEqual(body, '<p>Body!</p>')

def test_digest_message_body_no_sanitize(self):
dig = self._create_for_partner(self.partner1)
dig.sanitize_msg_body = False
message = self.message_model.create({
'body': '<p style="font-weight:bold">Body!</p>',
'subtype_id': self.subtype1.id,
'res_id': self.partner3.id,
'model': 'res.partner',
'partner_ids': [(4, self.partner1.id)]
})
body = dig.message_body(message)
self.assertEqual(body, '<p style="font-weight:bold">Body!</p>')

0 comments on commit 45cff79

Please sign in to comment.