forked from gfcapalbo/social
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail_digest.py
283 lines (249 loc) · 9.6 KB
/
mail_digest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# -*- coding: utf-8 -*-
# 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, tools, _
import logging
logger = logging.getLogger('[mail_digest]')
class MailDigest(models.Model):
_name = 'mail.digest'
_description = 'Mail digest'
_order = 'create_date desc'
name = fields.Char(
string="Name",
compute="_compute_name",
readonly=True,
)
partner_id = fields.Many2one(
string='Partner',
comodel_name='res.partner',
readonly=True,
required=True,
ondelete='cascade',
)
frequency = fields.Selection(
related='partner_id.notify_frequency',
readonly=True,
)
message_ids = fields.Many2many(
comodel_name='mail.message',
string='Messages'
)
mail_id = fields.Many2one(
'mail.mail',
'Mail',
ondelete='set null',
)
state = fields.Selection(related='mail_id.state', readonly=True)
# To my future self: never ever change this field to `template_id`.
# When creating digest records within the context of mail composer
# (and possibly other contexts) you'll have a `default_template_id`
# key in the context which is going to override our safe default.
# This is going to break email generation because the template
# will be completely wrong. Lesson learned :)
digest_template_id = fields.Many2one(
'ir.ui.view',
'Qweb mail template',
ondelete='set null',
default=lambda self: self._default_digest_template_id(),
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."""
return self.env.ref('mail_digest.default_digest_tmpl',
raise_if_not_found=False)
@api.multi
@api.depends("partner_id", "partner_id.notify_frequency")
def _compute_name(self):
for rec in self:
rec.name = u'{} - {}'.format(
rec.partner_id.name, rec._get_subject())
@api.model
def create_or_update(self, partners, message, subtype_id=None):
"""Create or update digest.
:param partners: recipients as `res.partner` browse list
:param message: `mail.message` to include in digest
:param subtype_id: `mail.message.subtype` instance
"""
subtype_id = subtype_id or message.subtype_id
for partner in partners:
digest = self._get_or_create_by_partner(partner, message)
digest.message_ids |= message
return True
@api.model
def _get_by_partner(self, partner, mail_id=False):
"""Retrieve digest record for given partner.
:param partner: `res.partner` browse record
:param mail_id: `mail.mail` record for further filtering.
By default we lookup for pending digest without notification yet.
"""
domain = [
('partner_id', '=', partner.id),
('mail_id', '=', mail_id),
]
return self.search(domain, limit=1)
@api.model
def _get_or_create_by_partner(self, partner, message=None, mail_id=False):
"""Retrieve digest record or create it by partner.
:param partner: `res.partner` record to create/get digest for
:param message: `mail.message` to include in digest
:param mail_id: `mail.mail` record to set on digest
"""
existing = self._get_by_partner(partner, mail_id=mail_id)
if existing:
return existing
values = {'partner_id': partner.id, }
return self.create(values)
@api.model
def _message_group_by_key(self, msg):
"""Return the key to group messages by."""
return msg.subtype_id.id
@api.multi
def _message_group_by(self):
"""Group digest messages.
A digest can contain several messages.
To display them in a nice and organized form in your emails
we group them by subtype by default.
"""
self.ensure_one()
grouped = {}
for msg in self.message_ids:
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.
If you run a website we get website's name
otherwise we default to current user's company name.
"""
# default to company
name = self.env.user.company_id.name
if 'website' in self.env:
# TODO: shall we make this configurable at digest or global level?
# Maybe you have a website but
# your digest msgs are not related to it at all or partially.
ws = None
try:
ws = self.env['website'].get_current_website()
name = ws.name
except RuntimeError:
# RuntimeError: object unbound -> no website request.
# Fallback to default website if any.
ws = self.env['website'].search([], limit=1)
if ws:
name = ws.name
return name
@api.multi
def _get_subject(self):
"""Build the full subject for digest's mail."""
# TODO: shall we move this to computed field?
self.ensure_one()
subject = u'[{}] '.format(self._get_site_name())
if self.partner_id.notify_frequency == 'daily':
subject += _('Daily update')
elif self.partner_id.notify_frequency == 'weekly':
subject += _('Weekly update')
return subject
@api.multi
def _get_template_values(self):
"""Collect variables to render digest's template."""
self.ensure_one()
subject = self._get_subject()
template_values = {
'digest': self,
'subject': subject,
'grouped_messages': self._message_group_by(),
'base_url':
self.env['ir.config_parameter'].get_param('web.base.url'),
}
return template_values
@api.multi
def _get_email_values(self, template=None):
"""Collect variables to create digest's mail message."""
self.ensure_one()
template = template or self.digest_template_id
if not template:
raise exceptions.UserError(_(
'You must pass a template or set one on the digest record.'
))
subject = self._get_subject()
template_values = self._get_template_values()
values = {
'email_from': self.env.user.company_id.email,
'recipient_ids': [(4, self.partner_id.id)],
'subject': subject,
'body_html': template.with_context(
**self._template_context()
).render(template_values),
}
return values
def _create_mail_context(self):
"""Inject context vars.
By default we make sure that digest's email
will have only digest's partner among recipients.
"""
return {
'notify_only_recipients': True,
}
@api.multi
def _template_context(self):
"""Rendering context for digest's template.
By default we enforce partner's language.
"""
self.ensure_one()
return {
'lang': self.partner_id.lang,
}
@api.multi
def create_email(self, template=None):
"""Create `mail.message` records for current digests.
:param template: qweb template instance to override default digest one.
"""
mail_model = self.env['mail.mail'].with_context(
**self._create_mail_context())
created = []
for item in self:
if not item.message_ids:
# useless to create a mail for a digest w/ messages
# messages could be deleted by admin for instance.
continue
values = item.with_context(
**item._template_context()
)._get_email_values(template=template)
item.mail_id = mail_model.create(values)
created.append(item.id)
if created:
logger.info('Create email for digest IDS=%s', str(created))
return created
@api.multi
def action_create_email(self):
return self.create_email()
@api.model
def process(self, frequency='daily', domain=None):
"""Process existing digest records to create emails via cron.
:param frequency: lookup digest records by partners' `notify_frequency`
:param domain: pass custom domain to lookup only specific digests
"""
if not domain:
domain = [
('mail_id', '=', False),
('partner_id.notify_frequency', '=', frequency),
]
self.search(domain).create_email()