[MERGE] Forward port of addons until 8903
[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 from openerp import tools
25
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         # get some important values from context
73         composition_mode = context.get('default_composition_mode', context.get('mail.compose.message.mode'))
74         model = context.get('default_model', context.get('active_model'))
75         res_id = context.get('default_res_id', context.get('active_id'))
76         message_id = context.get('default_parent_id', context.get('message_id', context.get('active_id')))
77         active_ids = context.get('active_ids')
78
79         # get default values according to the composition mode
80         if composition_mode == 'reply':
81             vals = self.get_message_data(cr, uid, message_id, context=context)
82         elif composition_mode == 'comment' and model and res_id:
83             vals = self.get_record_data(cr, uid, model, res_id, context=context)
84         elif composition_mode == 'mass_mail' and model and active_ids:
85             vals = {'model': model, 'res_id': res_id}
86         else:
87             vals = {'model': model, 'res_id': res_id}
88         if composition_mode:
89             vals['composition_mode'] = composition_mode
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.get('model') == 'res.users' and result.get('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', 'Comment a document'), ('reply', 'Reply to a message'), ('mass_mail', 'Mass mailing')]
109
110     _columns = {
111         'composition_mode': fields.selection(
112             lambda s, *a, **k: s._get_composition_mode_selection(*a, **k),
113             string='Composition mode'),
114         'partner_ids': fields.many2many('res.partner',
115             'mail_compose_message_res_partner_rel',
116             'wizard_id', 'partner_id', 'Additional contacts'),
117         'post': fields.boolean('Post a copy in the document',
118             help='Post a copy of the message on the document communication history.'),
119         'notify': fields.boolean('Notify followers',
120             help='Notify followers of the document'),
121         'same_thread': fields.boolean('Replies in the document',
122             help='Replies to the messages will go into the selected document.'),
123         'attachment_ids': fields.many2many('ir.attachment',
124             'mail_compose_message_ir_attachments_rel',
125             'wizard_id', 'attachment_id', 'Attachments'),
126         'filter_id': fields.many2one('ir.filters', 'Filters'),
127     }
128
129     _defaults = {
130         'composition_mode': 'comment',
131         'email_from': lambda self, cr, uid, ctx={}: self.pool.get('mail.mail')._get_default_from(cr, uid, context=ctx),
132         'body': lambda self, cr, uid, ctx={}: '',
133         'subject': lambda self, cr, uid, ctx={}: False,
134         'partner_ids': lambda self, cr, uid, ctx={}: [],
135         'notify': lambda self, cr, uid, ctx={}: False,
136         'post': lambda self, cr, uid, ctx={}: True,
137         'same_thread': lambda self, cr, uid, ctx={}: True,
138     }
139
140     def check_access_rule(self, cr, uid, ids, operation, context=None):
141         """ Access rules of mail.compose.message:
142             - create: if
143                 - model, no res_id, I create a message in mass mail mode
144             - then: fall back on mail.message acces rules
145         """
146         if isinstance(ids, (int, long)):
147             ids = [ids]
148
149         # Author condition (CREATE (mass_mail))
150         if operation == 'create' and uid != SUPERUSER_ID:
151             # read mail_compose_message.ids to have their values
152             message_values = {}
153             cr.execute('SELECT DISTINCT id, model, res_id FROM "%s" WHERE id = ANY (%%s) AND res_id = 0' % self._table, (ids,))
154             for id, rmod, rid in cr.fetchall():
155                 message_values[id] = {'model': rmod, 'res_id': rid}
156             # remove from the set to check the ids that mail_compose_message accepts
157             author_ids = [mid for mid, message in message_values.iteritems()
158                 if message.get('model') and not message.get('res_id')]
159             ids = list(set(ids) - set(author_ids))
160
161         return super(mail_compose_message, self).check_access_rule(cr, uid, ids, operation, context=context)
162
163     def _notify(self, cr, uid, newid, context=None):
164         """ Override specific notify method of mail.message, because we do
165             not want that feature in the wizard. """
166         return
167
168     def get_record_data(self, cr, uid, model, res_id, context=None):
169         """ Returns a defaults-like dict with initial values for the composition
170             wizard when sending an email related to the document record
171             identified by ``model`` and ``res_id``.
172
173             :param str model: model name of the document record this mail is
174                 related to.
175             :param int res_id: id of the document record this mail is related to
176         """
177         doc_name_get = self.pool.get(model).name_get(cr, uid, [res_id], context=context)
178         if doc_name_get:
179             record_name = doc_name_get[0][1]
180         else:
181             record_name = False
182         return {'model': model, 'res_id': res_id, 'record_name': record_name}
183
184     def get_message_data(self, cr, uid, message_id, context=None):
185         """ Returns a defaults-like dict with initial values for the composition
186             wizard when replying to the given message (e.g. including the quote
187             of the initial message, and the correct recipients).
188
189             :param int message_id: id of the mail.message to which the user
190                 is replying.
191         """
192         if not message_id:
193             return {}
194         if context is None:
195             context = {}
196         message_data = self.pool.get('mail.message').browse(cr, uid, message_id, context=context)
197
198         # create subject
199         re_prefix = _('Re:')
200         reply_subject = tools.ustr(message_data.subject or '')
201         if not (reply_subject.startswith('Re:') or reply_subject.startswith(re_prefix)) and message_data.subject:
202             reply_subject = "%s %s" % (re_prefix, reply_subject)
203         # get partner_ids from original message
204         partner_ids = [partner.id for partner in message_data.partner_ids] if message_data.partner_ids else []
205         partner_ids += context.get('default_partner_ids', [])
206
207         # update the result
208         result = {
209             'record_name': message_data.record_name,
210             'model': message_data.model,
211             'res_id': message_data.res_id,
212             'parent_id': message_data.id,
213             'subject': reply_subject,
214             'partner_ids': partner_ids,
215         }
216         return result
217
218     #------------------------------------------------------
219     # Wizard validation and send
220     #------------------------------------------------------
221
222     def send_mail(self, cr, uid, ids, context=None):
223         """ Process the wizard content and proceed with sending the related
224             email(s), rendering any template patterns on the fly if needed. """
225         if context is None:
226             context = {}
227         active_ids = context.get('active_ids')
228         is_log = context.get('mail_compose_log', False)
229
230         for wizard in self.browse(cr, uid, ids, context=context):
231             mass_mail_mode = wizard.composition_mode == 'mass_mail'
232             if mass_mail_mode:  # mass mail: avoid any auto subscription because this could lead to people being follower of plenty of documents
233                 context['mail_create_nosubscribe'] = True
234             active_model_pool = self.pool.get(wizard.model if wizard.model else 'mail.thread')
235
236             # wizard works in batch mode: [res_id] or active_ids
237             res_ids = active_ids if mass_mail_mode and wizard.model and active_ids else [wizard.res_id]
238             for res_id in res_ids:
239                 # default values, according to the wizard options
240                 post_values = {
241                     'subject': wizard.subject,
242                     'body': wizard.body,
243                     'parent_id': wizard.parent_id and wizard.parent_id.id,
244                     'partner_ids': [partner.id for partner in wizard.partner_ids],
245                     'attachments': [(attach.datas_fname or attach.name, base64.b64decode(attach.datas)) for attach in wizard.attachment_ids],
246                 }
247                 # mass mailing: render and override default values
248                 if mass_mail_mode and wizard.model:
249                     email_dict = self.render_message(cr, uid, wizard, res_id, context=context)
250                     new_partner_ids = email_dict.pop('partner_ids', [])
251                     post_values['partner_ids'] += new_partner_ids
252                     new_attachments = email_dict.pop('attachments', [])
253                     post_values['attachments'] += new_attachments
254                     post_values.update(email_dict)
255                     # email_from: mass mailing only can specify another email_from
256                     if email_dict.get('email_from'):
257                         post_values['email_from'] = email_dict.pop('email_from')
258                     # replies redirection: mass mailing only
259                     if not wizard.same_thread:
260                         post_values['reply_to'] = email_dict.pop('reply_to')
261                 # clean the context (hint: mass mailing sets some default values that
262                 # could be wrongly interpreted by mail_mail)
263                 context.pop('default_email_to', None)
264                 context.pop('default_partner_ids', None)
265                 # post the message
266                 if mass_mail_mode and not wizard.post:
267                     post_values['recipient_ids'] = [(4, id) for id in post_values.pop('partner_ids', [])]
268                     self.pool.get('mail.mail').create(cr, uid, post_values, context=context)
269                 else:
270                     subtype = 'mail.mt_comment'
271                     if is_log:  # log a note: subtype is False
272                         subtype = False
273                     elif mass_mail_mode:  # mass mail: is a log pushed to recipients, author not added
274                         subtype = False
275                         context = dict(context, mail_create_nosubscribe=True)  # add context key to avoid subscribing the author
276                     msg_id = active_model_pool.message_post(cr, uid, [res_id], type='comment', subtype=subtype, context=context, **post_values)
277                     # mass_mailing, post without notify: notify specific partners
278                     if mass_mail_mode and not wizard.notify and post_values['partner_ids']:
279                         self.pool.get('mail.notification')._notify(cr, uid, msg_id, post_values['partner_ids'], context=context)
280
281         return {'type': 'ir.actions.act_window_close'}
282
283     def render_message(self, cr, uid, wizard, res_id, context=None):
284         """ Generate an email from the template for given (wizard.model, res_id)
285             pair. This method is meant to be inherited by email_template that
286             will produce a more complete dictionary. """
287         return {
288             'subject': self.render_template(cr, uid, wizard.subject, wizard.model, res_id, context),
289             'body': self.render_template(cr, uid, wizard.body, wizard.model, res_id, context),
290             'email_from': self.render_template(cr, uid, wizard.email_from, wizard.model, res_id, context),
291             'reply_to': self.render_template(cr, uid, wizard.reply_to, wizard.model, res_id, context),
292         }
293
294     def render_template(self, cr, uid, template, model, res_id, context=None):
295         """ Render the given template text, replace mako-like expressions ``${expr}``
296             with the result of evaluating these expressions with an evaluation context
297             containing:
298
299                 * ``user``: browse_record of the current user
300                 * ``object``: browse_record of the document record this mail is
301                               related to
302                 * ``context``: the context passed to the mail composition wizard
303
304             :param str template: the template text to render
305             :param str model: model name of the document record this mail is related to.
306             :param int res_id: id of the document record this mail is related to.
307         """
308         if context is None:
309             context = {}
310
311         def merge(match):
312             exp = str(match.group()[2:-1]).strip()
313             result = eval(exp, {
314                 'user': self.pool.get('res.users').browse(cr, uid, uid, context=context),
315                 'object': self.pool.get(model).browse(cr, uid, res_id, context=context),
316                 'context': dict(context),  # copy context to prevent side-effects of eval
317                 })
318             return result and tools.ustr(result) or ''
319         return template and EXPRESSION_PATTERN.sub(merge, template)