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