2011-02-23 09:27:08 +00:00
|
|
|
# -*- 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/>
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2012-11-16 17:02:59 +00:00
|
|
|
from openerp import tools
|
|
|
|
from openerp.osv import osv, fields
|
2012-10-17 09:29:38 +00:00
|
|
|
|
2012-11-15 12:24:06 +00:00
|
|
|
def _reopen(self, res_id, model):
|
|
|
|
return {'type': 'ir.actions.act_window',
|
|
|
|
'view_mode': 'form',
|
|
|
|
'view_type': 'form',
|
|
|
|
'res_id': res_id,
|
|
|
|
'res_model': self._name,
|
|
|
|
'target': 'new',
|
|
|
|
# save original model in context, because selecting the list of available
|
|
|
|
# templates requires a model in context
|
|
|
|
'context': {
|
|
|
|
'default_model': model,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2012-10-17 09:29:38 +00:00
|
|
|
class mail_compose_message(osv.TransientModel):
|
2011-08-25 12:27:25 +00:00
|
|
|
_inherit = 'mail.compose.message'
|
2011-03-01 10:28:52 +00:00
|
|
|
|
2011-02-23 09:27:08 +00:00
|
|
|
_columns = {
|
2013-03-27 15:17:27 +00:00
|
|
|
'template_id': fields.many2one('email.template', 'Use template', select=True),
|
2013-03-14 11:03:15 +00:00
|
|
|
'partner_to': fields.char('To (Partner IDs)',
|
2013-02-25 16:48:57 +00:00
|
|
|
help="Comma-separated list of recipient partners ids (placeholders may be used here)"),
|
2013-03-14 11:03:15 +00:00
|
|
|
'email_to': fields.char('To (Emails)',
|
2013-02-25 16:48:57 +00:00
|
|
|
help="Comma-separated recipient addresses (placeholders may be used here)",),
|
2013-03-14 11:03:15 +00:00
|
|
|
'email_cc': fields.char('Cc (Emails)',
|
2013-02-25 16:48:57 +00:00
|
|
|
help="Carbon copy recipients (placeholders may be used here)"),
|
|
|
|
}
|
|
|
|
|
2013-03-01 14:48:27 +00:00
|
|
|
def send_mail(self, cr, uid, ids, context=None):
|
|
|
|
""" Override of send_mail to 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. """
|
2013-06-06 12:37:24 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
|
|
|
wizard_context = dict(context)
|
2013-03-01 14:48:27 +00:00
|
|
|
for wizard in self.browse(cr, uid, ids, context=context):
|
2013-06-06 12:37:24 +00:00
|
|
|
if wizard.template_id and not wizard.template_id.user_signature:
|
|
|
|
wizard_context['mail_notify_user_signature'] = False # template user_signature is added when generating body_html
|
2013-03-01 14:48:27 +00:00
|
|
|
if not wizard.attachment_ids or wizard.composition_mode == 'mass_mail' or not wizard.template_id:
|
|
|
|
continue
|
|
|
|
new_attachment_ids = []
|
|
|
|
for attachment in wizard.attachment_ids:
|
2013-06-06 13:08:51 +00:00
|
|
|
if attachment in wizard.template_id.attachment_ids:
|
2013-03-06 13:46:12 +00:00
|
|
|
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))
|
2013-03-01 14:48:27 +00:00
|
|
|
else:
|
|
|
|
new_attachment_ids.append(attachment.id)
|
|
|
|
self.write(cr, uid, wizard.id, {'attachment_ids': [(6, 0, new_attachment_ids)]}, context=context)
|
2013-06-06 12:37:24 +00:00
|
|
|
return super(mail_compose_message, self).send_mail(cr, uid, ids, context=wizard_context)
|
2013-03-01 14:48:27 +00:00
|
|
|
|
2012-11-16 17:02:59 +00:00
|
|
|
def onchange_template_id(self, cr, uid, ids, template_id, composition_mode, model, res_id, context=None):
|
|
|
|
""" - mass_mailing: we cannot render, so return the template values
|
|
|
|
- normal mode: return rendered values """
|
|
|
|
if template_id and composition_mode == 'mass_mail':
|
2013-04-11 12:44:46 +00:00
|
|
|
fields = ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc', 'reply_to', 'attachment_ids']
|
2013-03-14 11:03:15 +00:00
|
|
|
template_values = self.pool.get('email.template').read(cr, uid, template_id, fields, context)
|
|
|
|
values = dict((field, template_values[field]) for field in fields if template_values.get(field))
|
2012-11-16 17:02:59 +00:00
|
|
|
elif template_id:
|
2012-09-03 18:11:11 +00:00
|
|
|
values = self.generate_email_for_composer(cr, uid, template_id, res_id, context=context)
|
2013-03-04 09:22:48 +00:00
|
|
|
# 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
|
2013-03-06 13:46:12 +00:00
|
|
|
values['attachment_ids'] = values.pop('attachment_ids', [])
|
2012-09-05 15:56:26 +00:00
|
|
|
ir_attach_obj = self.pool.get('ir.attachment')
|
|
|
|
for attach_fname, attach_datas in values.pop('attachments', []):
|
|
|
|
data_attach = {
|
|
|
|
'name': attach_fname,
|
|
|
|
'datas': attach_datas,
|
|
|
|
'datas_fname': attach_fname,
|
2013-03-04 09:22:48 +00:00
|
|
|
'res_model': 'mail.compose.message',
|
|
|
|
'res_id': 0,
|
2013-02-25 16:48:57 +00:00
|
|
|
'type': 'binary', # override default_type from context, possibly meant for another model!
|
2012-09-05 15:56:26 +00:00
|
|
|
}
|
|
|
|
values['attachment_ids'].append(ir_attach_obj.create(cr, uid, data_attach, context=context))
|
2012-08-31 17:19:00 +00:00
|
|
|
else:
|
2013-02-25 16:48:57 +00:00
|
|
|
values = self.default_get(cr, uid, ['subject', 'body', 'email_from', 'email_to', 'email_cc', 'partner_to', 'reply_to', 'attachment_ids'], context=context)
|
2012-08-30 15:51:54 +00:00
|
|
|
|
2012-09-03 18:11:11 +00:00
|
|
|
if values.get('body_html'):
|
|
|
|
values['body'] = values.pop('body_html')
|
2011-04-20 11:54:30 +00:00
|
|
|
return {'value': values}
|
2011-02-23 09:27:08 +00:00
|
|
|
|
2011-04-26 12:20:54 +00:00
|
|
|
def save_as_template(self, cr, uid, ids, context=None):
|
2012-08-30 15:51:54 +00:00
|
|
|
""" hit save as template button: current form value will be a new
|
|
|
|
template attached to the current document. """
|
2011-09-08 17:37:52 +00:00
|
|
|
email_template = self.pool.get('email.template')
|
2012-08-30 15:51:54 +00:00
|
|
|
ir_model_pool = self.pool.get('ir.model')
|
2011-04-26 11:54:45 +00:00
|
|
|
for record in self.browse(cr, uid, ids, context=context):
|
2012-09-03 18:11:11 +00:00
|
|
|
model_ids = ir_model_pool.search(cr, uid, [('model', '=', record.model)], context=context)
|
2011-09-09 15:54:27 +00:00
|
|
|
model_id = model_ids and model_ids[0] or False
|
|
|
|
model_name = ''
|
|
|
|
if model_id:
|
2012-08-30 15:51:54 +00:00
|
|
|
model_name = ir_model_pool.browse(cr, uid, model_id, context=context).name
|
2011-09-08 17:37:52 +00:00
|
|
|
template_name = "%s: %s" % (model_name, tools.ustr(record.subject))
|
2011-04-26 11:54:45 +00:00
|
|
|
values = {
|
2011-09-08 17:37:52 +00:00
|
|
|
'name': template_name,
|
2011-04-26 11:54:45 +00:00
|
|
|
'subject': record.subject or False,
|
2012-09-05 15:56:26 +00:00
|
|
|
'body_html': record.body or False,
|
2011-09-09 15:54:27 +00:00
|
|
|
'model_id': model_id or False,
|
2013-03-27 15:17:27 +00:00
|
|
|
'attachment_ids': [(6, 0, [att.id for att in record.attachment_ids])],
|
2011-04-26 11:54:45 +00:00
|
|
|
}
|
2011-09-08 17:37:52 +00:00
|
|
|
template_id = email_template.create(cr, uid, values, context=context)
|
2013-03-27 15:17:27 +00:00
|
|
|
# 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)
|
2012-11-15 12:24:06 +00:00
|
|
|
return _reopen(self, record.id, record.model)
|
2011-04-26 11:54:45 +00:00
|
|
|
|
2012-08-31 17:19:00 +00:00
|
|
|
#------------------------------------------------------
|
|
|
|
# Wizard validation and send
|
|
|
|
#------------------------------------------------------
|
|
|
|
|
2013-03-05 15:15:03 +00:00
|
|
|
def _get_or_create_partners_from_values(self, cr, uid, rendered_values, context=None):
|
|
|
|
""" Check for email_to, email_cc, partner_to """
|
|
|
|
partner_ids = []
|
|
|
|
mails = tools.email_split(rendered_values.pop('email_to', '') + ' ' + rendered_values.pop('email_cc', ''))
|
|
|
|
for mail in mails:
|
|
|
|
partner_id = self.pool.get('res.partner').find_or_create(cr, uid, mail, context=context)
|
|
|
|
partner_ids.append(partner_id)
|
|
|
|
partner_to = rendered_values.pop('partner_to', '')
|
|
|
|
if partner_to:
|
|
|
|
for partner_id in partner_to.split(','):
|
2013-03-15 12:07:59 +00:00
|
|
|
if partner_id: # placeholders could generate '', 3, 2 due to some empty field values
|
|
|
|
partner_ids.append(int(partner_id))
|
2013-03-05 15:15:03 +00:00
|
|
|
return partner_ids
|
|
|
|
|
2012-09-03 18:11:11 +00:00
|
|
|
def generate_email_for_composer(self, cr, uid, template_id, res_id, context=None):
|
|
|
|
""" Call email_template.generate_email(), get fields relevant for
|
|
|
|
mail.compose.message, transform email_cc and email_to into partner_ids """
|
|
|
|
template_values = self.pool.get('email.template').generate_email(cr, uid, template_id, res_id, context=context)
|
2012-09-05 15:56:26 +00:00
|
|
|
# filter template values
|
2013-04-11 12:44:46 +00:00
|
|
|
fields = ['subject', 'body_html', 'email_from', 'email_to', 'partner_to', 'email_cc', 'reply_to', 'attachment_ids', 'attachments']
|
2012-09-05 15:56:26 +00:00
|
|
|
values = dict((field, template_values[field]) for field in fields if template_values.get(field))
|
|
|
|
values['body'] = values.pop('body_html', '')
|
2012-11-06 19:12:55 +00:00
|
|
|
|
2012-09-03 18:11:11 +00:00
|
|
|
# transform email_to, email_cc into partner_ids
|
2013-03-15 12:07:59 +00:00
|
|
|
partner_ids = self._get_or_create_partners_from_values(cr, uid, values, context=context)
|
2013-03-08 09:11:56 +00:00
|
|
|
# legacy template behavior: void values do not erase existing values and the
|
|
|
|
# related key is removed from the values dict
|
|
|
|
if partner_ids:
|
2013-03-08 12:43:41 +00:00
|
|
|
values['partner_ids'] = list(partner_ids)
|
2012-11-06 19:12:55 +00:00
|
|
|
|
2012-09-03 18:11:11 +00:00
|
|
|
return values
|
|
|
|
|
2012-09-05 15:56:26 +00:00
|
|
|
def render_message(self, cr, uid, wizard, res_id, context=None):
|
2012-12-11 15:54:35 +00:00
|
|
|
""" Override to handle templates. """
|
2012-09-05 15:56:26 +00:00
|
|
|
# generate the composer email
|
2012-12-11 15:54:35 +00:00
|
|
|
if wizard.template_id:
|
2013-04-11 13:41:19 +00:00
|
|
|
values = self.generate_email_for_composer(cr, uid, wizard.template_id.id, res_id, context=context)
|
2012-12-11 15:54:35 +00:00
|
|
|
else:
|
|
|
|
values = {}
|
2013-03-06 13:46:12 +00:00
|
|
|
# remove attachments as they should not be rendered
|
|
|
|
values.pop('attachment_ids', None)
|
2012-08-31 17:19:00 +00:00
|
|
|
# get values to return
|
2012-09-05 15:56:26 +00:00
|
|
|
email_dict = super(mail_compose_message, self).render_message(cr, uid, wizard, res_id, context)
|
2013-03-14 11:03:15 +00:00
|
|
|
# those values are not managed; they are readonly
|
|
|
|
email_dict.pop('email_to', None)
|
|
|
|
email_dict.pop('email_cc', None)
|
|
|
|
email_dict.pop('partner_to', None)
|
2013-03-05 15:15:03 +00:00
|
|
|
# update template values by wizard values
|
2013-02-13 17:39:40 +00:00
|
|
|
values.update(email_dict)
|
|
|
|
return values
|
2012-08-31 17:19:00 +00:00
|
|
|
|
2011-09-09 20:10:37 +00:00
|
|
|
def render_template(self, cr, uid, template, model, res_id, context=None):
|
|
|
|
return self.pool.get('email.template').render_template(cr, uid, template, model, res_id, context=context)
|
2011-02-23 09:27:08 +00:00
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|