[IMP] mail.compose.message: remove attachment_ids from render_message as it should...
[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 re
23 from openerp import tools
24 from openerp.osv import osv
25 from openerp.osv import fields
26 from openerp.tools.safe_eval import safe_eval as eval
27 from openerp.tools.translate import _
28
29 # main mako-like expression pattern
30 EXPRESSION_PATTERN = re.compile('(\$\{.+?\})')
31
32
33 class mail_compose_message(osv.TransientModel):
34     """ Generic message composition wizard. You may inherit from this wizard
35         at model and view levels to provide specific features.
36
37         The behavior of the wizard depends on the composition_mode field:
38         - 'reply': reply to a previous message. The wizard is pre-populated
39             via ``get_message_data``.
40         - 'comment': new post on a record. The wizard is pre-populated via
41             ``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
51     def default_get(self, cr, uid, fields, context=None):
52         """ Handle composition mode. Some details about context keys:
53             - comment: default mode, model and ID of a record the user comments
54                 - default_model or active_model
55                 - default_res_id or active_id
56             - reply: active_id of a message the user replies to
57                 - default_parent_id or message_id or active_id: ID of the
58                     mail.message we reply to
59                 - message.res_model or default_model
60                 - message.res_id or default_res_id
61             - mass_mail: model and IDs of records the user mass-mails
62                 - active_ids: record IDs
63                 - default_model or active_model
64         """
65         if context is None:
66             context = {}
67         result = super(mail_compose_message, self).default_get(cr, uid, fields, context=context)
68
69         # get some important values from context
70         composition_mode = context.get('default_composition_mode', context.get('mail.compose.message.mode'))
71         model = context.get('default_model', context.get('active_model'))
72         res_id = context.get('default_res_id', context.get('active_id'))
73         message_id = context.get('default_parent_id', context.get('message_id', context.get('active_id')))
74         active_ids = context.get('active_ids')
75
76         # get default values according to the composition mode
77         if composition_mode == 'reply':
78             vals = self.get_message_data(cr, uid, message_id, context=context)
79         elif composition_mode == 'comment' and model and res_id:
80             vals = self.get_record_data(cr, uid, model, res_id, context=context)
81         elif composition_mode == 'mass_mail' and model and active_ids:
82             vals = {'model': model, 'res_id': res_id}
83         else:
84             vals = {'model': model, 'res_id': res_id}
85         if composition_mode:
86             vals['composition_mode'] = composition_mode
87
88         for field in vals:
89             if field in fields:
90                 result[field] = vals[field]
91
92         # TDE HACK: as mailboxes used default_model='res.users' and default_res_id=uid
93         # (because of lack of an accessible pid), creating a message on its own
94         # profile may crash (res_users does not allow writing on it)
95         # Posting on its own profile works (res_users redirect to res_partner)
96         # but when creating the mail.message to create the mail.compose.message
97         # access rights issues may rise
98         # We therefore directly change the model and res_id
99         if result.get('model') == 'res.users' and result.get('res_id') == uid:
100             result['model'] = 'res.partner'
101             result['res_id'] = self.pool.get('res.users').browse(cr, uid, uid).partner_id.id
102         return result
103
104     def _get_composition_mode_selection(self, cr, uid, context=None):
105         return [('comment', 'Comment a document'), ('reply', 'Reply to a message'), ('mass_mail', 'Mass mailing')]
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         'attachment_ids': fields.many2many('ir.attachment',
115             'mail_compose_message_ir_attachments_rel',
116             'wizard_id', 'attachment_id', 'Attachments'),
117         'filter_id': fields.many2one('ir.filters', 'Filters'),
118     }
119
120     _defaults = {
121         'composition_mode': 'comment',
122         'body': lambda self, cr, uid, ctx={}: '',
123         'subject': lambda self, cr, uid, ctx={}: False,
124         'partner_ids': lambda self, cr, uid, ctx={}: [],
125     }
126
127     def _notify(self, cr, uid, newid, context=None):
128         """ Override specific notify method of mail.message, because we do
129             not want that feature in the wizard. """
130         return
131
132     def get_record_data(self, cr, uid, model, res_id, context=None):
133         """ Returns a defaults-like dict with initial values for the composition
134             wizard when sending an email related to the document record
135             identified by ``model`` and ``res_id``.
136
137             :param str model: model name of the document record this mail is
138                 related to.
139             :param int res_id: id of the document record this mail is related to
140         """
141         doc_name_get = self.pool.get(model).name_get(cr, uid, [res_id], context=context)
142         if doc_name_get:
143             record_name = doc_name_get[0][1]
144         else:
145             record_name = False
146         return {'model': model, 'res_id': res_id, 'record_name': record_name}
147
148     def get_message_data(self, cr, uid, message_id, context=None):
149         """ Returns a defaults-like dict with initial values for the composition
150             wizard when replying to the given message (e.g. including the quote
151             of the initial message, and the correct recipients).
152
153             :param int message_id: id of the mail.message to which the user
154                 is replying.
155         """
156         if not message_id:
157             return {}
158         if context is None:
159             context = {}
160         message_data = self.pool.get('mail.message').browse(cr, uid, message_id, context=context)
161
162         # create subject
163         re_prefix = _('Re:')
164         reply_subject = tools.ustr(message_data.subject or '')
165         if not (reply_subject.startswith('Re:') or reply_subject.startswith(re_prefix)) and message_data.subject:
166             reply_subject = "%s %s" % (re_prefix, reply_subject)
167         # get partner_ids from original message
168         partner_ids = [partner.id for partner in message_data.partner_ids] if message_data.partner_ids else []
169         partner_ids += context.get('default_partner_ids', [])
170
171         # update the result
172         result = {
173             'record_name': message_data.record_name,
174             'model': message_data.model,
175             'res_id': message_data.res_id,
176             'parent_id': message_data.id,
177             'subject': reply_subject,
178             'partner_ids': partner_ids,
179         }
180         return result
181
182     #------------------------------------------------------
183     # Wizard validation and send
184     #------------------------------------------------------
185
186     def send_mail(self, cr, uid, ids, context=None):
187         """ Process the wizard content and proceed with sending the related
188             email(s), rendering any template patterns on the fly if needed. """
189         if context is None:
190             context = {}
191         ir_attachment_obj = self.pool.get('ir.attachment')
192         active_ids = context.get('active_ids')
193
194         for wizard in self.browse(cr, uid, ids, context=context):
195             mass_mail_mode = wizard.composition_mode == 'mass_mail'
196             active_model_pool_name = wizard.model if wizard.model else 'mail.thread'
197             active_model_pool = self.pool.get(active_model_pool_name)
198
199             # wizard works in batch mode: [res_id] or active_ids
200             res_ids = active_ids if mass_mail_mode and wizard.model and active_ids else [wizard.res_id]
201             for res_id in res_ids:
202                 # mail.message values, according to the wizard options
203                 post_values = {
204                     'subject': wizard.subject,
205                     'body': wizard.body,
206                     'parent_id': wizard.parent_id and wizard.parent_id.id,
207                     'partner_ids': [partner.id for partner in wizard.partner_ids],
208                     'attachment_ids': [attach.id for attach in wizard.attachment_ids],
209                 }
210                 # mass mailing: render and override default values
211                 if mass_mail_mode and wizard.model:
212                     email_dict = self.render_message(cr, uid, wizard, res_id, context=context)
213                     post_values['partner_ids'] += email_dict.pop('partner_ids', [])
214                     post_values['attachments'] = email_dict.pop('attachments', [])
215                     attachment_ids = []
216                     for attach_id in post_values.pop('attachment_ids'):
217                         new_attach_id = ir_attachment_obj.copy(cr, uid, attach_id, {'res_model': active_model_pool_name, 'res_id': res_id}, context=context)
218                         attachment_ids.append(new_attach_id)
219                     post_values['attachment_ids'] = attachment_ids
220                     post_values.update(email_dict)
221                 # post the message
222                 subtype = 'mail.mt_comment' if not context.get('mail_compose_log', False) else False
223                 active_model_pool.message_post(cr, uid, [res_id], type='comment', subtype=subtype, context=context, **post_values)
224
225             # mass mailing: delete mail.compose.message attachments, added by the user and that have been duplicated
226             if wizard.attachment_ids:
227                 ir_attachment_obj.unlink(cr, uid, [attach.id for attach in wizard.attachment_ids if attach.res_model == self._name], context=context)
228
229         return {'type': 'ir.actions.act_window_close'}
230
231     def render_message(self, cr, uid, wizard, res_id, context=None):
232         """ Generate an email from the template for given (wizard.model, res_id)
233             pair. This method is meant to be inherited by email_template that
234             will produce a more complete dictionary. """
235         return {
236             'subject': self.render_template(cr, uid, wizard.subject, wizard.model, res_id, context),
237             'body': self.render_template(cr, uid, wizard.body, wizard.model, res_id, context),
238         }
239
240     def render_template(self, cr, uid, template, model, res_id, context=None):
241         """ Render the given template text, replace mako-like expressions ``${expr}``
242             with the result of evaluating these expressions with an evaluation context
243             containing:
244
245                 * ``user``: browse_record of the current user
246                 * ``object``: browse_record of the document record this mail is
247                               related to
248                 * ``context``: the context passed to the mail composition wizard
249
250             :param str template: the template text to render
251             :param str model: model name of the document record this mail is related to.
252             :param int res_id: id of the document record this mail is related to.
253         """
254         if context is None:
255             context = {}
256
257         def merge(match):
258             exp = str(match.group()[2:-1]).strip()
259             result = eval(exp, {
260                 'user': self.pool.get('res.users').browse(cr, uid, uid, context=context),
261                 'object': self.pool.get(model).browse(cr, uid, res_id, context=context),
262                 'context': dict(context),  # copy context to prevent side-effects of eval
263                 })
264             return result and tools.ustr(result) or ''
265         return template and EXPRESSION_PATTERN.sub(merge, template)