[IMP] mail, email_template: still continuing work to improve performances of mass...
[odoo/odoo.git] / addons / mail / wizard / mail_compose_message.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation, either version 3 of the License, or
10 #    (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #    You should have received a copy of the GNU General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>
19 #
20 ##############################################################################
21
22 import base64
23 import re
24
25 from openerp import tools
26 from openerp import SUPERUSER_ID
27 from openerp.osv import osv
28 from openerp.osv import fields
29 from openerp.tools.safe_eval import safe_eval as eval
30 from openerp.tools.translate import _
31
32 # main mako-like expression pattern
33 EXPRESSION_PATTERN = re.compile('(\$\{.+?\})')
34
35
36 class mail_compose_message(osv.TransientModel):
37     """ Generic message composition wizard. You may inherit from this wizard
38         at model and view levels to provide specific features.
39
40         The behavior of the wizard depends on the composition_mode field:
41         - 'reply': reply to a previous message. The wizard is pre-populated
42             via ``get_message_data``.
43         - 'comment': new post on a record. The wizard is pre-populated via
44             ``get_record_data``
45         - 'mass_mail': wizard in mass mailing mode where the mail details can
46             contain template placeholders that will be merged with actual data
47             before being sent to each recipient.
48     """
49     _name = 'mail.compose.message'
50     _inherit = 'mail.message'
51     _description = 'Email composition wizard'
52     _log_access = True
53
54     def default_get(self, cr, uid, fields, context=None):
55         """ Handle composition mode. Some details about context keys:
56             - comment: default mode, model and ID of a record the user comments
57                 - default_model or active_model
58                 - default_res_id or active_id
59             - reply: active_id of a message the user replies to
60                 - default_parent_id or message_id or active_id: ID of the
61                     mail.message we reply to
62                 - message.res_model or default_model
63                 - message.res_id or default_res_id
64             - mass_mail: model and IDs of records the user mass-mails
65                 - active_ids: record IDs
66                 - default_model or active_model
67         """
68         if context is None:
69             context = {}
70         result = super(mail_compose_message, self).default_get(cr, uid, fields, context=context)
71
72         # v6.1 compatibility mode
73         result['composition_mode'] = result.get('composition_mode', context.get('mail.compose.message.mode'))
74         result['model'] = result.get('model', context.get('active_model'))
75         result['res_id'] = result.get('res_id', context.get('active_id'))
76         result['parent_id'] = result.get('parent_id', context.get('message_id'))
77
78         # default values according to composition mode - NOTE: reply is deprecated, fall back on comment
79         if result['composition_mode'] == 'reply':
80             result['composition_mode'] = 'comment'
81         vals = {}
82         if 'active_domain' in context:  # not context.get() because we want to keep global [] domains
83             vals['use_active_domain'] = True
84             vals['active_domain'] = '%s' % context.get('active_domain')
85         if result.get('parent_id'):
86             vals.update(self.get_message_data(cr, uid, result.get('parent_id'), context=context))
87         if result['composition_mode'] == 'comment' and result['model'] and result['res_id']:
88             vals.update(self.get_record_data(cr, uid, result['model'], result['res_id'], context=context))
89         result['recipients_data'] = self.get_recipients_data(cr, uid, result, context=context)
90
91         for field in vals:
92             if field in fields:
93                 result[field] = vals[field]
94
95         # TDE HACK: as mailboxes used default_model='res.users' and default_res_id=uid
96         # (because of lack of an accessible pid), creating a message on its own
97         # profile may crash (res_users does not allow writing on it)
98         # Posting on its own profile works (res_users redirect to res_partner)
99         # but when creating the mail.message to create the mail.compose.message
100         # access rights issues may rise
101         # We therefore directly change the model and res_id
102         if result['model'] == 'res.users' and result['res_id'] == uid:
103             result['model'] = 'res.partner'
104             result['res_id'] = self.pool.get('res.users').browse(cr, uid, uid).partner_id.id
105         return result
106
107     def _get_composition_mode_selection(self, cr, uid, context=None):
108         return [('comment', 'Post on a document'),
109                 ('mass_mail', 'Email Mass Mailing'),
110                 ('mass_post', 'Post on Multiple Documents')]
111
112     _columns = {
113         'composition_mode': fields.selection(
114             lambda s, *a, **k: s._get_composition_mode_selection(*a, **k),
115             string='Composition mode'),
116         'partner_ids': fields.many2many('res.partner',
117             'mail_compose_message_res_partner_rel',
118             'wizard_id', 'partner_id', 'Additional Contacts'),
119         'recipients_data': fields.text(string='Recipients Data',
120             help='Helper field used in mass mailing to display a sample of recipients'),
121         'use_active_domain': fields.boolean('Use active domain'),
122         'active_domain': fields.char('Active domain', readonly=True),
123         'notify': fields.boolean('Notify followers',
124             help='Notify followers of the document'),
125         'same_thread': fields.boolean('Replies in the document',
126             help='Replies to the messages will go into the selected document.'),
127         'attachment_ids': fields.many2many('ir.attachment',
128             'mail_compose_message_ir_attachments_rel',
129             'wizard_id', 'attachment_id', 'Attachments'),
130         'filter_id': fields.many2one('ir.filters', 'Filters'),
131     }
132     #TODO change same_thread to False in trunk (Require view update)
133     _defaults = {
134         'composition_mode': 'comment',
135         'body': lambda self, cr, uid, ctx={}: '',
136         'subject': lambda self, cr, uid, ctx={}: False,
137         'partner_ids': lambda self, cr, uid, ctx={}: [],
138         'notify': True,
139         'same_thread': True,
140     }
141
142     def check_access_rule(self, cr, uid, ids, operation, context=None):
143         """ Access rules of mail.compose.message:
144             - create: if
145                 - model, no res_id, I create a message in mass mail mode
146             - then: fall back on mail.message acces rules
147         """
148         if isinstance(ids, (int, long)):
149             ids = [ids]
150
151         # Author condition (CREATE (mass_mail))
152         if operation == 'create' and uid != SUPERUSER_ID:
153             # read mail_compose_message.ids to have their values
154             message_values = {}
155             cr.execute('SELECT DISTINCT id, model, res_id FROM "%s" WHERE id = ANY (%%s) AND res_id = 0' % self._table, (ids,))
156             for id, rmod, rid in cr.fetchall():
157                 message_values[id] = {'model': rmod, 'res_id': rid}
158             # remove from the set to check the ids that mail_compose_message accepts
159             author_ids = [mid for mid, message in message_values.iteritems()
160                 if message.get('model') and not message.get('res_id')]
161             ids = list(set(ids) - set(author_ids))
162
163         return super(mail_compose_message, self).check_access_rule(cr, uid, ids, operation, context=context)
164
165     def _notify(self, cr, uid, newid, context=None, force_send=False, user_signature=True):
166         """ Override specific notify method of mail.message, because we do
167             not want that feature in the wizard. """
168         return
169
170     def get_recipients_data(self, cr, uid, values, context=None):
171         """ Returns a string explaining the targetted recipients, to ease the use
172         of the wizard. """
173         composition_mode, model, res_id = values['composition_mode'], values['model'], values['res_id']
174         if composition_mode == 'comment' and model and res_id:
175             doc_name = self.pool[model].name_get(cr, uid, [res_id], context=context)
176             return doc_name and 'Followers of %s' % doc_name[0][1] or False
177         elif composition_mode == 'mass_post' and model:
178             active_ids = context.get('active_ids', list())
179             if not active_ids:
180                 return False
181             name_gets = [rec_name[1] for rec_name in self.pool[model].name_get(cr, uid, active_ids[:3], context=context)]
182             return 'Followers of selected documents (' + ', '.join(name_gets) + len(active_ids) > 3 and ', ...' or '' + ')'
183         return False
184
185     def get_record_data(self, cr, uid, model, res_id, context=None):
186         """ Returns a defaults-like dict with initial values for the composition
187             wizard when sending an email related to the document record
188             identified by ``model`` and ``res_id``.
189
190             :param str model: model name of the document record this mail is
191                 related to.
192             :param int res_id: id of the document record this mail is related to
193         """
194         doc_name_get = self.pool[model].name_get(cr, uid, [res_id], context=context)
195         return {
196             'record_name': doc_name_get and doc_name_get[0][1] or False,
197             'subject': doc_name_get and 'Re: %s' % doc_name_get[0][1] or False,
198         }
199
200     def get_message_data(self, cr, uid, message_id, context=None):
201         """ Returns a defaults-like dict with initial values for the composition
202             wizard when replying to the given message (e.g. including the quote
203             of the initial message, and the correct recipients).
204
205             :param int message_id: id of the mail.message to which the user
206                 is replying.
207         """
208         if not message_id:
209             return {}
210         if context is None:
211             context = {}
212         message_data = self.pool.get('mail.message').browse(cr, uid, message_id, context=context)
213
214         # create subject
215         re_prefix = _('Re:')
216         reply_subject = tools.ustr(message_data.subject or message_data.record_name or '')
217         if not (reply_subject.startswith('Re:') or reply_subject.startswith(re_prefix)):
218             reply_subject = "%s %s" % (re_prefix, reply_subject)
219
220         # get partner_ids from original message
221         partner_ids = [partner.id for partner in message_data.partner_ids] if message_data.partner_ids else []
222         partner_ids += context.get('default_partner_ids', [])
223         if context.get('is_private') and message_data.author_id:  # check message is private then add author also in partner list.
224             partner_ids += [message_data.author_id.id]
225         # update the result
226         return {
227             'record_name': message_data.record_name,
228             'subject': reply_subject,
229             'partner_ids': partner_ids,
230         }
231
232     #------------------------------------------------------
233     # Wizard validation and send
234     #------------------------------------------------------
235
236     def send_mail(self, cr, uid, ids, context=None):
237         """ Process the wizard content and proceed with sending the related
238             email(s), rendering any template patterns on the fly if needed. """
239         if context is None:
240             context = {}
241         import datetime
242         print '--> beginning sending email', datetime.datetime.now()
243         # clean the context (hint: mass mailing sets some default values that
244         # could be wrongly interpreted by mail_mail)
245         context.pop('default_email_to', None)
246         context.pop('default_partner_ids', None)
247
248         active_ids = context.get('active_ids')
249         is_log = context.get('mail_compose_log', False)
250
251         for wizard in self.browse(cr, uid, ids, context=context):
252             mass_mode = wizard.composition_mode in ('mass_mail', 'mass_post')
253             active_model_pool = self.pool[wizard.model if wizard.model else 'mail.thread']
254             if not hasattr(active_model_pool, 'message_post'):
255                 context['thread_model'] = wizard.model
256                 active_model_pool = self.pool['mail.thread']
257
258             # wizard works in batch mode: [res_id] or active_ids or active_domain
259             if mass_mode and wizard.use_active_domain and wizard.model:
260                 res_ids = self.pool[wizard.model].search(cr, uid, eval(wizard.active_domain), context=context)
261             elif mass_mode and wizard.model and active_ids:
262                 res_ids = active_ids
263             else:
264                 res_ids = [wizard.res_id]
265
266             print '----> before computing values', datetime.datetime.now()
267             all_mail_values = self.get_mail_values(cr, uid, wizard, res_ids, context=context)
268             print '----> after computing values', datetime.datetime.now()
269
270             for res_id, mail_values in all_mail_values.iteritems():
271                 if wizard.composition_mode == 'mass_mail':
272                     self.pool.get('mail.mail').create(cr, uid, mail_values, context=context)
273                 else:
274                     subtype = 'mail.mt_comment'
275                     if is_log or (wizard.composition_mode == 'mass_post' and not wizard.notify):  # log a note: subtype is False
276                         subtype = False
277                     if wizard.composition_mode == 'mass_post':
278                         context = dict(context,
279                                        mail_notify_force_send=False,  # do not send emails directly but use the queue instead
280                                        mail_create_nosubscribe=True)  # add context key to avoid subscribing the author
281                     active_model_pool.message_post(cr, uid, [res_id], type='comment', subtype=subtype, context=context, **mail_values)
282
283         print '--> finished sending email', datetime.datetime.now()
284         return {'type': 'ir.actions.act_window_close'}
285
286     def get_mail_values(self, cr, uid, wizard, res_ids, context=None):
287         """Generate the values that will be used by send_mail to create mail_messages
288         or mail_mails. """
289         results = dict.fromkeys(res_ids, False)
290         mass_mail_mode = wizard.composition_mode == 'mass_mail'
291
292         # render all template-based value at once
293         if mass_mail_mode and wizard.model:
294             rendered_values = self.render_message_batch(cr, uid, wizard, res_ids, context=context)
295
296         for res_id in res_ids:
297             # static wizard (mail.message) values
298             mail_values = {
299                 'subject': wizard.subject,
300                 'body': wizard.body,
301                 'parent_id': wizard.parent_id and wizard.parent_id.id,
302                 'partner_ids': [partner.id for partner in wizard.partner_ids],
303                 'attachment_ids': [attach.id for attach in wizard.attachment_ids],
304                 'author_id': wizard.author_id.id,
305                 'email_from': wizard.email_from,
306             }
307             # mass mailing: rendering override wizard static values
308             if mass_mail_mode and wizard.model:
309                 # rendered values using template
310                 email_dict = rendered_values[res_id]
311                 mail_values['partner_ids'] += email_dict.pop('partner_ids', [])
312                 # process attachments: should not be encoded before being processed by message_post / mail_mail create
313                 attachments = []
314                 if email_dict.get('attachments'):
315                     for name, enc_cont in email_dict.pop('attachments'):
316                         attachments.append((name, base64.b64decode(enc_cont)))
317                 mail_values['attachments'] = attachments
318                 attachment_ids = []
319                 for attach_id in mail_values.pop('attachment_ids'):
320                     new_attach_id = self.pool.get('ir.attachment').copy(cr, uid, attach_id, {'res_model': self._name, 'res_id': wizard.id}, context=context)
321                     attachment_ids.append(new_attach_id)
322                 mail_values['attachment_ids'] = attachment_ids
323                 # email_from: mass mailing only can specify another email_from
324                 if email_dict.get('email_from'):
325                     mail_values['email_from'] = email_dict.pop('email_from')
326                 # replies redirection: mass mailing only
327                 if wizard.same_thread:
328                     email_dict.pop('reply_to', None)
329                 else:
330                     mail_values['reply_to'] = email_dict.pop('reply_to', None)
331                 mail_values.update(email_dict)
332
333                 # value tweaking in mass mailing
334                 mail_values['record_name'] = False  # avoid browsing the record for an email
335                 if wizard.same_thread:  # same thread: keep a copy of the message in the chatter to enable the reply redirection
336                     mail_values.update(notification=True, model=wizard.model, res_id=res_id)
337                 m2m_attachment_ids = self.pool['mail.thread']._message_preprocess_attachments(
338                     cr, uid, mail_values.pop('attachments', []),
339                     mail_values.pop('attachment_ids', []),
340                     'mail.message', 0,
341                     context=context)
342                 mail_values['attachment_ids'] = m2m_attachment_ids
343                 if not mail_values.get('reply_to'):
344                     mail_values['reply_to'] = mail_values['email_from']
345
346                 # mail_mail values
347                 if 'mail_auto_delete' in context:
348                     mail_values['auto_delete'] = context.get('mail_auto_delete')
349                 mail_values['body_html'] = mail_values.get('body', '')
350                 mail_values['recipient_ids'] = [(4, id) for id in mail_values.pop('partner_ids', [])]
351
352             results[res_id] = mail_values
353         return results
354
355     #------------------------------------------------------
356     # Template rendering
357     #------------------------------------------------------
358
359     def render_message_batch(self, cr, uid, wizard, res_ids, context=None):
360         """Generate template-based values of wizard, for the document records given
361         by res_ids. This method is meant to be inherited by email_template that
362         will produce a more complete dictionary, using Jinja2 templates.
363
364         Each template is generated for all res_ids, allowing to parse the template
365         once, and render it multiple times. This is useful for mass mailing where
366         template rendering represent a significant part of the process.
367
368         :param browse wizard: current mail.compose.message browse record
369         :param list res_ids: list of record ids
370
371         :return dict results: for each res_id, the generated template values for
372                               subject, body, email_from and reply_to
373         """
374         subjects = self.render_template_batch(cr, uid, wizard.subject, wizard.model, res_ids, context)
375         bodies = self.render_template_batch(cr, uid, wizard.body, wizard.model, res_ids, context)
376         emails_from = self.render_template_batch(cr, uid, wizard.email_from, wizard.model, res_ids, context)
377         replies_to = self.render_template_batch(cr, uid, wizard.reply_to, wizard.model, res_ids, context)
378
379         results = dict.fromkeys(res_ids, False)
380         for res_id in res_ids:
381             results[res_id] = {
382                 'subject': subjects[res_id],
383                 'body': bodies[res_id],
384                 'email_from': emails_from[res_id],
385                 'reply_to': replies_to[res_id],
386             }
387         return results
388
389     def render_template_batch(self, cr, uid, template, model, res_ids, context=None):
390         """ Render the given template text, replace mako-like expressions ``${expr}``
391         with the result of evaluating these expressions with an evaluation context
392         containing:
393
394             * ``user``: browse_record of the current user
395             * ``object``: browse_record of the document record this mail is
396                           related to
397             * ``context``: the context passed to the mail composition wizard
398
399         :param str template: the template text to render
400         :param str model: model name of the document record this mail is related to
401         :param list res_ids: list of record ids
402         """
403         if context is None:
404             context = {}
405         results = dict.fromkeys(res_ids, False)
406
407         for res_id in res_ids:
408             def merge(match):
409                 exp = str(match.group()[2:-1]).strip()
410                 result = eval(exp, {
411                     'user': self.pool.get('res.users').browse(cr, uid, uid, context=context),
412                     'object': self.pool[model].browse(cr, uid, res_id, context=context),
413                     'context': dict(context),  # copy context to prevent side-effects of eval
414                 })
415                 return result and tools.ustr(result) or ''
416             results[res_id] = template and EXPRESSION_PATTERN.sub(merge, template)
417         return results
418
419     # Compatibility methods
420     def render_template(self, cr, uid, template, model, res_id, context=None):
421         return self.render_template_batch(cr, uid, template, model, [res_id], context)[res_id]
422
423     def render_message(self, cr, uid, wizard, res_id, context=None):
424         return self.render_message_batch(cr, uid, wizard, [res_id], context)[res_id]