~ajite/server-env-tools/7.0-server-env-tools-0001

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- encoding: utf-8 -*-
#################################################################################
#
#    Scheduler Error Mailer module for OpenERP
#    Copyright (C) 2012-2013 Akretion (http://www.akretion.com/)
#    @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 openerp.osv import orm, fields
from openerp.tools.translate import _
import logging

logger = logging.getLogger(__name__)

class ir_cron(orm.Model):
    _inherit = "ir.cron"

    _columns = {
        'email_template': fields.many2one('email.template',
            'Error E-mail Template',
            help="Select the email template that will be sent when this scheduler fails."),
    }


    def _handle_callback_exception(self, cr, uid, model_name, method_name, args, job_id, job_exception):

        res = super(ir_cron, self)._handle_callback_exception(cr, uid,
            model_name, method_name, args, job_id, job_exception)

        my_cron = self.browse(cr, uid, job_id)

        if my_cron.email_template:
            # we put the job_exception in context to be able to print it inside
            # the email template
            context = {
                'job_exception': job_exception,
                'dbname': cr.dbname,
                }

            logger.debug("Sending scheduler error email with context=%s" % context)
            self.pool['email.template'].send_mail(cr, uid,
                my_cron.email_template.id, my_cron.id, force_send=True,
                context=context)

        return res


class res_users(orm.Model):
    _inherit = 'res.users'

    def test_scheduler_failure(self, cr, uid, context=None):
        """This function is used to test and debug this module"""
        raise orm.except_orm(_('Error :'), _("Task failure with UID = %d." % uid))