1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>)
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.
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.
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/>
20 ##############################################################################
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 _
32 # main mako-like expression pattern
33 EXPRESSION_PATTERN = re.compile('(\$\{.+?\})')
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.
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
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.
49 _name = 'mail.compose.message'
50 _inherit = 'mail.message'
51 _description = 'Email composition wizard'
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
70 result = super(mail_compose_message, self).default_get(cr, uid, fields, context=context)
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'))
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'
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)
93 result[field] = vals[field]
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
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')]
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'),
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={}: [],
142 def check_access_rule(self, cr, uid, ids, operation, context=None):
143 """ Access rules of mail.compose.message:
145 - model, no res_id, I create a message in mass mail mode
146 - then: fall back on mail.message acces rules
148 if isinstance(ids, (int, long)):
151 # Author condition (CREATE (mass_mail))
152 if operation == 'create' and uid != SUPERUSER_ID:
153 # read mail_compose_message.ids to have their 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))
163 return super(mail_compose_message, self).check_access_rule(cr, uid, ids, operation, context=context)
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. """
170 def get_recipients_data(self, cr, uid, values, context=None):
171 """ Returns a string explaining the targetted recipients, to ease the use
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())
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 '' + ')'
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``.
190 :param str model: model name of the document record this mail is
192 :param int res_id: id of the document record this mail is related to
194 doc_name_get = self.pool[model].name_get(cr, uid, [res_id], context=context)
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,
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).
205 :param int message_id: id of the mail.message to which the user
212 message_data = self.pool.get('mail.message').browse(cr, uid, message_id, context=context)
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)
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]
227 'record_name': message_data.record_name,
228 'subject': reply_subject,
229 'partner_ids': partner_ids,
232 #------------------------------------------------------
233 # Wizard validation and send
234 #------------------------------------------------------
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. """
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)
246 active_ids = context.get('active_ids')
247 is_log = context.get('mail_compose_log', False)
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']
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:
262 res_ids = [wizard.res_id]
264 all_mail_values = self.get_mail_values(cr, uid, wizard, res_ids, context=context)
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', []),
275 mail_values['attachment_ids'] = m2m_attachment_ids
276 self.pool.get('mail.mail').create(cr, uid, mail_values, context=context)
278 subtype = 'mail.mt_comment'
279 if is_log or (wizard.composition_mode == 'mass_post' and not wizard.notify): # log a note: subtype is False
281 if wizard.composition_mode == 'mass_post':
282 context = dict(context,
283 mail_notify_force_send=False, # do not send emails directly but use the queue instead
284 mail_create_nosubscribe=True) # add context key to avoid subscribing the author
285 active_model_pool.message_post(cr, uid, [res_id], type='comment', subtype=subtype, context=context, **mail_values)
287 return {'type': 'ir.actions.act_window_close'}
289 def get_mail_values(self, cr, uid, wizard, res_ids, context=None):
290 """Generate the values that will be used by send_mail to create mail_messages
292 results = dict.fromkeys(res_ids, False)
293 mass_mail_mode = wizard.composition_mode == 'mass_mail'
295 # render all template-based value at once
296 if mass_mail_mode and wizard.model:
297 rendered_values = self.render_message_batch(cr, uid, wizard, res_ids, context=context)
299 for res_id in res_ids:
300 # static wizard (mail.message) values
302 'subject': wizard.subject,
304 'parent_id': wizard.parent_id and wizard.parent_id.id,
305 'partner_ids': [partner.id for partner in wizard.partner_ids],
306 'attachment_ids': [attach.id for attach in wizard.attachment_ids],
308 # mass mailing: rendering override wizard static values
309 if mass_mail_mode and wizard.model:
310 email_dict = rendered_values[res_id]
311 mail_values['partner_ids'] += email_dict.pop('partner_ids', [])
312 # process attachments: should not be encoded before being processed by message_post / mail_mail create
314 if email_dict.get('attachments'):
315 for name, enc_cont in email_dict.pop('attachments'):
316 attachments.append((name, base64.b64decode(enc_cont)))
317 mail_values['attachments'] = attachments
319 for attach_id in mail_values.pop('attachment_ids'):
320 new_attach_id = self.pool.get('ir.attachment').copy(cr, uid, attach_id, {'res_model': self._name, 'res_id': wizard.id}, context=context)
321 attachment_ids.append(new_attach_id)
322 mail_values['attachment_ids'] = attachment_ids
323 # email_from: mass mailing only can specify another email_from
324 if email_dict.get('email_from'):
325 mail_values['email_from'] = email_dict.pop('email_from')
326 # replies redirection: mass mailing only
327 if not wizard.same_thread:
328 mail_values['reply_to'] = email_dict.pop('reply_to', None)
330 email_dict.pop('reply_to', None)
331 mail_values.update(email_dict)
333 if 'mail_auto_delete' in context:
334 mail_values['auto_delete'] = context.get('mail_auto_delete')
335 mail_values['body_html'] = mail_values.get('body', '')
336 mail_values['recipient_ids'] = [(4, id) for id in mail_values.pop('partner_ids', [])]
337 results[res_id] = mail_values
340 #------------------------------------------------------
342 #------------------------------------------------------
344 def render_message_batch(self, cr, uid, wizard, res_ids, context=None):
345 """Generate template-based values of wizard, for the document records given
346 by res_ids. This method is meant to be inherited by email_template that
347 will produce a more complete dictionary, using Jinja2 templates.
349 Each template is generated for all res_ids, allowing to parse the template
350 once, and render it multiple times. This is useful for mass mailing where
351 template rendering represent a significant part of the process.
353 :param browse wizard: current mail.compose.message browse record
354 :param list res_ids: list of record ids
356 :return dict results: for each res_id, the generated template values for
357 subject, body, email_from and reply_to
359 subjects = self.render_template_batch(cr, uid, wizard.subject, wizard.model, res_ids, context)
360 bodies = self.render_template_batch(cr, uid, wizard.body, wizard.model, res_ids, context)
361 emails_from = self.render_template_batch(cr, uid, wizard.email_from, wizard.model, res_ids, context)
362 replies_to = self.render_template_batch(cr, uid, wizard.reply_to, wizard.model, res_ids, context)
364 results = dict.fromkeys(res_ids, False)
365 for res_id in res_ids:
367 'subject': subjects[res_id],
368 'body': bodies[res_id],
369 'email_from': emails_from[res_id],
370 'reply_to': replies_to[res_id],
374 def render_template_batch(self, cr, uid, template, model, res_ids, context=None):
375 """ Render the given template text, replace mako-like expressions ``${expr}``
376 with the result of evaluating these expressions with an evaluation context
379 * ``user``: browse_record of the current user
380 * ``object``: browse_record of the document record this mail is
382 * ``context``: the context passed to the mail composition wizard
384 :param str template: the template text to render
385 :param str model: model name of the document record this mail is related to
386 :param list res_ids: list of record ids
390 results = dict.fromkeys(res_ids, False)
392 for res_id in res_ids:
394 exp = str(match.group()[2:-1]).strip()
396 'user': self.pool.get('res.users').browse(cr, uid, uid, context=context),
397 'object': self.pool[model].browse(cr, uid, res_id, context=context),
398 'context': dict(context), # copy context to prevent side-effects of eval
400 return result and tools.ustr(result) or ''
401 results[res_id] = template and EXPRESSION_PATTERN.sub(merge, template)
404 # Compatibility methods
405 def render_template(self, cr, uid, template, model, res_id, context=None):
406 return self.render_template_batch(cr, uid, template, model, [res_id], context)[res_id]
408 def render_message(self, cr, uid, wizard, res_id, context=None):
409 return self.render_message_batch(cr, uid, wizard, [res_id], context)[res_id]