在odoo v8中发生错误时发送电子邮件并引发消息错误

时间:2016-04-21 08:54:40

标签: python model openerp odoo-8

我想在发生错误时收到电子邮件并引发消息错误。

    from openerp import models, fields, api, _
    from openerp.exceptions import ValidationError
    from datetime import datetime

因此,要发送和发送电子邮件,我在此功能中创建了模板:

    @api.model
    def email_template(self):
       template_obj = self.env['email.template']
       model = self.env['ir.model'].search([('model', '=', 'sync.synchro.partner')])[0]
       company= self.env['res.company'].search([])[0]
       body_html = '''Synchronization Error'''
       synchronise_erreur_template = {
                               'model_id': model.id,
                               'name': 'Erreur Technique',
                               'subject' : 'Erreur produite lors de Synchronisation des contacts',
                               'body_html': body_html,
                               'email_from' : company.email or '${object.employee_id.work_email}',
                               'email_to' : 'tech@example.com',
                                }

我在发生错误时调用它

    @api.multi
    def schedule_synchro(self):
        remote_partners = self.env['res.partner']
        template_obj = self.pool['email.template']
        records = self.env['sync.synchro.partner'].search([])
        for record in records :
            if record.model_name == 'partners':
                try: 
                    remote_partners.export2remote([record.local_id])
                    record.unlink()
                except (Warning) as exc:
                    template_obj.send_mail(self.env.cr, self.env.uid,
                         synchro_partner.synchronise_erreur_template.id, 
                         synchro_partner.id, force_send=True,context=self.env.context)
                    raise except_orm('Couldn t Transfer partner to remote database')

但即使出现错误,我也不会收到电子邮件或邮件错误

1 个答案:

答案 0 :(得分:0)

我找到了解决方案

    @api.model
    def email_template(self):
            template_obj = self.env['email.template']
            model = self.env['ir.model'].search([('model', '=', 'sync.synchro.partner')])[0]
            company= self.env['res.company'].search([])[0]
            body_html =  u""" <html><h1>Erreur de Synchronisation</h1>
            <p>This error was raised ...</p></html> """
            synchronise_catogory_erreur_template = {
                'model_id': model.id,
                'name': 'Erreur Technique',
                'subject' : 'Erreur produite lors de Synchronisation',
                'body_html': body_html,
                'email_from' : company.email or '${object.employee_id.work_email}',
                'email_to' : 'tech@exemple.com',
                }
            template_exists = template_obj.search([('subject','=',"Erreur produite lors de Synchronisation")])
            if not template_exists :
                template_record = template_obj.create(synchronise_erreur_template)
            else :
                template_record = template_exists[0]
            return template_record

我在这里打电话:

    @api.multi
    def schedule_synchro(self):
            template_record = self.email_template()
            template_obj = self.pool['email.template']
            ......
            for record in records:
                if record.model_name == 'partners':
                    try: 
                        .....
                    except Exception as exc:
                        template_obj.send_mail(self.env.cr, self.env.uid,
                            template_record.id, 
                            record.id, force_send=True,context=self.env.context)
                        raise Warning("Couldn t Transfer partner category to remote database \n")