Browse Source

Odoo v8.0 compatibility

pull/81/head
Jairo Llopis 10 years ago
parent
commit
58756416c1
  1. 11
      cron_run_manually/__openerp__.py
  2. 77
      cron_run_manually/ir_cron.py

11
cron_run_manually/__openerp__.py

@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# This module copyright (C) 2013 Therp BV (<http://therp.nl>)
# Code snippets from openobject-server copyright (C) 2004-2013 OpenERP S.A.
@ -17,13 +16,11 @@
#
# 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/>.
#
##############################################################################
{
'name': 'Call cron jobs from their form view',
'version': '0.1',
'author': ["Therp BV", "OpenERP S.A."],
'version': '1.0',
'author': "Therp BV (OpenERP S.A.)",
'license': 'AGPL-3',
'category': 'Tools',
'description': """
@ -33,5 +30,5 @@ of the scheduler.
""",
'depends': ['base'],
'data': ['view/ir_cron.xml'],
'installable': False,
'installable': True,
}

77
cron_run_manually/ir_cron.py

@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# This module copyright (C) 2013 Therp BV (<http://therp.nl>)
# Code snippets from openobject-server copyright (C) 2004-2013 OpenERP S.A.
@ -17,66 +16,62 @@
#
# 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/>.
#
##############################################################################
import psycopg2
import logging
from openerp.osv import orm
from openerp.tools import SUPERUSER_ID
from openerp.tools.translate import _
from openerp import _, api, exceptions, models, SUPERUSER_ID
from openerp.tools.safe_eval import safe_eval
class irCron(orm.Model):
_inherit = 'ir.cron'
_logger = logging.getLogger(__name__)
def run_manually(self, cr, uid, ids, context=None):
"""
Run a job from the cron form view.
Cut and paste of code snippets from addons/base/ir/ir_cron.py
"""
logger = logging.getLogger('cron_run_manually')
cr.execute(
"""
SELECT * from ir_cron
WHERE id in %s
""", (tuple(ids),))
jobs = cr.dictfetchall()
class Cron(models.Model):
_name = _inherit = "ir.cron"
for job in jobs:
if uid != SUPERUSER_ID and (
not job['active'] or not job['numbercall']):
raise orm.except_orm(
_('Error'),
@api.one
def run_manually(self):
"""Run a job from the cron form view."""
if self.env.uid != SUPERUSER_ID and (not self.active or
not self.numbercall):
raise exceptions.AccessError(
_('Only the admin user is allowed to '
'execute inactive cron jobs manually'))
_logger.warn(self.__dict__)
try:
# Try to grab an exclusive lock on the job row
# until the end of the transaction
cr.execute(
self.env.cr.execute(
"""SELECT *
FROM ir_cron
WHERE id=%s
FOR UPDATE NOWAIT""",
(job['id'],), log_exceptions=False)
# Got the lock on the job row, run its code
logger.debug('Job `%s` triggered from form', job['name'])
model = self.pool.get(job['model'])
method = getattr(model, job['function'])
args = safe_eval('tuple(%s)' % (job['args'] or ''))
method(cr, job['user_id'], *args)
(self.id,),
log_exceptions=False)
except psycopg2.OperationalError as e:
except Exception as e:
# User friendly error if the lock could not be claimed
if e.pgcode == '55P03':
raise orm.except_orm(
_('Error'),
if getattr(e, "pgcode", None) == '55P03':
raise exceptions.Warning(
_('Another process/thread is already busy '
'executing this job'))
raise
return True
_logger.info('Job `%s` triggered from form', self.name)
# Prepare execution
method = getattr(self.env[self.model], self.function)
args = safe_eval('tuple(%s)' % (self.args or ''))
# Hack the UID
old_uid = self.env.uid
self.env.uid = self.user_id
# Execute the cron job
try:
method(*args)
finally:
# Revert UID to original
self.env.uid = old_uid
Loading…
Cancel
Save