[REVERT] mail: undo model-level change that can cause issue for stable deployments
[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 import SUPERUSER_ID
25 from openerp.osv import osv
26 from openerp.osv import fields
27 from openerp.tools.safe_eval import safe_eval as eval
28 from openerp.tools.translate import _
29
30 # main mako-like expression pattern
31 EXPRESSION_PATTERN = re.compile('(\$\{.+?\})')
32
33
34 class mail_compose_message(osv.TransientModel):
35     """ Generic message composition wizard. You may inherit from this wizard
36         at model and view levels to provide specific features.
37
38         The behavior of the wizard depends on the composition_mode field:
39         - 'reply': reply to a previous message. The wizard is pre-populated
40             via ``get_message_data``.
41         - 'comment': new post on a record. The wizard is pre-populated via
42             ``get_record_data``
43         - 'mass_mail': wizard in mass mailing mode where the mail details can
44             contain template placeholders that will be merged with actual data
45             before being sent to each recipient.
46     """
47     _name = 'mail.compose.message'
48     _inherit = 'mail.message'
49     _description = 'Email composition wizard'
50     _log_access = True
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         # get some important values from context
71         composition_mode = context.get('default_composition_mode', context.get('mail.compose.message.mode'))
72         model = context.get('default_model', context.get('active_model'))
73         res_id = context.get('default_res_id', context.get('active_id'))
74         message_id = context.get('default_parent_id', context.get('message_id', context.get('active_id')))
75         active_ids = context.get('active_ids')
76
77         # get default values according to the composition mode
78         if composition_mode == 'reply':
79             vals = self.get_message_data(cr, uid, message_id, context=context)
80         elif composition_mode == 'comment' and model and res_id:
81             vals = self.get_record_data(cr, uid, model, res_id, context=context)
82         elif composition_mode == 'mass_mail' and model and active_ids:
83             vals = {'model': model, 'res_id': res_id}
84         else:
85             vals = {'model': model, 'res_id': res_id}
86         if composition_mode:
87             vals['composition_mode'] = composition_mode
88
89         for field in vals:
90             if field in fields:
91                 result[field] = vals[field]
92
93         # TDE HACK: as mailboxes used default_model='res.users' and default_res_id=uid
94         # (because of lack of an accessible pid), creating a message on its own
95         # profile may crash (res_users does not allow writing on it)
96         # Posting on its own profile works (res_users redirect to res_partner)
97         # but when creating the mail.message to create the mail.compose.message
98         # access rights issues may rise
99         # We therefore directly change the model and res_id
100         if result.get('model') == 'res.users' and result.get('res_id') == uid:
101             result['model'] = 'res.partner'
102             result['res_id'] = self.pool.get('res.users').browse(cr, uid, uid).partner_id.id
103         return result
104
105     def _get_composition_mode_selection(self, cr, uid, context=None):
106         return [('comment', 'Comment a document'), ('reply', 'Reply to a message'), ('mass_mail', 'Mass mailing')]
107
108     _columns = {
109         'composition_mode': fields.selection(
110             lambda s, *a, **k: s._get_composition_mode_selection(*a, **k),
111             string='Composition mode'),
112         'partner_ids': fields.many2many('res.partner',
113             'mail_compose_message_res_partner_rel',
114             'wizard_id', 'partner_id', 'Additional contacts'),
115         'attachment_ids': fields.many2many('ir.attachment',
116             'mail_compose_message_ir_attachments_rel',
117             'wizard_id', 'attachment_id', 'Attachments'),
118         'filter_id': fields.many2one('ir.filters', 'Filters'),
119     }
120
121     _defaults = {
122         'composition_mode': 'comment',
123         'body': lambda self, cr, uid, ctx={}: '',
124         'subject': lambda self, cr, uid, ctx={}: False,
125         'partner_ids': lambda self, cr, uid, ctx={}: [],
126     }
127
128     def check_access_rule(self, cr, uid, ids, operation, context=None):
129         """ Access rules of mail.compose.message:
130             - create: if
131                 - model, no res_id, I create a message in mass mail mode
132             - then: fall back on mail.message acces rules
133         """
134         if isinstance(ids, (int, long)):
135             ids = [ids]
136
137         # Author condition (CREATE (mass_mail))
138         if operation == 'create' and uid != SUPERUSER_ID:
139             # read mail_compose_message.ids to have their values
140             message_values = {}
141             cr.execute('SELECT DISTINCT id, model, res_id FROM "%s" WHERE id = ANY (%%s) AND res_id = 0' % self._table, (ids,))
142             for id, rmod, rid in cr.fetchall():
143                 message_values[id] = {'model': rmod, 'res_id': rid}
144             # remove from the set to check the ids that mail_compose_message accepts
145             author_ids = [mid for mid, message in message_values.iteritems()
146                 if message.get('model') and not message.get('res_id')]
147             ids = list(set(ids) - set(author_ids))
148
149         return super(mail_compose_message, self).check_access_rule(cr, uid, ids, operation, context=context)
150
151     def _notify(self, cr, uid, newid, context=None):
152         """ Override specific notify method of mail.message, because we do
153             not want that feature in the wizard. """
154         return
155
156     def get_record_data(self, cr, uid, model, res_id, context=None):
157         """ Returns a defaults-like dict with initial values for the composition
158             wizard when sending an email related to the document record
159             identified by ``model`` and ``res_id``.
160
161             :param str model: model name of the document record this mail is
162                 related to.
163             :param int res_id: id of the document record this mail is related to
164         """
165         doc_name_get = self.pool.get(model).name_get(cr, uid, [res_id], context=context)
166         record_name = False
167         if doc_name_get:
168             record_name = doc_name_get[0][1]
169         values = {
170             'model': model,
171             'res_id': res_id,
172             'record_name': record_name,
173         }
174         if record_name:
175             values['subject'] = 'Re: %s' % record_name
176         return values
177
178     def get_message_data(self, cr, uid, message_id, context=None):
179         """ Returns a defaults-like dict with initial values for the composition
180             wizard when replying to the given message (e.g. including the quote
181             of the initial message, and the correct recipients).
182
183             :param int message_id: id of the mail.message to which the user
184                 is replying.
185         """
186         if not message_id:
187             return {}
188         if context is None:
189             context = {}
190         message_data = self.pool.get('mail.message').browse(cr, uid, message_id, context=context)
191
192         # create subject
193         re_prefix = _('Re:')
194         reply_subject = tools.ustr(message_data.subject or message_data.record_name or '')
195         if not (reply_subject.startswith('Re:') or reply_subject.startswith(re_prefix)) and message_data.subject:
196             reply_subject = "%s %s" % (re_prefix, reply_subject)
197         # get partner_ids from original message
198         partner_ids = [partner.id for partner in message_data.partner_ids] if message_data.partner_ids else []
199         partner_ids += context.get('default_partner_ids', [])
200
201         # update the result
202         result = {
203             'record_name': message_data.record_name,
204             'model': message_data.model,
205             'res_id': message_data.res_id,
206             'parent_id': message_data.id,
207             'subject': reply_subject,
208             'partner_ids': partner_ids,
209         }
210         return result
211
212     #------------------------------------------------------
213     # Wizard validation and send
214     #------------------------------------------------------
215
216     def send_mail(self, cr, uid, ids, context=None):
217         """ Process the wizard content and proceed with sending the related
218             email(s), rendering any template patterns on the fly if needed. """
219         if context is None:
220             context = {}
221         ir_attachment_obj = self.pool.get('ir.attachment')
222         active_ids = context.get('active_ids')
223         is_log = context.get('mail_compose_log', False)
224
225         for wizard in self.browse(cr, uid, ids, context=context):
226             mass_mail_mode = wizard.composition_mode == 'mass_mail'
227             active_model_pool_name = wizard.model if wizard.model else 'mail.thread'
228             active_model_pool = self.pool.get(active_model_pool_name)
229
230             # wizard works in batch mode: [res_id] or active_ids
231             res_ids = active_ids if mass_mail_mode and wizard.model and active_ids else [wizard.res_id]
232             for res_id in res_ids:
233                 # mail.message values, according to the wizard options
234                 post_values = {
235                     'subject': wizard.subject,
236                     'body': wizard.body,
237                     'parent_id': wizard.parent_id and wizard.parent_id.id,
238                     'partner_ids': [partner.id for partner in wizard.partner_ids],
239                     'attachment_ids': [attach.id for attach in wizard.attachment_ids],
240                 }
241                 # mass mailing: render and override default values
242                 if mass_mail_mode and wizard.model:
243                     email_dict = self.render_message(cr, uid, wizard, res_id, context=context)
244                     post_values['partner_ids'] += email_dict.pop('partner_ids', [])
245                     post_values['attachments'] = email_dict.pop('attachments', [])
246                     attachment_ids = []
247                     for attach_id in post_values.pop('attachment_ids'):
248                         new_attach_id = ir_attachment_obj.copy(cr, uid, attach_id, {'res_model': self._name, 'res_id': wizard.id}, context=context)
249                         attachment_ids.append(new_attach_id)
250                     post_values['attachment_ids'] = attachment_ids
251                     post_values.update(email_dict)
252                 # post the message
253                 subtype = 'mail.mt_comment'
254                 if is_log:  # log a note: subtype is False
255                     subtype = False
256                 elif mass_mail_mode:  # mass mail: is a log pushed to recipients, author not added
257                     subtype = False
258                     context = dict(context, mail_create_nosubscribe=True)  # add context key to avoid subscribing the author
259                 msg_id = active_model_pool.message_post(cr, uid, [res_id], type='comment', subtype=subtype, context=context, **post_values)
260                 # mass_mailing: notify specific partners, because subtype was False, and no-one was notified
261                 if mass_mail_mode and post_values['partner_ids']:
262                     self.pool.get('mail.notification')._notify(cr, uid, msg_id, post_values['partner_ids'], context=context)
263
264         return {'type': 'ir.actions.act_window_close'}
265
266     def render_message(self, cr, uid, wizard, res_id, context=None):
267         """ Generate an email from the template for given (wizard.model, res_id)
268             pair. This method is meant to be inherited by email_template that
269             will produce a more complete dictionary. """
270         return {
271             'subject': self.render_template(cr, uid, wizard.subject, wizard.model, res_id, context),
272             'body': self.render_template(cr, uid, wizard.body, wizard.model, res_id, context),
273         }
274
275     def render_template(self, cr, uid, template, model, res_id, context=None):
276         """ Render the given template text, replace mako-like expressions ``${expr}``
277             with the result of evaluating these expressions with an evaluation context
278             containing:
279
280                 * ``user``: browse_record of the current user
281                 * ``object``: browse_record of the document record this mail is
282                               related to
283                 * ``context``: the context passed to the mail composition wizard
284
285             :param str template: the template text to render
286             :param str model: model name of the document record this mail is related to.
287             :param int res_id: id of the document record this mail is related to.
288         """
289         if context is None:
290             context = {}
291
292         def merge(match):
293             exp = str(match.group()[2:-1]).strip()
294             result = eval(exp, {
295                 'user': self.pool.get('res.users').browse(cr, uid, uid, context=context),
296                 'object': self.pool.get(model).browse(cr, uid, res_id, context=context),
297                 'context': dict(context),  # copy context to prevent side-effects of eval
298                 })
299             return result and tools.ustr(result) or ''
300         return template and EXPRESSION_PATTERN.sub(merge, template)