diff --git a/README.md b/README.md index da53efb40e6..c3182bb7ca0 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ addon | version | maintainers | summary [base_technical_user](base_technical_user/) | 16.0.1.0.0 | | Add a technical user parameter on the company [base_time_window](base_time_window/) | 16.0.1.0.0 | | Base model to handle time windows [base_view_inheritance_extension](base_view_inheritance_extension/) | 16.0.1.1.0 | | Adds more operators for view inheritance +[cron_daylight_saving_time_resistant](cron_daylight_saving_time_resistant/) | 16.0.1.0.0 | [![florian-dacosta](https://github.com/florian-dacosta.png?size=30px)](https://github.com/florian-dacosta) | Run cron on fixed hours [database_cleanup](database_cleanup/) | 16.0.1.0.1 | | Database cleanup [dbfilter_from_header](dbfilter_from_header/) | 16.0.1.0.0 | | Filter databases with HTTP headers [excel_import_export](excel_import_export/) | 16.0.1.1.0 | [![kittiu](https://github.com/kittiu.png?size=30px)](https://github.com/kittiu) | Base module for developing Excel import/export/report diff --git a/cron_daylight_saving_time_resistant/README.rst b/cron_daylight_saving_time_resistant/README.rst new file mode 100644 index 00000000000..3aeeab441db --- /dev/null +++ b/cron_daylight_saving_time_resistant/README.rst @@ -0,0 +1,98 @@ +=================================== +Cron daylight saving time resistant +=================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:fdc494dd40cb7c08648cb1ecb755f4a0ba5580eef66adf55482ee787e47c2afb + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github + :target: https://github.com/OCA/server-tools/tree/16.0/cron_daylight_saving_time_resistant + :alt: OCA/server-tools +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/server-tools-16-0/server-tools-16-0-cron_daylight_saving_time_resistant + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/server-tools&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module adjust cron to run at fixed hours, local time. + + +Without this module, when a daylight saving time change occur, the cron will not take +the hour change in account. + +With this module, when a daylight saving time change occur, the offset (+1 or -1 hour) +will be applied. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +* Go to the menu Settings => Technical => Automation => Scheduled Actions + Then you can check the check box Daylight Saving Time Resistant + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* akretion + +Contributors +~~~~~~~~~~~~ + +* Raphaël Reverdy https://akretion.com +* Florian da Costa + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +.. |maintainer-florian-dacosta| image:: https://github.com/florian-dacosta.png?size=40px + :target: https://github.com/florian-dacosta + :alt: florian-dacosta + +Current `maintainer `__: + +|maintainer-florian-dacosta| + +This module is part of the `OCA/server-tools `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/cron_daylight_saving_time_resistant/__init__.py b/cron_daylight_saving_time_resistant/__init__.py new file mode 100644 index 00000000000..0650744f6bc --- /dev/null +++ b/cron_daylight_saving_time_resistant/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/cron_daylight_saving_time_resistant/__manifest__.py b/cron_daylight_saving_time_resistant/__manifest__.py new file mode 100644 index 00000000000..07f6c8bd629 --- /dev/null +++ b/cron_daylight_saving_time_resistant/__manifest__.py @@ -0,0 +1,18 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Cron daylight saving time resistant", + "summary": "Run cron on fixed hours", + "version": "16.0.1.0.0", + "category": "Tools", + "website": "https://github.com/OCA/server-tools", + "author": "akretion, Odoo Community Association (OCA)", + "license": "AGPL-3", + "installable": True, + "maintainers": ["florian-dacosta"], + "depends": [ + "base", + ], + "data": [ + "views/ir_cron_views.xml", + ], +} diff --git a/cron_daylight_saving_time_resistant/i18n/cron_daylight_saving_time_resistant.pot b/cron_daylight_saving_time_resistant/i18n/cron_daylight_saving_time_resistant.pot new file mode 100644 index 00000000000..60d4b3d421c --- /dev/null +++ b/cron_daylight_saving_time_resistant/i18n/cron_daylight_saving_time_resistant.pot @@ -0,0 +1,36 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * cron_daylight_saving_time_resistant +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: cron_daylight_saving_time_resistant +#: model:ir.model.fields,help:cron_daylight_saving_time_resistant.field_ir_cron__daylight_saving_time_resistant +msgid "" +"Adjust interval to run at the same hour after and beforedaylight saving time" +" change. It's used twice a year" +msgstr "" + +#. module: cron_daylight_saving_time_resistant +#: model:ir.model.fields,field_description:cron_daylight_saving_time_resistant.field_ir_cron__daylight_saving_time_resistant +msgid "Daylight Saving Time Resistant" +msgstr "" + +#. module: cron_daylight_saving_time_resistant +#: model:ir.model,name:cron_daylight_saving_time_resistant.model_ir_cron +msgid "Scheduled Actions" +msgstr "" + +#. module: cron_daylight_saving_time_resistant +#: model:ir.model.fields,field_description:cron_daylight_saving_time_resistant.field_ir_cron__smart_search +msgid "Smart Search" +msgstr "" diff --git a/cron_daylight_saving_time_resistant/models/__init__.py b/cron_daylight_saving_time_resistant/models/__init__.py new file mode 100644 index 00000000000..9115592626a --- /dev/null +++ b/cron_daylight_saving_time_resistant/models/__init__.py @@ -0,0 +1 @@ +from . import ir_cron diff --git a/cron_daylight_saving_time_resistant/models/ir_cron.py b/cron_daylight_saving_time_resistant/models/ir_cron.py new file mode 100644 index 00000000000..62273647a41 --- /dev/null +++ b/cron_daylight_saving_time_resistant/models/ir_cron.py @@ -0,0 +1,85 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import logging +from datetime import datetime + +import pytz + +from odoo import api, fields, models + +from odoo.addons.base.models.ir_cron import _intervalTypes + +_logger = logging.getLogger(__name__) + + +class IrCron(models.Model): + _inherit = "ir.cron" + + daylight_saving_time_resistant = fields.Boolean( + help="Adjust interval to run at the same hour after and before" + "daylight saving time change. It's used twice a year" + ) + + def _calculate_daylight_offset(self, nextcall, delta, numbercall, now): + + tz = nextcall.tzinfo + before_offset = tz.normalize(nextcall).utcoffset() + + while nextcall < now and numbercall: + if numbercall > 0: + numbercall -= 1 + if numbercall: + nextcall += delta + + after_offset = tz.normalize(nextcall).utcoffset() + + diff_offset = after_offset - before_offset + return diff_offset + + @classmethod + def _process_job(cls, db, cron_cr, job): + """Add or remove the Daylight saving offset when needed.""" + res = super()._process_job(db, cron_cr, job) + # changing the date has to be after the super, else, e may add a hour + # to next call, and the super will no run the cron, (because now will + # be 1 hour too soon) and the date will just be incremented of 1 + # hour, each hour...until the changes time really occurs... + if job["daylight_saving_time_resistant"]: + with cls.pool.cursor() as job_cr: + try: + cron = api.Environment( + job_cr, + job["user_id"], + {"lastcall": fields.Datetime.from_string(job["lastcall"])}, + )[cls._name] + now = fields.Datetime.context_timestamp(cron, datetime.now()) + # original nextcall + nextcall = fields.Datetime.context_timestamp(cron, job["nextcall"]) + numbercall = job["numbercall"] + delta = _intervalTypes[job["interval_type"]](job["interval_number"]) + diff_offset = cron._calculate_daylight_offset( + nextcall, delta, numbercall, now + ) + if diff_offset and nextcall < now and numbercall: + cron_cr.execute( + """ + SELECT nextcall FROM ir_cron WHERE id = %s + """, + (job["id"],), + ) + res_sql = cron_cr.fetchall() + new_nextcall = res_sql and res_sql[0][0] + new_nextcall = fields.Datetime.context_timestamp( + cron, new_nextcall + ) + new_nextcall -= diff_offset + modified_next_call = fields.Datetime.to_string( + new_nextcall.astimezone(pytz.UTC) + ) + cron_cr.execute( + "UPDATE ir_cron SET nextcall=%s WHERE id=%s", + (modified_next_call, job["id"]), + ) + finally: + cron_cr.commit() + return res diff --git a/cron_daylight_saving_time_resistant/readme/CONFIGURE.rst b/cron_daylight_saving_time_resistant/readme/CONFIGURE.rst new file mode 100644 index 00000000000..8fc14e7c142 --- /dev/null +++ b/cron_daylight_saving_time_resistant/readme/CONFIGURE.rst @@ -0,0 +1,2 @@ +* Go to the menu Settings => Technical => Automation => Scheduled Actions + Then you can check the check box Daylight Saving Time Resistant diff --git a/cron_daylight_saving_time_resistant/readme/CONTRIBUTORS.rst b/cron_daylight_saving_time_resistant/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000000..3444e783702 --- /dev/null +++ b/cron_daylight_saving_time_resistant/readme/CONTRIBUTORS.rst @@ -0,0 +1,2 @@ +* Raphaël Reverdy https://akretion.com +* Florian da Costa diff --git a/cron_daylight_saving_time_resistant/readme/DESCRIPTION.rst b/cron_daylight_saving_time_resistant/readme/DESCRIPTION.rst new file mode 100644 index 00000000000..c43f98341a0 --- /dev/null +++ b/cron_daylight_saving_time_resistant/readme/DESCRIPTION.rst @@ -0,0 +1,8 @@ +This module adjust cron to run at fixed hours, local time. + + +Without this module, when a daylight saving time change occur, the cron will not take +the hour change in account. + +With this module, when a daylight saving time change occur, the offset (+1 or -1 hour) +will be applied. diff --git a/cron_daylight_saving_time_resistant/static/description/icon.png b/cron_daylight_saving_time_resistant/static/description/icon.png new file mode 100644 index 00000000000..3a0328b516c Binary files /dev/null and b/cron_daylight_saving_time_resistant/static/description/icon.png differ diff --git a/cron_daylight_saving_time_resistant/static/description/index.html b/cron_daylight_saving_time_resistant/static/description/index.html new file mode 100644 index 00000000000..5a29a1f2b17 --- /dev/null +++ b/cron_daylight_saving_time_resistant/static/description/index.html @@ -0,0 +1,435 @@ + + + + + +Cron daylight saving time resistant + + + +
+

Cron daylight saving time resistant

+ + +

Beta License: AGPL-3 OCA/server-tools Translate me on Weblate Try me on Runboat

+

This module adjust cron to run at fixed hours, local time.

+

Without this module, when a daylight saving time change occur, the cron will not take +the hour change in account.

+

With this module, when a daylight saving time change occur, the offset (+1 or -1 hour) +will be applied.

+

Table of contents

+ +
+

Configuration

+
    +
  • Go to the menu Settings => Technical => Automation => Scheduled Actions +Then you can check the check box Daylight Saving Time Resistant
  • +
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • akretion
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

Current maintainer:

+

florian-dacosta

+

This module is part of the OCA/server-tools project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/cron_daylight_saving_time_resistant/tests/__init__.py b/cron_daylight_saving_time_resistant/tests/__init__.py new file mode 100644 index 00000000000..ddfe571ecc3 --- /dev/null +++ b/cron_daylight_saving_time_resistant/tests/__init__.py @@ -0,0 +1 @@ +from . import test_dst diff --git a/cron_daylight_saving_time_resistant/tests/test_dst.py b/cron_daylight_saving_time_resistant/tests/test_dst.py new file mode 100644 index 00000000000..25ab5d81bca --- /dev/null +++ b/cron_daylight_saving_time_resistant/tests/test_dst.py @@ -0,0 +1,73 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from datetime import datetime, timedelta + +from freezegun import freeze_time + +import odoo +from odoo import fields +from odoo.tests.common import TransactionCase +from odoo.tools.misc import DEFAULT_SERVER_DATETIME_FORMAT + + +class TestDST(TransactionCase): + def setUp(self): + super().setUp() + self.registry.enter_test_mode(self.env.cr) + + def tearDown(self): + self.registry.leave_test_mode() + super().tearDown() + + def _check_cron_date_after_run(self, cron, datetime_str): + # add 10 sec to make sure cron will run + datetime_current = datetime.strptime( + datetime_str, DEFAULT_SERVER_DATETIME_FORMAT + ) + timedelta(seconds=10) + datetime_current_str = datetime_current.strftime(DEFAULT_SERVER_DATETIME_FORMAT) + with freeze_time(datetime_current_str): + cron.write( + {"nextcall": datetime_str, "daylight_saving_time_resistant": True} + ) + cron.flush_recordset() + self.env.cr.execute("SELECT * FROM ir_cron WHERE id = %s", (cron.id,)) + job = self.env.cr.dictfetchall()[0] + timezone_date_orig = fields.Datetime.context_timestamp(cron, cron.nextcall) + # ensure Paris time zone is taken into account. If we only work in UTC + # there is not change of hour and the test will be green even if it does + # nothing at all... + self.assertEqual(timezone_date_orig.tzinfo.zone, "Europe/Paris") + with odoo.registry(self.env.cr.dbname).cursor() as new_cr: + registry = odoo.registry(new_cr.dbname) + db = odoo.sql_db.db_connect(new_cr.dbname) + + registry["ir.cron"]._process_job(db, new_cr, job) + # since it is updated as a sql query in module + cron.invalidate_recordset() + day_after_date_orig = (timezone_date_orig + timedelta(days=1)).day + timezone_date_after = fields.Datetime.context_timestamp(cron, cron.nextcall) + # check the cron is really planned the next day (which mean it has run + # then check the planned hour is the same even in case of change of time + # (brussels summer time/ brussels winter time + self.assertEqual(day_after_date_orig, timezone_date_after.day) + self.assertEqual(timezone_date_orig.hour, timezone_date_after.hour) + + def test_cron(self): + user = self.env.ref("base.user_root") + user.write({"tz": "Europe/Paris"}) + user.invalidate_recordset() + cron = self.env["ir.cron"].create( + { + "name": "TestCron", + "model_id": self.env.ref("base.model_res_partner").id, + "state": "code", + "code": "model.search([])", + "interval_number": 1, + "interval_type": "days", + "numbercall": -1, + "doall": False, + } + ) + # from summer time to winter time + self._check_cron_date_after_run(cron, "2021-10-30 15:00:00") + # from winter time to summer time + self._check_cron_date_after_run(cron, "2021-03-27 15:00:00") diff --git a/cron_daylight_saving_time_resistant/views/ir_cron_views.xml b/cron_daylight_saving_time_resistant/views/ir_cron_views.xml new file mode 100644 index 00000000000..0f79894dd7c --- /dev/null +++ b/cron_daylight_saving_time_resistant/views/ir_cron_views.xml @@ -0,0 +1,12 @@ + + + + ir.cron + + + + + + + + diff --git a/setup/_metapackage/VERSION.txt b/setup/_metapackage/VERSION.txt index 332b83e611f..5e7be1c7012 100644 --- a/setup/_metapackage/VERSION.txt +++ b/setup/_metapackage/VERSION.txt @@ -1 +1 @@ -16.0.20240116.0 \ No newline at end of file +16.0.20240129.0 \ No newline at end of file diff --git a/setup/_metapackage/setup.py b/setup/_metapackage/setup.py index 2f7c260385e..e3c8b25eda3 100644 --- a/setup/_metapackage/setup.py +++ b/setup/_metapackage/setup.py @@ -26,6 +26,7 @@ 'odoo-addon-base_technical_user>=16.0dev,<16.1dev', 'odoo-addon-base_time_window>=16.0dev,<16.1dev', 'odoo-addon-base_view_inheritance_extension>=16.0dev,<16.1dev', + 'odoo-addon-cron_daylight_saving_time_resistant>=16.0dev,<16.1dev', 'odoo-addon-database_cleanup>=16.0dev,<16.1dev', 'odoo-addon-dbfilter_from_header>=16.0dev,<16.1dev', 'odoo-addon-excel_import_export>=16.0dev,<16.1dev', diff --git a/setup/cron_daylight_saving_time_resistant/odoo/addons/cron_daylight_saving_time_resistant b/setup/cron_daylight_saving_time_resistant/odoo/addons/cron_daylight_saving_time_resistant new file mode 120000 index 00000000000..61296a7d37f --- /dev/null +++ b/setup/cron_daylight_saving_time_resistant/odoo/addons/cron_daylight_saving_time_resistant @@ -0,0 +1 @@ +../../../../cron_daylight_saving_time_resistant \ No newline at end of file diff --git a/setup/cron_daylight_saving_time_resistant/setup.py b/setup/cron_daylight_saving_time_resistant/setup.py new file mode 100644 index 00000000000..28c57bb6403 --- /dev/null +++ b/setup/cron_daylight_saving_time_resistant/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)