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