From ba0b49162d3637ac5c69e57121bf200e1b0eea95 Mon Sep 17 00:00:00 2001 From: David BEAL Date: Mon, 4 Jun 2012 13:13:04 +0200 Subject: [PATCH] Now uses an email template instead of a simple mail Add a default e-mail template Add the code we used to test the failure of scheduler (the import directive is commented) --- scheduler_error_mailer/__init__.py | 6 +- scheduler_error_mailer/__openerp__.py | 19 +++-- scheduler_error_mailer/ir_cron.py | 76 +++++++++---------- scheduler_error_mailer/ir_cron.xml | 8 +- scheduler_error_mailer/ir_cron_email_tpl.xml | 32 ++++++++ scheduler_error_mailer/test_fail_scheduler.py | 39 ++++++++++ 6 files changed, 123 insertions(+), 57 deletions(-) create mode 100644 scheduler_error_mailer/ir_cron_email_tpl.xml create mode 100644 scheduler_error_mailer/test_fail_scheduler.py diff --git a/scheduler_error_mailer/__init__.py b/scheduler_error_mailer/__init__.py index 78a455ba8..82ee32f70 100644 --- a/scheduler_error_mailer/__init__.py +++ b/scheduler_error_mailer/__init__.py @@ -1,8 +1,9 @@ # -*- coding: utf-8 -*- ############################################################################## # -# Asterisk Click2Dial module for OpenERP -# Copyright (C) 2010 Sébastien BEAU +# Scheduler error mailer module for OpenERP +# Copyright (C) 2012 Akretion +# @author: Sébastien Beau # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -20,4 +21,5 @@ ############################################################################## import ir_cron +# import test_fail_scheduler diff --git a/scheduler_error_mailer/__openerp__.py b/scheduler_error_mailer/__openerp__.py index aead8b9fd..c477c4e64 100644 --- a/scheduler_error_mailer/__openerp__.py +++ b/scheduler_error_mailer/__openerp__.py @@ -1,8 +1,11 @@ # -*- encoding: utf-8 -*- ############################################################################## # -# Model module for OpenERP -# Copyright (C) 2010 Sébastien BEAU +# Scheduler error mailer module for OpenERP +# Copyright (C) 2012 Akretion +# @author: Sébastien Beau +# @author David Beal +# @author Alexis de Lattre # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -21,16 +24,16 @@ { - 'name': 'Scheduler Error Mailer', - 'version': '0.1', - 'category': 'Generic Modules/Others', + 'name': 'Scheduler error mailer', + 'version': '1.0', + 'category': 'Extra Tools', 'license': 'AGPL-3', - 'description': """This module dfkdsjfkdsjkfjksadd the posibility to send a email when a scheduler raise an error""", + 'description': """This module adds the possibility to send an e-mail when a scheduler raises an error""", 'author': 'Akretion', 'website': 'http://www.akretion.com/', - 'depends': ['poweremail'], + 'depends': ['email_template'], 'init_xml': [], - 'update_xml': ['ir_cron.xml'], + 'update_xml': ['ir_cron.xml', 'ir_cron_email_tpl.xml'], 'demo_xml': [], 'installable': True, 'active': False, diff --git a/scheduler_error_mailer/ir_cron.py b/scheduler_error_mailer/ir_cron.py index 510f11c8f..8782eb0b7 100644 --- a/scheduler_error_mailer/ir_cron.py +++ b/scheduler_error_mailer/ir_cron.py @@ -1,55 +1,49 @@ # -*- encoding: utf-8 -*- ################################################################################# -# # -# Model module for OpenERP # -# Copyright (C) 2010 Sébastien BEAU # -# # -# This program is free software: you can redistribute it and/or modify # -# it under the terms of the GNU Affero 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 Affero General Public License for more details. # -# # -# You should have received a copy of the GNU Affero General Public License # -# along with this program. If not, see . # -# # -################################################################################# +# +# Scheduler error mailer module for OpenERP +# Copyright (C) 2012 Akretion +# @author: Sébastien Beau +# @author David Beal +# @author Alexis de Lattre +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## from osv import osv, fields -import netsvc +from datetime import datetime -class ir_cron(osv.osv, netsvc.Agent): +class ir_cron(osv.osv): _inherit = "ir.cron" _columns = { - 'email_to' : fields.char('TO', size=256, help="If an error occure with this scheduler an email will be send"), - 'message' : fields.text('Message', help="If an error occure with this scheduler this message will be send via email"), - 'send_email' : fields.boolean('Active'), - 'email_account' : fields.many2one('poweremail.core_accounts', 'FROM') + 'email_template' : fields.many2one('email.template', 'E-mail template'), } + def _handle_callback_exception(self, cr, uid, model, func, args, job_id, job_exception): + res = super(ir_cron, self)._handle_callback_exception(cr, uid, model, func, args, job_id, job_exception) - job = self.read(cr, uid, job_id, ['send_email', 'message', 'email_TO', 'email_account', 'name']) - #TODO USE POWEREMAIL TEMPLATE - if job['send_email']: - addresses = {'To' : job['email_TO']} - mail_obj = self.pool.get('poweremail.mailbox') - id = mail_obj.create(cr, uid, { - 'pem_to' : job['email_TO'], - 'pem_subject' : "OPENERP : error when excecuting scheduler " + job["name"], - 'pem_body_text' : job['message'], - 'pem_account_id' : job['email_account'][0], - 'mail_type' : 'text/plain', - 'folder' : 'outbox', - 'state' :'na', - }) - mail_obj.send_this_mail(cr, uid, [id]) - return res -ir_cron() + my_cron = self.browse(cr, uid, job_id) + + if my_cron.email_template.id: + # we put the job_exception in context to be able to get it inside the mail template + context = {'job_exception': job_exception} + id_mail_messsage = self.pool.get('email.template').send_mail(cr, uid, + my_cron.email_template.id, my_cron.id, force_send=True, context=context) + + return res diff --git a/scheduler_error_mailer/ir_cron.xml b/scheduler_error_mailer/ir_cron.xml index 33a6d3dfe..da3e844d8 100644 --- a/scheduler_error_mailer/ir_cron.xml +++ b/scheduler_error_mailer/ir_cron.xml @@ -1,7 +1,6 @@ @@ -16,10 +15,7 @@ - - - - + diff --git a/scheduler_error_mailer/ir_cron_email_tpl.xml b/scheduler_error_mailer/ir_cron_email_tpl.xml new file mode 100644 index 000000000..5406d9aaf --- /dev/null +++ b/scheduler_error_mailer/ir_cron_email_tpl.xml @@ -0,0 +1,32 @@ + + + + + + + + + Scheduler error + ${object.user_id.user_email or ''} + [DB ${object._cr.dbname}] Scheduler '${object.name or ''}' FAILED + ${object.user_id.user_email or ''} + + + + + + diff --git a/scheduler_error_mailer/test_fail_scheduler.py b/scheduler_error_mailer/test_fail_scheduler.py new file mode 100644 index 000000000..937f88e6e --- /dev/null +++ b/scheduler_error_mailer/test_fail_scheduler.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Scheduler error mailer module for OpenERP +# Copyright (C) 2012 Akretion +# @author David Beal +# @author Alexis de Lattre +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + +from osv import fields, osv +from tools.translate import _ + +class test_fail_scheduler(osv.osv_memory): + _name = "test.fail.scheduler" + _description = "Test scheduler failure" + + def test_fail(self, cr, uid, context=None): + """ This a test fail, only for debugging purpose + DO NOT UNCOMMENT IMPORT IN init.py IN PRODUCTION ENVIRONNEMENT + """ + + raise osv.except_osv(_('Error :'), _("task failure")) + # context['tytytyty'] + +test_fail_scheduler()