[MERGE] Sync with trunnk
[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         # clean the context (hint: mass mailing sets some default values that
242         # could be wrongly interpreted by mail_mail)
243         context.pop('default_email_to', None)
244         context.pop('default_partner_ids', None)
245
246         active_ids = context.get('active_ids')
247         is_log = context.get('mail_compose_log', False)
248
249         for wizard in self.browse(cr, uid, ids, context=context):
250             mass_mode = wizard.composition_mode in ('mass_mail', 'mass_post')
251             active_model_pool = self.pool[wizard.model if wizard.model else 'mail.thread']
252             if not hasattr(active_model_pool, 'message_post'):
253                 context['thread_model'] = wizard.model
254                 active_model_pool = self.pool['mail.thread']
255
256             # wizard works in batch mode: [res_id] or active_ids or active_domain
257             if mass_mode and wizard.use_active_domain and wizard.model:
258                 res_ids = self.pool[wizard.model].search(cr, uid, eval(wizard.active_domain), context=context)
259             elif mass_mode and wizard.model and active_ids:
260                 res_ids = active_ids
261             else:
262                 res_ids = [wizard.res_id]
263
264             all_mail_values = self.get_mail_values(cr, uid, wizard, res_ids, context=context)
265
266             for res_id, mail_values in all_mail_values.iteritems():
267                 if wizard.composition_mode == 'mass_mail':
268                     if wizard.same_thread:  # same thread: keep a copy of the message in the chatter to enable the reply redirection
269                         mail_values.update(notification=True, model=wizard.model, res_id=res_id)
270                     m2m_attachment_ids = self.pool['mail.thread']._message_preprocess_attachments(
271                         cr, uid, mail_values.pop('attachments', []),
272                         mail_values.pop('attachment_ids', []),
273                         'mail.message', 0,
274                         context=context)
275                     mail_values['attachment_ids'] = m2m_attachment_ids
276                     if not mail_values.get('reply_to'):
277                         mail_values['reply_to'] = mail_values['email_from']
278                     self.pool.get('mail.mail').create(cr, uid, mail_values, context=context)
279                 else:
280                     subtype = 'mail.mt_comment'
281                     if is_log or (wizard.composition_mode == 'mass_post' and not wizard.notify):  # log a note: subtype is False
282                         subtype = False
283                     if wizard.composition_mode == 'mass_post':
284                         context = dict(context,
285                                        mail_notify_force_send=False,  # do not send emails directly but use the queue instead
286                                        mail_create_nosubscribe=True)  # add context key to avoid subscribing the author
287                     active_model_pool.message_post(cr, uid, [res_id], type='comment', subtype=subtype, context=context, **mail_values)
288
289         return {'type': 'ir.actions.act_window_close'}
290
291     def get_mail_values(self, cr, uid, wizard, res_ids, context=None):
292         """Generate the values that will be used by send_mail to create mail_messages
293         or mail_mails. """
294         results = dict.fromkeys(res_ids, False)
295         mass_mail_mode = wizard.composition_mode == 'mass_mail'
296
297         # render all template-based value at once
298         if mass_mail_mode and wizard.model:
299             rendered_values = self.render_message_batch(cr, uid, wizard, res_ids, context=context)
300
301         for res_id in res_ids:
302             # static wizard (mail.message) values
303             mail_values = {
304                 'subject': wizard.subject,
305                 'body': wizard.body,
306                 'parent_id': wizard.parent_id and wizard.parent_id.id,
307                 'partner_ids': [partner.id for partner in wizard.partner_ids],
308                 'attachment_ids': [attach.id for attach in wizard.attachment_ids],
309             }
310             # mass mailing: rendering override wizard static values
311             if mass_mail_mode and wizard.model:
312                 email_dict = rendered_values[res_id]
313                 mail_values['partner_ids'] += email_dict.pop('partner_ids', [])
314                 # process attachments: should not be encoded before being processed by message_post / mail_mail create
315                 attachments = []
316                 if email_dict.get('attachments'):
317                     for name, enc_cont in email_dict.pop('attachments'):
318                         attachments.append((name, base64.b64decode(enc_cont)))
319                 mail_values['attachments'] = attachments
320                 attachment_ids = []
321                 for attach_id in mail_values.pop('attachment_ids'):
322                     new_attach_id = self.pool.get('ir.attachment').copy(cr, uid, attach_id, {'res_model': self._name, 'res_id': wizard.id}, context=context)
323                     attachment_ids.append(new_attach_id)
324                 mail_values['attachment_ids'] = attachment_ids
325                 # email_from: mass mailing only can specify another email_from
326                 if email_dict.get('email_from'):
327                     mail_values['email_from'] = email_dict.pop('email_from')
328                 # replies redirection: mass mailing only
329                 if wizard.same_thread and wizard.post:
330                     email_dict.pop('reply_to', None)
331                 else:
332                     mail_values['reply_to'] = email_dict.pop('reply_to', None)
333                 mail_values.update(email_dict)
334                 # mail_mail values
335                 if 'mail_auto_delete' in context:
336                     mail_values['auto_delete'] = context.get('mail_auto_delete')
337                 mail_values['body_html'] = mail_values.get('body', '')
338                 mail_values['recipient_ids'] = [(4, id) for id in mail_values.pop('partner_ids', [])]
339             results[res_id] = mail_values
340         return results
341
342     #------------------------------------------------------
343     # Template rendering
344     #------------------------------------------------------
345
346     def render_message_batch(self, cr, uid, wizard, res_ids, context=None):
347         """Generate template-based values of wizard, for the document records given
348         by res_ids. This method is meant to be inherited by email_template that
349         will produce a more complete dictionary, using Jinja2 templates.
350
351         Each template is generated for all res_ids, allowing to parse the template
352         once, and render it multiple times. This is useful for mass mailing where
353         template rendering represent a significant part of the process.
354
355         :param browse wizard: current mail.compose.message browse record
356         :param list res_ids: list of record ids
357
358         :return dict results: for each res_id, the generated template values for
359                               subject, body, email_from and reply_to
360         """
361         subjects = self.render_template_batch(cr, uid, wizard.subject, wizard.model, res_ids, context)
362         bodies = self.render_template_batch(cr, uid, wizard.body, wizard.model, res_ids, context)
363         emails_from = self.render_template_batch(cr, uid, wizard.email_from, wizard.model, res_ids, context)
364         replies_to = self.render_template_batch(cr, uid, wizard.reply_to, wizard.model, res_ids, context)
365
366         results = dict.fromkeys(res_ids, False)
367         for res_id in res_ids:
368             results[res_id] = {
369                 'subject': subjects[res_id],
370                 'body': bodies[res_id],
371                 'email_from': emails_from[res_id],
372                 'reply_to': replies_to[res_id],
373             }
374         return results
375
376     def render_template_batch(self, cr, uid, template, model, res_ids, context=None):
377         """ Render the given template text, replace mako-like expressions ``${expr}``
378         with the result of evaluating these expressions with an evaluation context
379         containing:
380
381             * ``user``: browse_record of the current user
382             * ``object``: browse_record of the document record this mail is
383                           related to
384             * ``context``: the context passed to the mail composition wizard
385
386         :param str template: the template text to render
387         :param str model: model name of the document record this mail is related to
388         :param list res_ids: list of record ids
389         """
390         if context is None:
391             context = {}
392         results = dict.fromkeys(res_ids, False)
393
394         for res_id in res_ids:
395             def merge(match):
396                 exp = str(match.group()[2:-1]).strip()
397                 result = eval(exp, {
398                     'user': self.pool.get('res.users').browse(cr, uid, uid, context=context),
399                     'object': self.pool[model].browse(cr, uid, res_id, context=context),
400                     'context': dict(context),  # copy context to prevent side-effects of eval
401                 })
402                 return result and tools.ustr(result) or ''
403             results[res_id] = template and EXPRESSION_PATTERN.sub(merge, template)
404         return results
405
406     # Compatibility methods
407     def render_template(self, cr, uid, template, model, res_id, context=None):
408         return self.render_template_batch(cr, uid, template, model, [res_id], context)[res_id]
409
410     def render_message(self, cr, uid, wizard, res_id, context=None):
411         return self.render_message_batch(cr, uid, wizard, [res_id], context)[res_id]