Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syncing from upstream OCA/server-tools (16.0) #1435

Merged
merged 15 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
98 changes: 98 additions & 0 deletions cron_daylight_saving_time_resistant/README.rst
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/OCA/server-tools/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 <https://github.com/OCA/server-tools/issues/new?body=module:%20cron_daylight_saving_time_resistant%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

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 <[email protected]>

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 <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-florian-dacosta|

This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/16.0/cron_daylight_saving_time_resistant>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions cron_daylight_saving_time_resistant/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
18 changes: 18 additions & 0 deletions cron_daylight_saving_time_resistant/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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",
],
}
Original file line number Diff line number Diff line change
@@ -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 ""
1 change: 1 addition & 0 deletions cron_daylight_saving_time_resistant/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import ir_cron
85 changes: 85 additions & 0 deletions cron_daylight_saving_time_resistant/models/ir_cron.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions cron_daylight_saving_time_resistant/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Go to the menu Settings => Technical => Automation => Scheduled Actions
Then you can check the check box Daylight Saving Time Resistant
2 changes: 2 additions & 0 deletions cron_daylight_saving_time_resistant/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Raphaël Reverdy https://akretion.com
* Florian da Costa <[email protected]>
8 changes: 8 additions & 0 deletions cron_daylight_saving_time_resistant/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading