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