Browse Source

mail_digest: add layout preview

pull/273/head
Simone Orsi 7 years ago
parent
commit
67517a0a6a
  1. 8
      mail_digest/README.rst
  2. 1
      mail_digest/__init__.py
  3. 1
      mail_digest/__manifest__.py
  4. 1
      mail_digest/controllers/__init__.py
  5. 136
      mail_digest/controllers/digest_layout_preview.py
  6. BIN
      mail_digest/images/digest_layout_preview.png
  7. 18
      mail_digest/templates/digest_layout_preview.xml
  8. 1
      mail_digest/tests/__init__.py
  9. 57
      mail_digest/tests/test_preview.py

8
mail_digest/README.rst

@ -65,6 +65,14 @@ NOTE: under the hood the digest notification logic excludes followers to be noti
since you really want to notify only mail.digest's partner. since you really want to notify only mail.digest's partner.
Digest rendering preview
------------------------
You can check how messages are formatted per each mail subtype by going to `/digest/layout-preview` in your browser.
.. image:: ./images/digest_layout_preview.png
Known issues / Roadmap Known issues / Roadmap
====================== ======================

1
mail_digest/__init__.py

@ -1 +1,2 @@
from . import controllers
from . import models from . import models

1
mail_digest/__manifest__.py

@ -20,6 +20,7 @@
'views/user_notification_views.xml', 'views/user_notification_views.xml',
'views/user_views.xml', 'views/user_views.xml',
'templates/digest_default.xml', 'templates/digest_default.xml',
'templates/digest_layout_preview.xml',
], ],
'images': [ 'images': [
'static/description/preview.png', 'static/description/preview.png',

1
mail_digest/controllers/__init__.py

@ -0,0 +1 @@
from . import digest_layout_preview

136
mail_digest/controllers/digest_layout_preview.py

@ -0,0 +1,136 @@
# -*- coding: utf-8 -*-
# Copyright 2018 Simone Orsi <simone.orsi@camptocamp.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import http
from odoo.http import request
import random
FAKE_NAMES = [
'Madison Castillo',
'Destiny Frost',
'Dennis Parrish',
'Christy Moore',
'Larry James',
'David Simmons',
'Dr. Francis Ramos',
'Michelle Williams',
'Allison Montgomery',
'Michelle Rodriguez',
'Gina Patel',
'Corey Ray',
'Brent Myers',
'Sydney Hicks',
'Austin Buckley',
'Patricia Jones DDS',
'Dylan Davila',
'Christopher Bolton',
'James Cline',
'Gary Johnson',
'Jennifer Reese',
'Kevin Davis',
'Sandra Robinson',
'Sara Warner',
'Jaime Dunn',
'Mark Austin',
'Kendra Nelson',
'Matthew White',
'Rebecca Berger',
'Amanda Thornton',
'Lorraine Schultz',
'Chelsea Daniel',
'Kayla Jackson',
'Melanie Grant',
'Oscar Jones',
'Jon Sanchez',
'Kevin Anderson',
'Yvonne Mullen',
'Jonathan King',
'Wendy Hernandez'
]
FAKE_NUMBERS = range(1, 30)
class DigestPreview(http.Controller):
digest_test_template = 'mail_digest.digest_layout_preview'
@http.route([
'/digest/layout-preview',
], type='http', auth='user')
def digest_test(self):
digest = self._fake_digest()
mail_values = digest._get_email_values()
values = {
'env': request.env,
'digest_html': mail_values['body_html'],
}
return request.render(self.digest_test_template, values)
def _fake_digest(self):
user = request.env.user
digest_model = request.env['mail.digest'].sudo()
digest = digest_model.new()
digest.partner_id = user.partner_id
digest.digest_template_id = digest._default_digest_template_id()
digest.message_ids = self._fake_messages()
digest.sanitize_msg_body = True
return digest
def _fake_messages(self):
messages = request.env['mail.message'].sudo()
subtype_model = request.env['mail.message.subtype'].sudo()
subtypes = subtype_model.search([])
records = request.env['res.partner'].sudo().search([])
# TODO: filter subtypes?
for i, subtype in enumerate(subtypes):
# generate a couple of messages for each type
for x in range(1, 3):
msg = messages.new()
msg.subtype_id = subtype
subject, body = self._fake_content(subtype, i, x)
msg.subject = subject
msg.body = body
msg.message_type = random.choice(
('email', 'comment', 'notification'))
msg.email_from = 'random@user%d.com' % i
msg.partner_ids = [(6, 0, request.env.user.partner_id.ids)]
if i + x % 2 == 0:
# relate a document
msg.model = records._name
msg.res_id = random.choice(records.ids)
messages += msg
return messages
def _fake_content(self, subtype, i, x):
subject = 'Lorem ipsum %d / %d' % (i, x)
body = 'Random text here lorem ipsum %d / %d' % (i, x)
if i % 2 == 0 and x > 1:
# simulate also random styles that are goin to be stripped
body = """
<p style="font-size: 13px; font-family: &quot;Lucida Grande&quot;, Helvetica, Verdana, Arial, sans-serif; margin: 0px 0px 9px 0px">Lorem ipsum dolor sit amet, cetero menandri mel id.</p>
<p>Ad modus tantas qui, quo choro facete delicata te.
Epicurei accusata vix eu, prima erant graeci sit te,
vivendum molestiae an mel.</p>
<p>Sed apeirian atomorum id, no ius possit antiopam molestiae.</p>
""" # noqa
return subject, body.strip()
def _fake_tracking_vals(self):
tracking_model = request.env['mail.tracking.value'].sudo()
track_vals1 = tracking_model.create_tracking_values(
random.choice(FAKE_NAMES), random.choice(FAKE_NAMES),
'name', {'type': 'char', 'string': 'Name'},
)
track_vals2 = tracking_model.create_tracking_values(
random.choice(FAKE_NUMBERS), random.choice(FAKE_NUMBERS),
'count', {'type': 'integer', 'string': 'Count'},
)
return [
(0, 0, track_vals1),
(0, 0, track_vals2),
]

BIN
mail_digest/images/digest_layout_preview.png

After

Width: 691  |  Height: 1022  |  Size: 69 KiB

18
mail_digest/templates/digest_layout_preview.xml

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<template id="digest_layout_preview" name="Mail digest layout preview">
<style>
#digest_layout_wrapper {
max-width: 600px;
margin: 0 auto;
border: 1px dashed #ddd;
padding: 4em;
}
</style>
<div id="digest_layout_wrapper">
<t t-raw="digest_html" />
</div>
</template>
</odoo>

1
mail_digest/tests/__init__.py

@ -1,3 +1,4 @@
from . import test_digest from . import test_digest
from . import test_partner_domains from . import test_partner_domains
from . import test_subtypes_conf from . import test_subtypes_conf
from . import test_preview

57
mail_digest/tests/test_preview.py

@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Simone Orsi <simone.orsi@camptocamp.com>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
from odoo.tests.common import SavepointCase
import mock
from ..controllers.digest_layout_preview import DigestPreview
REQUEST_PATH = 'odoo.addons.mail_digest.controllers.digest_layout_preview'
class PreviewCase(SavepointCase):
"""Easy tests for preview controller to make codecov happy."""
@classmethod
def setUpClass(cls):
super(PreviewCase, cls).setUpClass()
cls.ctrl = DigestPreview()
@mock.patch(REQUEST_PATH + '.request')
def test_fake_digest(self, patched_req):
patched_req.env = self.env
digest = self.ctrl._fake_digest()
self.assertEqual(
digest.partner_id, self.env.user.partner_id,
)
self.assertEqual(
digest.digest_template_id, digest._default_digest_template_id(),
)
self.assertTrue(digest.message_ids)
self.assertTrue(digest.sanitize_msg_body)
@mock.patch(REQUEST_PATH + '.request')
def test_fake_messages(self, patched_req):
patched_req.env = self.env
all_types = self.env['mail.message.subtype'].search([])
messages = self.ctrl._fake_messages()
self.assertEqual(
len(messages), len(all_types) * 2
)
@mock.patch(REQUEST_PATH + '.request')
def test_fake_content(self, patched_req):
patched_req.env = self.env
subj, body = self.ctrl._fake_content(None, 1, 2)
body = 'Random text here lorem ipsum 1 / 2'
self.assertEqual(subj, 'Lorem ipsum 1 / 2')
self.assertEqual(body, 'Random text here lorem ipsum 1 / 2')
subj, body = self.ctrl._fake_content(None, 2, 2)
self.assertEqual(subj, 'Lorem ipsum 2 / 2')
self.assertTrue(body.startswith('<p style="font-size: 13px;'))
@mock.patch(REQUEST_PATH + '.request')
def test_fake_tracking_vals(self, patched_req):
patched_req.env = self.env
vals = self.ctrl._fake_tracking_vals()
self.assertEqual(len(vals), 2)
Loading…
Cancel
Save