Browse Source

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)
pull/1524/head
David BEAL 13 years ago
committed by Cristina Martin
parent
commit
5495f3ec24
  1. 6
      scheduler_error_mailer/__init__.py
  2. 19
      scheduler_error_mailer/__openerp__.py
  3. 76
      scheduler_error_mailer/ir_cron.py
  4. 8
      scheduler_error_mailer/ir_cron.xml
  5. 32
      scheduler_error_mailer/ir_cron_email_tpl.xml
  6. 39
      scheduler_error_mailer/test_fail_scheduler.py

6
scheduler_error_mailer/__init__.py

@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Asterisk Click2Dial module for OpenERP
# Copyright (C) 2010 Sébastien BEAU <sebastien.beau@akretion.com>
# Scheduler error mailer module for OpenERP
# Copyright (C) 2012 Akretion
# @author: Sébastien Beau <sebastien.beau@akretion.com>
#
# 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

19
scheduler_error_mailer/__openerp__.py

@ -1,8 +1,11 @@
# -*- encoding: utf-8 -*-
##############################################################################
#
# Model module for OpenERP
# Copyright (C) 2010 Sébastien BEAU <sebastien.beau@akretion.com>
# Scheduler error mailer module for OpenERP
# Copyright (C) 2012 Akretion
# @author: Sébastien Beau <sebastien.beau@akretion.com>
# @author David Beal <bealdavid@gmail.com>
# @author Alexis de Lattre <alexis.delattre@akretion.com>
#
# 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,

76
scheduler_error_mailer/ir_cron.py

@ -1,55 +1,49 @@
# -*- encoding: utf-8 -*-
#################################################################################
# #
# Model module for OpenERP #
# Copyright (C) 2010 Sébastien BEAU <sebastien.beau@akretion.comr> #
# #
# 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 <http://www.gnu.org/licenses/>. #
# #
#################################################################################
#
# Scheduler error mailer module for OpenERP
# Copyright (C) 2012 Akretion
# @author: Sébastien Beau <sebastien.beau@akretion.com>
# @author David Beal <bealdavid@gmail.com>
# @author Alexis de Lattre <alexis.delattre@akretion.com>
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
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

8
scheduler_error_mailer/ir_cron.xml

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
model module for OpenERP
Copyright (C) 2010 Sébastien BEAU <sebastien.beau@akretion.com>
Copyright (C) 2012 Akretion
The licence is in the file __openerp__.py
-->
@ -16,10 +15,7 @@
<field name="doall" position='after'>
<group col="6" colspan="4">
<separator string="Error Mailer" colspan="6"/>
<field name="send_email" colspan="1"/>
<field name="email_account" colspan="1"/>
<field name="email_to" colspan="1"/>
<field name="message" nolabel='1' colspan="6"/>
<field name="email_template" colspan="2"/>
</group>
</field>
</field>

32
scheduler_error_mailer/ir_cron_email_tpl.xml

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2012 Akretion
The licence is in the file __openerp__.py
-->
<openerp>
<!-- Email template -->
<data noupdate="1">
<record id="scheduler_error_mailer" model="email.template">
<field name="name">Scheduler error</field>
<field name="email_from">${object.user_id.user_email or ''}</field>
<field name="subject">[DB ${object._cr.dbname}] Scheduler '${object.name or ''}' FAILED</field>
<field name="email_to">${object.user_id.user_email or ''}</field>
<field name="model_id" ref="base.model_ir_cron"/>
<field name="auto_delete" eval="True"/>
<field name="body_text"><![CDATA[
On the database '${object._cr.dbname}', OpenERP has run the scheduler '${object.name or ''}' but it failed. Here is the error message :
${hasattr(context.get('ctx').get('job_exception'), 'value') and context.get('ctx').get('job_exception').value or context.get('ctx').get('job_exception')}
You may check the logs of OpenERP to get more information about this failure.
The next action on this task is scheduled on ${object.nextcall or ''}.
--
Automatic e-mail sent by OpenERP. Do not reply.
]]></field>
</record>
</data>
</openerp>

39
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 <bealdavid@gmail.com>
# @author Alexis de Lattre <alexis.delattre@akretion.com>
#
# 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 <http://www.gnu.org/licenses/>.
#
##############################################################################
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()
Loading…
Cancel
Save