# -*- coding: utf-8 -*- # Copyright (C) 2016-2017 Akretion (http://www.akretion.com) # @author: Alexis de Lattre # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from odoo import models, fields, api class ResPartner(models.Model): _inherit = 'res.partner' notify_email = fields.Selection( selection_add=[ ('all_except_notification', 'All Messages Except Notifications')], default='all_except_notification') @api.multi def _notify( self, message, force_send=False, send_after_commit=True, user_signature=True): if message.message_type == 'notification': message_sudo = message.sudo() email_channels = message.channel_ids.filtered( lambda channel: channel.email_send) bad_email = message_sudo.author_id and\ message_sudo.author_id.email or message.email_from self.sudo().search([ '|', ('id', 'in', self.ids), ('channel_ids', 'in', email_channels.ids), ('email', '!=', bad_email), ('notify_email', '=', 'always')])._notify_by_email( message, force_send=force_send, send_after_commit=send_after_commit, user_signature=user_signature) self._notify_by_chat(message) return True else: return super(ResPartner, self)._notify( message, force_send=force_send, send_after_commit=send_after_commit, user_signature=user_signature) class TemplatePreview(models.TransientModel): _inherit = "email_template.preview" res_id = fields.Integer(compute='_compute_res_id') object_id = fields.Reference(selection='_reference_models') @api.model def default_get(self, fields): result = super(TemplatePreview, self).default_get(fields) if result.get('model_id'): model = self.env['ir.model'].browse(result['model_id']) result['object_id'] = model.model return result def _reference_models(self): result = self.default_get(['model_id']) if result.get('model_id'): model = self.env['ir.model'].browse(result['model_id']) return [(model.model, model.name)] else: models = self.env['ir.model'].search([('state', '!=', 'manual')]) return [(model.model, model.name) for model in models if not model.model.startswith('ir.')] @api.depends('object_id') def _compute_res_id(self): for record in self: if self.object_id: record.res_id = self.object_id.id def send(self): template = self.env['mail.template'].browse( self._context['template_id']) template.send_mail( self.res_id, force_send=True, raise_exception=True)