# -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2016 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: Emanuel Cino <ecino@compassion.ch>
#
#    The licence is in the file __manifest__.py
#
##############################################################################

from odoo import models, api, fields


class EmailComposeMessage(models.TransientModel):
    _inherit = 'mail.compose.message'

    body = fields.Html(sanitize=False)

    @api.model
    def create_emails(self, template, res_ids, default_mail_values=None):
        """ Helper to generate a new e-mail given a template and objects.

        :param int template: mail.template record
        :param res_ids: ids of the resource objects
        :return: browse records of created e-mails (one per resource object)
        """
        all_mail_values = self._get_mail_values(template, res_ids)
        email_obj = self.env['mail.mail']
        emails = email_obj
        for res_id in res_ids:
            mail_values = all_mail_values[res_id]
            if default_mail_values:
                mail_values.update(default_mail_values)
            emails += email_obj.create(mail_values)
        return emails

    @api.model
    def get_generated_fields(self, template, res_ids):
        """ Helper to retrieve generated html given a template and objects.

        :param int template: mail.template record
        :param res_ids: ids of the resource objects
        :return: html code generated for the e-mail (list if len(res_ids)>1)
        """
        all_mail_values = self._get_mail_values(template, res_ids)
        email_fields = list()
        for res_id in res_ids:
            email_fields.append(all_mail_values[res_id])
        if len(email_fields) == 1:
            email_fields = email_fields[0]
        return email_fields

    def _get_mail_values(self, template, res_ids):
        """ Helper to get e-mail values given a template and objects.

        :param int template: mail.template record
        :param res_ids: ids of the resource objects
        :return: list of dictionaries containing e-mail values
        """
        if not isinstance(res_ids, list):
            res_ids = [res_ids]
        wizard = self.sudo().with_context(active_ids=res_ids).create({
            'template_id': template.id,
            'composition_mode': 'mass_mail',
            'model': template.model,
            'author_id': self.env.user.partner_id.id,
            'notification': True,
            'auto_delete': True,
        })
        # Fetch template values.
        wizard.write(
            wizard.onchange_template_id(
                template.id, 'mass_mail', False, False)['value'])
        return wizard.get_mail_values(res_ids)

    @api.multi
    def send_mail(self, auto_commit=False):
        """ Return to e-mails generated. """
        super(EmailComposeMessage, self).send_mail(auto_commit)
        mail_ids = self.env['mail.mail'].search([
            ('res_id', 'in', self.env.context.get('active_ids', [])),
            ('model', '=', self.env.context.get('active_model'))
        ])
        if mail_ids:
            return {
                'name': 'E-mails',
                'view_mode': 'form,tree',
                'view_type': 'form',
                'domain': [('id', 'in', mail_ids.ids)],
                'res_model': 'mail.mail',
                'res_id': mail_ids.ids[0],
                'type': 'ir.actions.act_window',
            }
        return True

@@ -2844,749 +2844,4 @@
 ds)%0A
-%0A    @api.multi%0A    def send_mail(self, auto_commit=False):%0A        %22%22%22 Return to e-mails generated. %22%22%22%0A        super(EmailComposeMessage, self).send_mail(auto_commit)%0A        mail_ids = self.env%5B'mail.mail'%5D.search(%5B%0A            ('res_id', 'in', self.env.context.get('active_ids', %5B%5D)),%0A            ('model', '=', self.env.context.get('active_model'))%0A        %5D)%0A        if mail_ids:%0A            return %7B%0A                'name': 'E-mails',%0A                'view_mode': 'form,tree',%0A                'view_type': 'form',%0A                'domain': %5B('id', 'in', mail_ids.ids)%5D,%0A                'res_model': 'mail.mail',%0A                'res_id': mail_ids.ids%5B0%5D,%0A                'type': 'ir.actions.act_window',%0A            %7D%0A        return True%0A

Remove redirection to e-mails after send_mail of mail_compose wizard