diff --git a/__unported__/cron_run_manually/__init__.py b/__unported__/cron_run_manually/__init__.py deleted file mode 100644 index 16e8b082f..000000000 --- a/__unported__/cron_run_manually/__init__.py +++ /dev/null @@ -1 +0,0 @@ -import model diff --git a/__unported__/cron_run_manually/__openerp__.py b/__unported__/cron_run_manually/__openerp__.py deleted file mode 100644 index bc6da1037..000000000 --- a/__unported__/cron_run_manually/__openerp__.py +++ /dev/null @@ -1,37 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# This module copyright (C) 2013 Therp BV () -# Code snippets from openobject-server copyright (C) 2004-2013 OpenERP S.A. -# -# 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 . -# -############################################################################## - -{ - 'name': 'Call cron jobs from their form view', - 'version': '0.1', - 'author': ["Therp BV", "OpenERP S.A."], - 'license': 'AGPL-3', - 'category': 'Tools', - 'description': """ -This module adds a button to the cron scheduled task form in OpenERP -that allows the administrator to run the job immediately, independently -of the scheduler. -""", - 'depends': ['base'], - 'data': ['view/ir_cron.xml'], - 'installable': False, - } diff --git a/__unported__/cron_run_manually/model/__init__.py b/__unported__/cron_run_manually/model/__init__.py deleted file mode 100644 index 452b62ec4..000000000 --- a/__unported__/cron_run_manually/model/__init__.py +++ /dev/null @@ -1 +0,0 @@ -import ir_cron diff --git a/__unported__/cron_run_manually/model/ir_cron.py b/__unported__/cron_run_manually/model/ir_cron.py deleted file mode 100644 index 3d7a30fa3..000000000 --- a/__unported__/cron_run_manually/model/ir_cron.py +++ /dev/null @@ -1,82 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# This module copyright (C) 2013 Therp BV () -# Code snippets from openobject-server copyright (C) 2004-2013 OpenERP S.A. -# -# 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 . -# -############################################################################## - -import psycopg2 -import logging -from openerp.osv import orm -from openerp.tools import SUPERUSER_ID -from openerp.tools.translate import _ -from openerp.tools.safe_eval import safe_eval - - -class irCron(orm.Model): - _inherit = 'ir.cron' - - 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() - - for job in jobs: - if uid != SUPERUSER_ID and ( - not job['active'] or not job['numbercall']): - raise orm.except_orm( - _('Error'), - _('Only the admin user is allowed to ' - 'execute inactive cron jobs manually')) - - try: - # Try to grab an exclusive lock on the job row - # until the end of the transaction - 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) - - except psycopg2.OperationalError as e: - # User friendly error if the lock could not be claimed - if e.pgcode == '55P03': - raise orm.except_orm( - _('Error'), - _('Another process/thread is already busy ' - 'executing this job')) - raise - - return True diff --git a/cron_run_manually/__init__.py b/cron_run_manually/__init__.py new file mode 100644 index 000000000..e7a731327 --- /dev/null +++ b/cron_run_manually/__init__.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- + +# OpenERP, Open Source Management Solution +# This module copyright (C) 2013 Therp BV () +# Code snippets from openobject-server copyright (C) 2004-2013 OpenERP S.A. +# +# 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 . import ir_cron diff --git a/cron_run_manually/__openerp__.py b/cron_run_manually/__openerp__.py new file mode 100644 index 000000000..87186c96c --- /dev/null +++ b/cron_run_manually/__openerp__.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- + +# OpenERP, Open Source Management Solution +# This module copyright (C) 2013 Therp BV () +# Code snippets from openobject-server copyright (C) 2004-2013 OpenERP S.A. +# +# 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 . + +{ + 'name': 'Call cron jobs from their form view', + 'version': '1.0', + 'author': "Therp BV (OpenERP S.A.)", + 'license': 'AGPL-3', + 'category': 'Tools', + 'description': """ +This module adds a button to the cron scheduled task form in OpenERP +that allows the administrator to run the job immediately, independently +of the scheduler. +""", + 'depends': ['base', 'mail'], + 'data': ['view/ir_cron.xml'], + "test": [ + "tests/correct_uid.yml", + ], + 'installable': True, +} diff --git a/__unported__/cron_run_manually/i18n/cron_run_manually.pot b/cron_run_manually/i18n/cron_run_manually.pot similarity index 50% rename from __unported__/cron_run_manually/i18n/cron_run_manually.pot rename to cron_run_manually/i18n/cron_run_manually.pot index 5324df8da..95b747f1e 100644 --- a/__unported__/cron_run_manually/i18n/cron_run_manually.pot +++ b/cron_run_manually/i18n/cron_run_manually.pot @@ -1,13 +1,13 @@ -# Translation of OpenERP Server. +# Translation of Odoo Server. # This file contains the translation of the following modules: # * cron_run_manually # msgid "" msgstr "" -"Project-Id-Version: OpenERP Server 7.0\n" +"Project-Id-Version: Odoo Server 8.0rc1\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2014-03-14 17:41+0000\n" -"PO-Revision-Date: 2014-03-14 17:41+0000\n" +"POT-Creation-Date: 2014-09-16 09:47+0000\n" +"PO-Revision-Date: 2014-09-16 09:47+0000\n" "Last-Translator: <>\n" "Language-Team: \n" "MIME-Version: 1.0\n" @@ -16,32 +16,18 @@ msgstr "" "Plural-Forms: \n" #. module: cron_run_manually -#: code:addons/cron_run_manually/model/ir_cron.py:77 +#: code:addons/cron_run_manually/ir_cron.py:57 #, python-format msgid "Another process/thread is already busy executing this job" msgstr "" #. module: cron_run_manually -#: view:ir.cron:0 -msgid "Run now" -msgstr "" - -#. module: cron_run_manually -#: code:addons/cron_run_manually/model/ir_cron.py:52 +#: code:addons/cron_run_manually/ir_cron.py:38 #, python-format msgid "Only the admin user is allowed to execute inactive cron jobs manually" msgstr "" #. module: cron_run_manually -#: model:_description:0 -#: model:ir.model,name:cron_run_manually.model_ir_cron -msgid "ir.cron" -msgstr "" - -#. module: cron_run_manually -#: code:addons/cron_run_manually/model/ir_cron.py:51 -#: code:addons/cron_run_manually/model/ir_cron.py:76 -#, python-format -msgid "Error" +#: view:ir.cron:cron_run_manually.ir_cron_view +msgid "Run now" msgstr "" - diff --git a/cron_run_manually/i18n/es.po b/cron_run_manually/i18n/es.po new file mode 100644 index 000000000..6d74ae23c --- /dev/null +++ b/cron_run_manually/i18n/es.po @@ -0,0 +1,36 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * cron_run_manually +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 8.0rc1\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2014-09-16 09:47+0000\n" +"PO-Revision-Date: 2014-09-16 11:52+0100\n" +"Last-Translator: Jairo Llopis \n" +"Language-Team: Grupo ESOC \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"X-Generator: Poedit 1.5.4\n" +"Language: es\n" +"X-Poedit-SourceCharset: UTF-8\n" + +#. module: cron_run_manually +#: code:addons/cron_run_manually/ir_cron.py:57 +#, python-format +msgid "Another process/thread is already busy executing this job" +msgstr "Hay otro proceso o hilo ejecutando este trabajo en este momento" + +#. module: cron_run_manually +#: code:addons/cron_run_manually/ir_cron.py:38 +#, python-format +msgid "Only the admin user is allowed to execute inactive cron jobs manually" +msgstr "" +"Solo el administrador puede ejecutar manualmente trabajos cron inactivos" + +#. module: cron_run_manually +#: view:ir.cron:cron_run_manually.ir_cron_view +msgid "Run now" +msgstr "Ejecutar ahora" diff --git a/__unported__/cron_run_manually/i18n/nl.po b/cron_run_manually/i18n/nl.po similarity index 100% rename from __unported__/cron_run_manually/i18n/nl.po rename to cron_run_manually/i18n/nl.po diff --git a/cron_run_manually/ir_cron.py b/cron_run_manually/ir_cron.py new file mode 100644 index 000000000..7dc7eea6c --- /dev/null +++ b/cron_run_manually/ir_cron.py @@ -0,0 +1,73 @@ +# -*- coding: utf-8 -*- + +# OpenERP, Open Source Management Solution +# This module copyright (C) 2013 Therp BV () +# Code snippets from openobject-server copyright (C) 2004-2013 OpenERP S.A. +# +# 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 . + +import logging +from openerp import _, api, exceptions, models, SUPERUSER_ID +from openerp.tools.safe_eval import safe_eval +from psycopg2 import OperationalError + +_logger = logging.getLogger(__name__) + + +class Cron(models.Model): + _name = _inherit = "ir.cron" + + @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')) + + try: + # Try to grab an exclusive lock on the job row + # until the end of the transaction + self.env.cr.execute( + """SELECT * + FROM ir_cron + WHERE id=%s + FOR UPDATE NOWAIT""", + (self.id,), + log_exceptions=False) + + except OperationalError as e: + # User friendly error if the lock could not be claimed + if getattr(e, "pgcode", None) == '55P03': + raise exceptions.Warning( + _('Another process/thread is already busy ' + 'executing this job')) + + raise + + _logger.info('Job `%s` triggered from form', self.name) + + # Execute the cron job + method = getattr(self.sudo(self.user_id).env[self.model], + self.function) + args = safe_eval('tuple(%s)' % (self.args or '')) + return method(*args) + + @api.model + def _current_uid(self): + """This function returns the current UID, for testing purposes.""" + + return self.env.uid diff --git a/cron_run_manually/tests/correct_uid.yml b/cron_run_manually/tests/correct_uid.yml new file mode 100644 index 000000000..d63294125 --- /dev/null +++ b/cron_run_manually/tests/correct_uid.yml @@ -0,0 +1,23 @@ +- I create a user that will run the job. +- !record {model: res.users, id: worker_user, view: False}: + company_id: base.main_company + name: worker + login: worker + password: worker + email: worker@example.com + +- Create a cron job to check the UID +- !record {model: ir.cron, id: check_uid_job}: + name: Check UID + active: True + user_id: worker_user + interval_number: 1 + interval_type: days + numbercall: -1 + doall: False + model: ir.cron + function: _current_uid + +- I execute the cron job manually to check its running UID +- !python {model: ir.cron, id: check_uid_job}: | + assert self.run_manually()[0] == self.user_id.id, "Wrong UID in cron job" diff --git a/__unported__/cron_run_manually/view/ir_cron.xml b/cron_run_manually/view/ir_cron.xml similarity index 100% rename from __unported__/cron_run_manually/view/ir_cron.xml rename to cron_run_manually/view/ir_cron.xml