diff options
| author | Thibault Delavallée <tde@openerp.com> | 2015-03-30 12:52:26 +0200 |
|---|---|---|
| committer | Thibault Delavallée <tde@openerp.com> | 2015-04-27 10:36:15 +0200 |
| commit | 4b122ad41d873febe288ab1f8c6cdc078ca2a479 (patch) | |
| tree | 9792fcbc0c61274ca6296e3ce6c2f67a2467b377 /addons/mail/wizard | |
| parent | b7f04278bd5375d6dc21a3381df91c725bb2fd44 (diff) | |
[MIGR] mail: migration to the new API
Diffstat (limited to 'addons/mail/wizard')
| -rw-r--r-- | addons/mail/wizard/email_template_preview.py | 105 | ||||
| -rw-r--r-- | addons/mail/wizard/email_template_preview_view.xml | 3 | ||||
| -rw-r--r-- | addons/mail/wizard/invite.py | 96 | ||||
| -rw-r--r-- | addons/mail/wizard/mail_compose_message.py | 374 | ||||
| -rw-r--r-- | addons/mail/wizard/mail_compose_message_view.xml | 1 |
5 files changed, 234 insertions, 345 deletions
diff --git a/addons/mail/wizard/email_template_preview.py b/addons/mail/wizard/email_template_preview.py index 779cf70bf39..6bf64f8082b 100644 --- a/addons/mail/wizard/email_template_preview.py +++ b/addons/mail/wizard/email_template_preview.py @@ -1,86 +1,47 @@ # -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (C) 2009 Sharoon Thomas -# Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/> -# -############################################################################## -from openerp.osv import fields, osv +from openerp import api, fields, models -class email_template_preview(osv.osv_memory): + +class TemplatePreview(models.TransientModel): _inherit = "mail.template" _name = "email_template.preview" _description = "Email Template Preview" - def _get_records(self, cr, uid, context=None): - """ - Return Records of particular Email Template's Model - """ - if context is None: - context = {} - - template_id = context.get('template_id', False) + @api.model + def _get_records(self): + """ Return Records of particular Email Template's Model """ + template_id = self._context.get('template_id') + default_res_id = self._context.get('default_res_id') if not template_id: return [] - email_template = self.pool.get('mail.template') - template = email_template.browse(cr, uid, int(template_id), context=context) - template_object = template.model_id - model = self.pool[template_object.model] - record_ids = model.search(cr, uid, [], 0, 10, 'id', context=context) - default_id = context.get('default_res_id') - - if default_id and default_id not in record_ids: - record_ids.insert(0, default_id) - - return model.name_get(cr, uid, record_ids, context) + template = self.env['mail.template'].browse(int(template_id)) + records = self.env[template.model_id.model].search([], limit=10) + if default_res_id and default_res_id not in records: + records |= self.env[template.model_id.model].browse(default_res_id) + return records.name_get() + @api.model + def default_get(self, fields): + result = super(TemplatePreview, self).default_get(fields) - def default_get(self, cr, uid, fields, context=None): - if context is None: - context = {} - result = super(email_template_preview, self).default_get(cr, uid, fields, context=context) - - email_template = self.pool.get('mail.template') - template_id = context.get('template_id') if 'res_id' in fields and not result.get('res_id'): - records = self._get_records(cr, uid, context=context) - result['res_id'] = records and records[0][0] or False # select first record as a Default - if template_id and 'model_id' in fields and not result.get('model_id'): - result['model_id'] = email_template.read(cr, uid, int(template_id), ['model_id'], context).get('model_id', False) + records = self._get_records() + result['res_id'] = records and records[0][0] or False # select first record as a Default + if self._context.get('template_id') and 'model_id' in fields and not result.get('model_id'): + result['model_id'] = self.env['mail.template'].browse(self._context['template_id']).model_id.id return result - _columns = { - 'res_id': fields.selection(_get_records, 'Sample Document'), - 'partner_ids': fields.many2many('res.partner', string='Recipients'), - } - - def on_change_res_id(self, cr, uid, ids, res_id, context=None): - if context is None: - context = {'value': {}} - if not res_id or not context.get('template_id'): - return {'value': {}} - - email_template = self.pool.get('mail.template') - template_id = context.get('template_id') - template = email_template.browse(cr, uid, template_id, context=context) - - # generate and get template values - mail_values = email_template.generate_email(cr, uid, template_id, res_id, context=context) - vals = dict((field, mail_values.get(field, False)) for field in ('email_from', 'email_to', 'email_cc', 'reply_to', 'subject', 'body_html', 'partner_to', 'partner_ids', 'attachment_ids')) - vals['name'] = template.name - return {'value': vals} + res_id = fields.Selection(_get_records, 'Sample Document') + partner_ids = fields.Many2many('res.partner', string='Recipients') + + @api.onchange('res_id') + @api.multi + def on_change_res_id(self): + mail_values = {} + if self.res_id and self._context.get('template_id'): + template = self.env['mail.template'].browse(self._context['template_id']) + self.name = template.name + mail_values = template.generate_email(self.res_id) + for field in ['email_from', 'email_to', 'email_cc', 'reply_to', 'subject', 'body_html', 'partner_to', 'partner_ids', 'attachment_ids']: + setattr(self, field, mail_values.get(field, False)) diff --git a/addons/mail/wizard/email_template_preview_view.xml b/addons/mail/wizard/email_template_preview_view.xml index 8a22692e425..a108c92cc33 100644 --- a/addons/mail/wizard/email_template_preview_view.xml +++ b/addons/mail/wizard/email_template_preview_view.xml @@ -10,8 +10,7 @@ <field name="model_id" invisible="1"/> <h3>Preview of <field name="name" readonly="1" nolabel="1" class="oe_inline"/></h3> Choose an example <field name="model_id" class="oe_inline" readonly="1"/> record: - <field name="res_id" on_change="on_change_res_id(res_id, context)" class="oe_inline" - style="margin-left: 8px;"/> + <field name="res_id" class="oe_inline" style="margin-left: 8px;"/> <group> <field name="subject" readonly="1"/> <field name="email_from" readonly="1" diff --git a/addons/mail/wizard/invite.py b/addons/mail/wizard/invite.py index e3916ec5e45..ed04e43ec5a 100644 --- a/addons/mail/wizard/invite.py +++ b/addons/mail/wizard/invite.py @@ -1,104 +1,66 @@ # -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (C) 2012-Today OpenERP SA (<http://www.openerp.com>) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/> -# -############################################################################## +from openerp import _, api, fields, models from openerp import tools -from openerp.osv import osv -from openerp.osv import fields -from openerp.tools.translate import _ -class invite_wizard(osv.osv_memory): +class Invite(models.TransientModel): """ Wizard to invite partners and make them followers. """ _name = 'mail.wizard.invite' _description = 'Invite wizard' - def default_get(self, cr, uid, fields, context=None): - result = super(invite_wizard, self).default_get(cr, uid, fields, context=context) - user_name = self.pool.get('res.users').name_get(cr, uid, [uid], context=context)[0][1] + @api.model + def default_get(self, fields): + result = super(Invite, self).default_get(fields) + user_name = self.env.user.name_get()[0][1] model = result.get('res_model') res_id = result.get('res_id') if 'message' in fields and model and res_id: - ir_model = self.pool.get('ir.model') - model_ids = ir_model.search(cr, uid, [('model', '=', self.pool[model]._name)], context=context) - model_name = ir_model.name_get(cr, uid, model_ids, context=context)[0][1] - - document_name = self.pool[model].name_get(cr, uid, [res_id], context=context)[0][1] + model_name = self.env['ir.model'].search([('model', '=', self.pool[model]._name)]).name_get()[0][1] + document_name = self.env[model].browse(res_id).name_get()[0][1] message = _('<div><p>Hello,</p><p>%s invited you to follow %s document: %s.<p></div>') % (user_name, model_name, document_name) result['message'] = message elif 'message' in fields: result['message'] = _('<div><p>Hello,</p><p>%s invited you to follow a new document.</p></div>') % user_name return result - _columns = { - 'res_model': fields.char('Related Document Model', - required=True, select=1, - help='Model of the followed resource'), - 'res_id': fields.integer('Related Document ID', select=1, - help='Id of the followed resource'), - 'partner_ids': fields.many2many('res.partner', string='Recipients', - help="List of partners that will be added as follower of the current document."), - 'message': fields.html('Message'), - 'send_mail': fields.boolean('Send Email', - help="If checked, the partners will receive an email warning they have been " - "added in the document's followers."), - } - - _defaults = { - 'send_mail': True, - } + res_model = fields.Char('Related Document Model', required=True, select=1, help='Model of the followed resource') + res_id = fields.Integer('Related Document ID', select=1, help='Id of the followed resource') + partner_ids = fields.Many2many('res.partner', string='Recipients', help="List of partners that will be added as follower of the current document.") + message = fields.Html('Message') + send_mail = fields.Boolean('Send Email', default=True, help="If checked, the partners will receive an email warning they have been added in the document's followers.") - def add_followers(self, cr, uid, ids, context=None): - for wizard in self.browse(cr, uid, ids, context=context): - model_obj = self.pool[wizard.res_model] - document = model_obj.browse(cr, uid, wizard.res_id, context=context) + @api.multi + def add_followers(self): + email_from = self.env['mail.message']._get_default_from() + for wizard in self: + Model = self.env[wizard.res_model] + document = Model.browse(wizard.res_id) # filter partner_ids to get the new followers, to avoid sending email to already following partners - new_follower_ids = [p.id for p in wizard.partner_ids if p not in document.message_follower_ids] - model_obj.message_subscribe(cr, uid, [wizard.res_id], new_follower_ids, context=context) + new_followers = wizard.partner_ids - document.message_follower_ids + document.message_subscribe(new_followers.ids) - ir_model = self.pool.get('ir.model') - model_ids = ir_model.search(cr, uid, [('model', '=', model_obj._name)], context=context) - model_name = ir_model.name_get(cr, uid, model_ids, context=context)[0][1] + model_ids = self.env['ir.model'].search([('model', '=', wizard.res_model)]) + model_name = model_ids.name_get()[0][1] # send an email if option checked and if a message exists (do not send void emails) if wizard.send_mail and wizard.message and not wizard.message == '<br>': # when deleting the message, cleditor keeps a <br> # add signature # FIXME 8.0: use notification_email_send, send a wall message and let mail handle email notification + message box - signature_company = self.pool.get('mail.notification').get_signature_footer(cr, uid, user_id=uid, res_model=wizard.res_model, res_id=wizard.res_id, context=context) + signature_company = self.env['mail.notification'].get_signature_footer(user_id=self._uid, res_model=wizard.res_model, res_id=wizard.res_id) wizard.message = tools.append_content_to_html(wizard.message, signature_company, plaintext=False, container_tag='div') # send mail to new followers - # the invite wizard should create a private message not related to any object -> no model, no res_id - mail_mail = self.pool.get('mail.mail') - mail_id = mail_mail.create(cr, uid, { + self.env['mail.mail'].create({ 'model': wizard.res_model, 'res_id': wizard.res_id, 'record_name': document.name_get()[0][1], - 'email_from': self.pool['mail.message']._get_default_from(cr, uid, context=context), - 'reply_to': self.pool['mail.message']._get_default_from(cr, uid, context=context), + 'email_from': email_from, + 'reply_to': email_from, 'subject': _('Invitation to follow %s: %s') % (model_name, document.name_get()[0][1]), 'body_html': '%s' % wizard.message, 'auto_delete': True, - 'message_id': self.pool['mail.message']._get_message_id(cr, uid, {'no_auto_thread': True}, context=context), - 'recipient_ids': [(4, id) for id in new_follower_ids] - }, context=context) - mail_mail.send(cr, uid, [mail_id], context=context) + 'message_id': self.env['mail.message']._get_message_id({'no_auto_thread': True}), + 'recipient_ids': [(4, id) for id in new_followers.ids]}).send() return {'type': 'ir.actions.act_window_close'} diff --git a/addons/mail/wizard/mail_compose_message.py b/addons/mail/wizard/mail_compose_message.py index 0f62ab37184..b857ed08dc2 100644 --- a/addons/mail/wizard/mail_compose_message.py +++ b/addons/mail/wizard/mail_compose_message.py @@ -1,33 +1,12 @@ # -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (C) 2010-Today OpenERP SA (<http://www.openerp.com>) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/> -# -############################################################################## import base64 import re +from openerp import _, api, fields, models, SUPERUSER_ID from openerp import tools -from openerp import SUPERUSER_ID -from openerp.osv import osv -from openerp.osv import fields from openerp.tools.safe_eval import safe_eval as eval -from openerp.tools.translate import _ + # main mako-like expression pattern EXPRESSION_PATTERN = re.compile('(\$\{.+?\})') @@ -48,7 +27,7 @@ def _reopen(self, res_id, model): } -class mail_compose_message(osv.TransientModel): +class MailComposer(models.TransientModel): """ Generic message composition wizard. You may inherit from this wizard at model and view levels to provide specific features. @@ -64,7 +43,8 @@ class mail_compose_message(osv.TransientModel): _log_access = True _batch_size = 500 - def default_get(self, cr, uid, fields, context=None): + @api.model + def default_get(self, fields): """ Handle composition mode. Some details about context keys: - comment: default mode, model and ID of a record the user comments - default_model or active_model @@ -78,28 +58,26 @@ class mail_compose_message(osv.TransientModel): - active_ids: record IDs - default_model or active_model """ - if context is None: - context = {} - result = super(mail_compose_message, self).default_get(cr, uid, fields, context=context) + result = super(MailComposer, self).default_get(fields) # v6.1 compatibility mode - result['composition_mode'] = result.get('composition_mode', context.get('mail.compose.message.mode', 'comment')) - result['model'] = result.get('model', context.get('active_model')) - result['res_id'] = result.get('res_id', context.get('active_id')) - result['parent_id'] = result.get('parent_id', context.get('message_id')) + result['composition_mode'] = result.get('composition_mode', self._context.get('mail.compose.message.mode', 'comment')) + result['model'] = result.get('model', self._context.get('active_model')) + result['res_id'] = result.get('res_id', self._context.get('active_id')) + result['parent_id'] = result.get('parent_id', self._context.get('message_id')) - if not result['model'] or not self.pool.get(result['model']) or not hasattr(self.pool[result['model']], 'message_post'): + if not result['model'] or not result['model'] in self.pool or not hasattr(self.env[result['model']], 'message_post'): result['no_auto_thread'] = True # default values according to composition mode - NOTE: reply is deprecated, fall back on comment if result['composition_mode'] == 'reply': result['composition_mode'] = 'comment' vals = {} - if 'active_domain' in context: # not context.get() because we want to keep global [] domains + if 'active_domain' in self._context: # not context.get() because we want to keep global [] domains vals['use_active_domain'] = True - vals['active_domain'] = '%s' % context.get('active_domain') + vals['active_domain'] = '%s' % self._context.get('active_domain') if result['composition_mode'] == 'comment': - vals.update(self.get_record_data(cr, uid, result, context=context)) + vals.update(self.get_record_data(result)) for field in vals: if field in fields: @@ -112,20 +90,20 @@ class mail_compose_message(osv.TransientModel): # but when creating the mail.message to create the mail.compose.message # access rights issues may rise # We therefore directly change the model and res_id - if result['model'] == 'res.users' and result['res_id'] == uid: + if result['model'] == 'res.users' and result['res_id'] == self._uid: result['model'] = 'res.partner' - result['res_id'] = self.pool.get('res.users').browse(cr, uid, uid).partner_id.id + result['res_id'] = self.env.user.partner_id.id if fields is not None: [result.pop(field, None) for field in result.keys() if field not in fields] # Override to pre-fill the data when having a template in single-email mode # and not going through the view: the on_change is not called in that case. - if result.get('composition_mode') != 'mass_mail' and context.get('default_template_id') and result.get('model') and result.get('res_id'): + if result.get('composition_mode') != 'mass_mail' and self._context.get('default_template_id') and result.get('model') and result.get('res_id'): result.update( self.onchange_template_id( - cr, uid, [], context['default_template_id'], result.get('composition_mode'), - result.get('model'), result.get('res_id'), context=context + self._context['default_template_id'], result.get('composition_mode'), + result.get('model'), result.get('res_id') )['value'] ) if fields is not None: @@ -133,87 +111,78 @@ class mail_compose_message(osv.TransientModel): return result - def _get_composition_mode_selection(self, cr, uid, context=None): + @api.model + def _get_composition_mode_selection(self): return [('comment', 'Post on a document'), ('mass_mail', 'Email Mass Mailing'), ('mass_post', 'Post on Multiple Documents')] - _columns = { - 'composition_mode': fields.selection( - lambda s, *a, **k: s._get_composition_mode_selection(*a, **k), - string='Composition mode'), - 'partner_ids': fields.many2many('res.partner', - 'mail_compose_message_res_partner_rel', - 'wizard_id', 'partner_id', 'Additional Contacts'), - 'use_active_domain': fields.boolean('Use active domain'), - 'active_domain': fields.text('Active domain', readonly=True), - 'attachment_ids': fields.many2many('ir.attachment', - 'mail_compose_message_ir_attachments_rel', - 'wizard_id', 'attachment_id', 'Attachments'), - 'is_log': fields.boolean('Log an Internal Note', - help='Whether the message is an internal note (comment mode only)'), - # mass mode options - 'notify': fields.boolean('Notify followers', - help='Notify followers of the document (mass post only)'), - 'template_id': fields.many2one('mail.template', 'Use template', select=True), - } - _defaults = { - 'composition_mode': 'comment', - 'body': lambda self, cr, uid, ctx={}: '', - 'subject': lambda self, cr, uid, ctx={}: False, - 'partner_ids': lambda self, cr, uid, ctx={}: [], - } - - def check_access_rule(self, cr, uid, ids, operation, context=None): + composition_mode = fields.Selection(selection=_get_composition_mode_selection, string='Composition mode', default='comment') + partner_ids = fields.Many2many( + 'res.partner', 'mail_compose_message_res_partner_rel', + 'wizard_id', 'partner_id', 'Additional Contacts') + use_active_domain = fields.Boolean('Use active domain') + active_domain = fields.Text('Active domain', readonly=True) + attachment_ids = fields.Many2many( + 'ir.attachment', 'mail_compose_message_ir_attachments_rel', + 'wizard_id', 'attachment_id', 'Attachments') + is_log = fields.Boolean('Log an Internal Note', + help='Whether the message is an internal note (comment mode only)') + subject = fields.Char(default=False) + # mass mode options + notify = fields.Boolean('Notify followers', help='Notify followers of the document (mass post only)') + template_id = fields.Many2one( + 'mail.template', 'Use template', select=True, + domain="[('model', '=', model)]") + + @api.multi + def check_access_rule(self, operation): """ Access rules of mail.compose.message: - create: if - model, no res_id, I create a message in mass mail mode - then: fall back on mail.message acces rules """ - if isinstance(ids, (int, long)): - ids = [ids] - # Author condition (CREATE (mass_mail)) - if operation == 'create' and uid != SUPERUSER_ID: + if operation == 'create' and self._uid != SUPERUSER_ID: # read mail_compose_message.ids to have their values message_values = {} - cr.execute('SELECT DISTINCT id, model, res_id FROM "%s" WHERE id = ANY (%%s) AND res_id = 0' % self._table, (ids,)) - for id, rmod, rid in cr.fetchall(): - message_values[id] = {'model': rmod, 'res_id': rid} + self._cr.execute('SELECT DISTINCT id, model, res_id FROM "%s" WHERE id = ANY (%%s) AND res_id = 0' % self._table, (self.ids,)) + for mid, rmod, rid in self._cr.fetchall(): + message_values[mid] = {'model': rmod, 'res_id': rid} # remove from the set to check the ids that mail_compose_message accepts author_ids = [mid for mid, message in message_values.iteritems() - if message.get('model') and not message.get('res_id')] - ids = list(set(ids) - set(author_ids)) + if message.get('model') and not message.get('res_id')] + self = self.browse(list(set(self.ids) - set(author_ids))) # not sure slef = ... - return super(mail_compose_message, self).check_access_rule(cr, uid, ids, operation, context=context) + return super(MailComposer, self).check_access_rule(operation) - def _notify(self, cr, uid, newid, context=None, force_send=False, user_signature=True): + @api.multi + def _notify(self, force_send=False, user_signature=True): """ Override specific notify method of mail.message, because we do not want that feature in the wizard. """ return - def get_record_data(self, cr, uid, values, context=None): + @api.model + def get_record_data(self, values): """ Returns a defaults-like dict with initial values for the composition wizard when sending an email related a previous email (parent_id) or a document (model, res_id). This is based on previously computed default values. """ - if context is None: - context = {} result, subject = {}, False if values.get('parent_id'): - parent = self.pool.get('mail.message').browse(cr, uid, values.get('parent_id'), context=context) + parent = self.env['mail.message'].browse(values.get('parent_id')) result['record_name'] = parent.record_name, subject = tools.ustr(parent.subject or parent.record_name or '') if not values.get('model'): result['model'] = parent.model if not values.get('res_id'): result['res_id'] = parent.res_id - partner_ids = values.get('partner_ids', list()) + [partner.id for partner in parent.partner_ids] - if context.get('is_private') and parent.author_id: # check message is private then add author also in partner list. + partner_ids = values.get('partner_ids', list()) + parent.partner_ids.ids + if self._context.get('is_private') and parent.author_id: # check message is private then add author also in partner list. partner_ids += [parent.author_id.id] result['partner_ids'] = partner_ids elif values.get('model') and values.get('res_id'): - doc_name_get = self.pool[values.get('model')].name_get(cr, uid, [values.get('res_id')], context=context) + doc_name_get = self.env[values.get('model')].browse(values.get('res_id')).name_get() result['record_name'] = doc_name_get and doc_name_get[0][1] or '' subject = tools.ustr(result['record_name']) @@ -229,120 +198,118 @@ class mail_compose_message(osv.TransientModel): #------------------------------------------------------ # action buttons call with positionnal arguments only, so we need an intermediary function # to ensure the context is passed correctly - def send_mail_action(self, cr, uid, ids, context=None): - self.send_mail(cr, uid, ids, context=context) + @api.multi + def send_mail_action(self): + # TDE/ ??? + return self.send_mail() - def send_mail(self, cr, uid, ids, auto_commit=False, context=None): + @api.multi + def send_mail(self, auto_commit=False): """ Process the wizard content and proceed with sending the related email(s), rendering any template patterns on the fly if needed. """ - context = dict(context or {}) - context.pop('default_email_to', None) - context.pop('default_partner_ids', None) - - for wizard in self.browse(cr, uid, ids, context=context): - + for wizard in self: + Mail = self.env['mail.mail'] # Duplicate attachments linked to the email.template. # Indeed, basic mail.compose.message wizard duplicates attachments in mass # mailing mode. But in 'single post' mode, attachments of an email template # also have to be duplicated to avoid changing their ownership. if wizard.template_id: - context['mail_notify_user_signature'] = False # template user_signature is added when generating body_html - context['mail_auto_delete'] = wizard.template_id.auto_delete # mass mailing: use template auto_delete value -> note, for emails mass mailing only + # template user_signature is added when generating body_html + # mass mailing: use template auto_delete value -> note, for emails mass mailing only + Mail = Mail.with_context(mail_notify_user_signature=False, mail_auto_delete=wizard.template_id.auto_delete) if wizard.attachment_ids and wizard.composition_mode != 'mass_mail' and wizard.template_id: new_attachment_ids = [] for attachment in wizard.attachment_ids: if attachment in wizard.template_id.attachment_ids: - new_attachment_ids.append(self.pool.get('ir.attachment').copy(cr, uid, attachment.id, {'res_model': 'mail.compose.message', 'res_id': wizard.id}, context=context)) + new_attachment_ids.append(attachment.copy({'res_model': 'mail.compose.message', 'res_id': wizard.id}).id) else: new_attachment_ids.append(attachment.id) - self.write(cr, uid, wizard.id, {'attachment_ids': [(6, 0, new_attachment_ids)]}, context=context) + wizard.write({'attachment_ids': [(6, 0, new_attachment_ids)]}) # Mass Mailing mass_mode = wizard.composition_mode in ('mass_mail', 'mass_post') - active_model_pool = self.pool[wizard.model if wizard.model else 'mail.thread'] - if not hasattr(active_model_pool, 'message_post'): - context['thread_model'] = wizard.model - active_model_pool = self.pool['mail.thread'] - + ActiveModel = self.env[wizard.model if wizard.model else 'mail.thread'] + if not hasattr(ActiveModel, 'message_post'): + ActiveModel = self.env['mail.thread'].with_context(thread_model=wizard.model) + if wizard.composition_mode == 'mass_post': + # do not send emails directly but use the queue instead + # add context key to avoid subscribing the author + ActiveModel = ActiveModel.with_context(mail_notify_force_send=False, mail_create_nosubscribe=True) # wizard works in batch mode: [res_id] or active_ids or active_domain if mass_mode and wizard.use_active_domain and wizard.model: - res_ids = self.pool[wizard.model].search(cr, uid, eval(wizard.active_domain), context=context) - elif mass_mode and wizard.model and context.get('active_ids'): - res_ids = context['active_ids'] + res_ids = self.env[wizard.model].search(eval(wizard.active_domain)).ids + elif mass_mode and wizard.model and self._context.get('active_ids'): + res_ids = self._context['active_ids'] else: res_ids = [wizard.res_id] - batch_size = int(self.pool['ir.config_parameter'].get_param(cr, SUPERUSER_ID, 'mail.batch_size')) or self._batch_size + batch_size = int(self.env['ir.config_parameter'].sudo().get_param('mail.batch_size')) or self._batch_size sliced_res_ids = [res_ids[i:i + batch_size] for i in range(0, len(res_ids), batch_size)] - - mail_mail_ids = [] for res_ids in sliced_res_ids: - batch_mail_mail_ids = [] - all_mail_values = self.get_mail_values(cr, uid, wizard, res_ids, context=context) + batch_mails = Mail + all_mail_values = wizard.get_mail_values(res_ids) for res_id, mail_values in all_mail_values.iteritems(): if wizard.composition_mode == 'mass_mail': - mail_mail_id = self.pool['mail.mail'].create(cr, uid, mail_values, context=context) - batch_mail_mail_ids.append(mail_mail_id) + batch_mails |= Mail.create(mail_values) else: subtype = 'mail.mt_comment' if wizard.is_log or (wizard.composition_mode == 'mass_post' and not wizard.notify): # log a note: subtype is False subtype = False - if wizard.composition_mode == 'mass_post': - context = dict(context, - mail_notify_force_send=False, # do not send emails directly but use the queue instead - mail_create_nosubscribe=True) # add context key to avoid subscribing the author - active_model_pool.message_post(cr, uid, [res_id], type='comment', subtype=subtype, context=context, **mail_values) + ActiveModel.browse(res_id).message_post(message_type='comment', subtype=subtype, **mail_values) if wizard.composition_mode == 'mass_mail': - self.pool['mail.mail'].send(cr, uid, batch_mail_mail_ids, auto_commit=auto_commit, context=context) + batch_mails.send(auto_commit=auto_commit) return {'type': 'ir.actions.act_window_close'} - def get_mail_values(self, cr, uid, wizard, res_ids, context=None): + @api.multi + def get_mail_values(self, res_ids): """Generate the values that will be used by send_mail to create mail_messages or mail_mails. """ + self.ensure_one() results = dict.fromkeys(res_ids, False) - rendered_values, default_recipients = {}, {} - mass_mail_mode = wizard.composition_mode == 'mass_mail' + rendered_values = {} + mass_mail_mode = self.composition_mode == 'mass_mail' # render all template-based value at once - if mass_mail_mode and wizard.model: - rendered_values = self.render_message_batch(cr, uid, wizard, res_ids, context=context) + if mass_mail_mode and self.model: + rendered_values = self.render_message(res_ids) # compute alias-based reply-to in batch reply_to_value = dict.fromkeys(res_ids, None) - if mass_mail_mode and not wizard.no_auto_thread: - reply_to_value = self.pool['mail.thread'].message_get_reply_to(cr, uid, res_ids, default=wizard.email_from, context=dict(context, thread_model=wizard.model)) + if mass_mail_mode and not self.no_auto_thread: + # reply_to_value = self.env['mail.thread'].with_context(thread_model=self.model).browse(res_ids).message_get_reply_to(default=self.email_from) + reply_to_value = self.env['mail.thread'].with_context(thread_model=self.model).message_get_reply_to(res_ids, default=self.email_from) for res_id in res_ids: # static wizard (mail.message) values mail_values = { - 'subject': wizard.subject, - 'body': wizard.body, - 'parent_id': wizard.parent_id and wizard.parent_id.id, - 'partner_ids': [partner.id for partner in wizard.partner_ids], - 'attachment_ids': [attach.id for attach in wizard.attachment_ids], - 'author_id': wizard.author_id.id, - 'email_from': wizard.email_from, - 'record_name': wizard.record_name, - 'no_auto_thread': wizard.no_auto_thread, + 'subject': self.subject, + 'body': self.body, + 'parent_id': self.parent_id and self.parent_id.id, + 'partner_ids': [partner.id for partner in self.partner_ids], + 'attachment_ids': [attach.id for attach in self.attachment_ids], + 'author_id': self.author_id.id, + 'email_from': self.email_from, + 'record_name': self.record_name, + 'no_auto_thread': self.no_auto_thread, } # mass mailing: rendering override wizard static values - if mass_mail_mode and wizard.model: + if mass_mail_mode and self.model: # always keep a copy, reset record name (avoid browsing records) - mail_values.update(notification=True, model=wizard.model, res_id=res_id, record_name=False) + mail_values.update(notification=True, model=self.model, res_id=res_id, record_name=False) # auto deletion of mail_mail - if 'mail_auto_delete' in context: - mail_values['auto_delete'] = context.get('mail_auto_delete') + if 'mail_auto_delete' in self._context: + mail_values['auto_delete'] = self._context.get('mail_auto_delete') # rendered values using template email_dict = rendered_values[res_id] mail_values['partner_ids'] += email_dict.pop('partner_ids', []) mail_values.update(email_dict) - if not wizard.no_auto_thread: + if not self.no_auto_thread: mail_values.pop('reply_to') if reply_to_value.get(res_id): mail_values['reply_to'] = reply_to_value[res_id] - if wizard.no_auto_thread and not mail_values.get('reply_to'): + if self.no_auto_thread and not mail_values.get('reply_to'): mail_values['reply_to'] = mail_values['email_from'] # mail_mail values: body -> body_html, partner_ids -> recipient_ids mail_values['body_html'] = mail_values.get('body', '') @@ -352,11 +319,11 @@ class mail_compose_message(osv.TransientModel): mail_values['attachments'] = [(name, base64.b64decode(enc_cont)) for name, enc_cont in email_dict.pop('attachments', list())] attachment_ids = [] for attach_id in mail_values.pop('attachment_ids'): - new_attach_id = self.pool.get('ir.attachment').copy(cr, uid, attach_id, {'res_model': self._name, 'res_id': wizard.id}, context=context) - attachment_ids.append(new_attach_id) - mail_values['attachment_ids'] = self.pool['mail.thread']._message_preprocess_attachments( - cr, uid, mail_values.pop('attachments', []), - attachment_ids, 'mail.message', 0, context=context) + new_attach_id = self.env['ir.attachment'].browse(attach_id).copy({'res_model': self._name, 'res_id': self.id}) + attachment_ids.append(new_attach_id.id) + mail_values['attachment_ids'] = self.env['mail.thread']._message_preprocess_attachments( + mail_values.pop('attachments', []), + attachment_ids, 'mail.message', 0) results[res_id] = mail_values return results @@ -365,25 +332,34 @@ class mail_compose_message(osv.TransientModel): # Template methods #------------------------------------------------------ - def onchange_template_id(self, cr, uid, ids, template_id, composition_mode, model, res_id, context=None): + @api.multi + @api.onchange('template_id', 'composition_mode', 'model', 'res_id') + def onchange_template_id_wrapper(self): + self.ensure_one() + values = self.onchange_template_id(self.template_id.id, self.composition_mode, self.model, self.res_id)['value'] + for fname, value in values.iteritems(): + setattr(self, fname, value) + + @api.multi + def onchange_template_id(self, template_id, composition_mode, model, res_id): """ - mass_mailing: we cannot render, so return the template values - normal mode: return rendered values """ if template_id and composition_mode == 'mass_mail': + template = self.env['mail.template'].browse(template_id) fields = ['subject', 'body_html', 'email_from', 'reply_to', 'mail_server_id'] - template = self.pool['mail.template'].browse(cr, uid, template_id, context=context) values = dict((field, getattr(template, field)) for field in fields if getattr(template, field)) if template.attachment_ids: values['attachment_ids'] = [att.id for att in template.attachment_ids] if template.mail_server_id: values['mail_server_id'] = template.mail_server_id.id if template.user_signature and 'body_html' in values: - signature = self.pool.get('res.users').browse(cr, uid, uid, context).signature + signature = self.env.user.signature values['body_html'] = tools.append_content_to_html(values['body_html'], signature, plaintext=False) elif template_id: - values = self.generate_email_for_composer_batch(cr, uid, template_id, [res_id], context=context)[res_id] + values = self.generate_email_for_composer(template_id, [res_id])[res_id] # transform attachments into attachment_ids; not attached to the document because this will # be done further in the posting process, allowing to clean database if email not send - ir_attach_obj = self.pool.get('ir.attachment') + Attachment = self.env['ir.attachment'] for attach_fname, attach_datas in values.pop('attachments', []): data_attach = { 'name': attach_fname, @@ -393,47 +369,44 @@ class mail_compose_message(osv.TransientModel): 'res_id': 0, 'type': 'binary', # override default_type from context, possibly meant for another model! } - values.setdefault('attachment_ids', list()).append(ir_attach_obj.create(cr, uid, data_attach, context=context)) + values.setdefault('attachment_ids', list()).append(Attachment.create(data_attach).id) else: - default_context = dict(context, default_composition_mode=composition_mode, default_model=model, default_res_id=res_id) - default_values = self.default_get(cr, uid, ['composition_mode', 'model', 'res_id', 'parent_id', 'partner_ids', 'subject', 'body', 'email_from', 'reply_to', 'attachment_ids', 'mail_server_id'], context=default_context) + default_values = self.with_context(default_composition_mode=composition_mode, default_model=model, default_res_id=res_id).default_get(['composition_mode', 'model', 'res_id', 'parent_id', 'partner_ids', 'subject', 'body', 'email_from', 'reply_to', 'attachment_ids', 'mail_server_id']) values = dict((key, default_values[key]) for key in ['subject', 'body', 'partner_ids', 'email_from', 'reply_to', 'attachment_ids', 'mail_server_id'] if key in default_values) if values.get('body_html'): values['body'] = values.pop('body_html') return {'value': values} - def save_as_template(self, cr, uid, ids, context=None): + @api.multi + def save_as_template(self): """ hit save as template button: current form value will be a new template attached to the current document. """ - email_template = self.pool.get('mail.template') - ir_model_pool = self.pool.get('ir.model') - for record in self.browse(cr, uid, ids, context=context): - model_ids = ir_model_pool.search(cr, uid, [('model', '=', record.model or 'mail.message')], context=context) - model_id = model_ids and model_ids[0] or False + for record in self: + models = self.env['ir.model'].search([('model', '=', record.model or 'mail.message')]) model_name = '' - if model_id: - model_name = ir_model_pool.browse(cr, uid, model_id, context=context).name + if models: + model_name = models.name template_name = "%s: %s" % (model_name, tools.ustr(record.subject)) values = { 'name': template_name, 'subject': record.subject or False, 'body_html': record.body or False, - 'model_id': model_id or False, + 'model_id': models.id or False, 'attachment_ids': [(6, 0, [att.id for att in record.attachment_ids])], } - template_id = email_template.create(cr, uid, values, context=context) + template = self.env['mail.template'].create(values) # generate the saved template - template_values = record.onchange_template_id(template_id, record.composition_mode, record.model, record.res_id)['value'] - template_values['template_id'] = template_id - record.write(template_values) + record.write({'template_id': template.id}) + record.onchange_template_id_wrapper() return _reopen(self, record.id, record.model) #------------------------------------------------------ # Template rendering #------------------------------------------------------ - def render_message_batch(self, cr, uid, wizard, res_ids, context=None): + @api.multi + def render_message(self, res_ids): """Generate template-based values of wizard, for the document records given by res_ids. This method is meant to be inherited by email_template that will produce a more complete dictionary, using Jinja2 templates. @@ -452,13 +425,18 @@ class mail_compose_message(osv.TransientModel): :return dict results: for each res_id, the generated template values for subject, body, email_from and reply_to """ - subjects = self.render_template_batch(cr, uid, wizard.subject, wizard.model, res_ids, context=context) - bodies = self.render_template_batch(cr, uid, wizard.body, wizard.model, res_ids, context=context, post_process=True) - emails_from = self.render_template_batch(cr, uid, wizard.email_from, wizard.model, res_ids, context=context) - replies_to = self.render_template_batch(cr, uid, wizard.reply_to, wizard.model, res_ids, context=context) + self.ensure_one() + multi_mode = True + if isinstance(res_ids, (int, long)): + multi_mode = False + res_ids = [res_ids] + + subjects = self.render_template(self.subject, self.model, res_ids) + bodies = self.render_template(self.body, self.model, res_ids, post_process=True) + emails_from = self.render_template(self.email_from, self.model, res_ids) + replies_to = self.render_template(self.reply_to, self.model, res_ids) - ctx = dict(context, thread_model=wizard.model) - default_recipients = self.pool['mail.thread'].message_get_default_recipients(cr, uid, res_ids, context=ctx) + default_recipients = self.env['mail.thread'].message_get_default_recipients(res_model=self.model, res_ids=res_ids) results = dict.fromkeys(res_ids, False) for res_id in res_ids: @@ -471,11 +449,10 @@ class mail_compose_message(osv.TransientModel): results[res_id].update(default_recipients.get(res_id, dict())) # generate template-based values - if wizard.template_id: - template_values = self.generate_email_for_composer_batch( - cr, uid, wizard.template_id.id, res_ids, - fields=['email_to', 'partner_to', 'email_cc', 'attachment_ids', 'mail_server_id'], - context=context) + if self.template_id: + template_values = self.generate_email_for_composer( + self.template_id.id, res_ids, + fields=['email_to', 'partner_to', 'email_cc', 'attachment_ids', 'mail_server_id']) else: template_values = {} @@ -492,39 +469,30 @@ class mail_compose_message(osv.TransientModel): # update template values by composer values template_values[res_id].update(results[res_id]) - return template_values + return multi_mode and template_values or template_values[res_ids[0]] - #------------------------------------------------------ - # Wizard validation and send - #------------------------------------------------------ - - def generate_email_for_composer_batch(self, cr, uid, template_id, res_ids, context=None, fields=None): + @api.model + def generate_email_for_composer(self, template_id, res_ids, fields=None): """ Call email_template.generate_email(), get fields relevant for mail.compose.message, transform email_cc and email_to into partner_ids """ - if context is None: - context = {} + multi_mode = True + if isinstance(res_ids, (int, long)): + multi_mode = False + res_ids = [res_ids] + if fields is None: fields = ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc', 'reply_to', 'attachment_ids', 'mail_server_id'] returned_fields = fields + ['partner_ids', 'attachments'] values = dict.fromkeys(res_ids, False) - ctx = dict(context, tpl_partners_only=True) - template_values = self.pool.get('mail.template').generate_email_batch(cr, uid, template_id, res_ids, fields=fields, context=ctx) + template_values = self.env['mail.template'].with_context(tpl_partners_only=True).browse(template_id).generate_email(res_ids, fields=fields) for res_id in res_ids: res_id_values = dict((field, template_values[res_id][field]) for field in returned_fields if template_values[res_id].get(field)) res_id_values['body'] = res_id_values.pop('body_html', '') values[res_id] = res_id_values - return values - - def render_template_batch(self, cr, uid, template, model, res_ids, context=None, post_process=False): - return self.pool.get('mail.template').render_template_batch(cr, uid, template, model, res_ids, context=context, post_process=post_process) - - # Compatibility methods - def render_template(self, cr, uid, template, model, res_id, context=None): - return self.render_template_batch(cr, uid, template, model, [res_id], context)[res_id] - def render_message(self, cr, uid, wizard, res_id, context=None): - return self.render_message_batch(cr, uid, wizard, [res_id], context)[res_id] + return multi_mode and values or values[res_ids[0]] - def generate_email_for_composer(self, cr, uid, template_id, res_id, context=None): - return self.generate_email_for_composer_batch(cr, uid, template_id, [res_id], context)[res_id] + @api.model + def render_template(self, template, model, res_ids, post_process=False): + return self.env['mail.template'].render_template(template, model, res_ids, post_process=post_process) diff --git a/addons/mail/wizard/mail_compose_message_view.xml b/addons/mail/wizard/mail_compose_message_view.xml index 24a53b195e0..7a9d4b23042 100644 --- a/addons/mail/wizard/mail_compose_message_view.xml +++ b/addons/mail/wizard/mail_compose_message_view.xml @@ -69,7 +69,6 @@ <!--FIX: To avoid css issue of many2one field in footer temporary used oe_form (BUG:1152464)--> <field name="template_id" nolabel="1" class='oe_inline' options="{'no_create': True}" - on_change="onchange_template_id(template_id, composition_mode, model, res_id, context)" domain="[('model_id.model','=',model or 'mail.message')]" context="{'default_model': model, 'default_body_html': body, 'default_subject': subject}"/> </div> <button icon="/mail/static/src/img/email_template_save.png" |
