diff --git a/partner_tz/README.rst b/partner_tz/README.rst new file mode 100644 index 000000000000..954683440a77 --- /dev/null +++ b/partner_tz/README.rst @@ -0,0 +1,104 @@ +================ +Partner timezone +================ + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:789236468a52507d130f08cbe10041b186f1a37f77989df31c70645a7e18a34f + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fpartner--contact-lightgray.png?logo=github + :target: https://github.com/OCA/partner-contact/tree/18.0/partner_tz + :alt: OCA/partner-contact +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/partner-contact-18-0/partner-contact-18-0-partner_tz + :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/partner-contact&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module displays the timezone field on partner form view. Moreover +it provides a few tools function in tools/tz_utils.py. + +**Table of contents** + +.. contents:: + :local: + +Known issues / Roadmap +====================== + +- Odoo default value for timezone field uses the tz set on the + connected user. So if a user creates a partner located in a different + timezone than his own, the created partner will be set in the user's + timezone instead of having the timezone from its own location. + Ideally, we should change this behaviour to get the timezone from the + country and city of the partner when they are defined. + http://www.geonames.org/export/web-services.html#timezone might be a + good starting point. + +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 +------- + +* Camptocamp + +Contributors +------------ + +- Akim Juillerat +- Hiep (Nguyen Hoang) +- Phuc (Tran Thanh) +- Denis Roussel +- `Binhex `__: + + - Deriman Alonso + +- Khoi (Kien Kim) + +Other credits +------------- + +The development of this module has been financially supported by: + +- Camptocamp + +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. + +This module is part of the `OCA/partner-contact `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/partner_tz/__init__.py b/partner_tz/__init__.py new file mode 100644 index 000000000000..fa947c50bbe4 --- /dev/null +++ b/partner_tz/__init__.py @@ -0,0 +1 @@ +from . import tools diff --git a/partner_tz/__manifest__.py b/partner_tz/__manifest__.py new file mode 100644 index 000000000000..ffb62e29f114 --- /dev/null +++ b/partner_tz/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2020 Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +{ + "name": "Partner timezone", + "summary": "Remove partner timezone default value and display on form", + "version": "18.0.1.0.0", + "category": "Uncategorized", + "website": "https://github.com/OCA/partner-contact", + "author": "Camptocamp, Odoo Community Association (OCA)", + "license": "AGPL-3", + "application": False, + "installable": True, + "depends": ["base"], + "data": ["views/res_partner.xml"], +} diff --git a/partner_tz/i18n/it.po b/partner_tz/i18n/it.po new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/partner_tz/i18n/partner_tz.pot b/partner_tz/i18n/partner_tz.pot new file mode 100644 index 000000000000..716a0702d88a --- /dev/null +++ b/partner_tz/i18n/partner_tz.pot @@ -0,0 +1,13 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 17.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" diff --git a/partner_tz/pyproject.toml b/partner_tz/pyproject.toml new file mode 100644 index 000000000000..4231d0cccb3d --- /dev/null +++ b/partner_tz/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/partner_tz/readme/CONTRIBUTORS.md b/partner_tz/readme/CONTRIBUTORS.md new file mode 100644 index 000000000000..cf5d9382a58f --- /dev/null +++ b/partner_tz/readme/CONTRIBUTORS.md @@ -0,0 +1,7 @@ +- Akim Juillerat \<\> +- Hiep (Nguyen Hoang) \<\> +- Phuc (Tran Thanh) \<\> +- Denis Roussel \<\> +- [Binhex](https://binhex.cloud/): + - Deriman Alonso +- Khoi (Kien Kim) \<> diff --git a/partner_tz/readme/CREDITS.md b/partner_tz/readme/CREDITS.md new file mode 100644 index 000000000000..705d3b30cad9 --- /dev/null +++ b/partner_tz/readme/CREDITS.md @@ -0,0 +1,3 @@ +The development of this module has been financially supported by: + +- Camptocamp diff --git a/partner_tz/readme/DESCRIPTION.md b/partner_tz/readme/DESCRIPTION.md new file mode 100644 index 000000000000..6d950a8d7033 --- /dev/null +++ b/partner_tz/readme/DESCRIPTION.md @@ -0,0 +1,2 @@ +This module displays the timezone field on partner form view. Moreover +it provides a few tools function in tools/tz_utils.py. diff --git a/partner_tz/readme/ROADMAP.md b/partner_tz/readme/ROADMAP.md new file mode 100644 index 000000000000..bff9306f8c34 --- /dev/null +++ b/partner_tz/readme/ROADMAP.md @@ -0,0 +1,8 @@ +- Odoo default value for timezone field uses the tz set on the connected + user. So if a user creates a partner located in a different timezone + than his own, the created partner will be set in the user's timezone + instead of having the timezone from its own location. Ideally, we + should change this behaviour to get the timezone from the country and + city of the partner when they are defined. + might be a + good starting point. diff --git a/partner_tz/static/description/icon.png b/partner_tz/static/description/icon.png new file mode 100644 index 000000000000..3a0328b516c4 Binary files /dev/null and b/partner_tz/static/description/icon.png differ diff --git a/partner_tz/static/description/index.html b/partner_tz/static/description/index.html new file mode 100644 index 000000000000..beee9214cc43 --- /dev/null +++ b/partner_tz/static/description/index.html @@ -0,0 +1,454 @@ + + + + + +Partner timezone + + + +
+

Partner timezone

+ + +

Beta License: AGPL-3 OCA/partner-contact Translate me on Weblate Try me on Runboat

+

This module displays the timezone field on partner form view. Moreover +it provides a few tools function in tools/tz_utils.py.

+

Table of contents

+ +
+

Known issues / Roadmap

+
    +
  • Odoo default value for timezone field uses the tz set on the +connected user. So if a user creates a partner located in a different +timezone than his own, the created partner will be set in the user’s +timezone instead of having the timezone from its own location. +Ideally, we should change this behaviour to get the timezone from the +country and city of the partner when they are defined. +http://www.geonames.org/export/web-services.html#timezone might be a +good starting point.
  • +
+
+
+

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

+
    +
  • Camptocamp
  • +
+
+
+

Contributors

+ +
+
+

Other credits

+

The development of this module has been financially supported by:

+
    +
  • Camptocamp
  • +
+
+
+

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.

+

This module is part of the OCA/partner-contact project on GitHub.

+

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

+
+
+
+ + diff --git a/partner_tz/tests/__init__.py b/partner_tz/tests/__init__.py new file mode 100644 index 000000000000..9a72706df417 --- /dev/null +++ b/partner_tz/tests/__init__.py @@ -0,0 +1 @@ +from . import test_tz diff --git a/partner_tz/tests/common.py b/partner_tz/tests/common.py new file mode 100644 index 000000000000..ec6ccacbd197 --- /dev/null +++ b/partner_tz/tests/common.py @@ -0,0 +1,28 @@ +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + + +class CommonPartnerTz: + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) + cls.partner_obj = cls.env["res.partner"] + cls.partner_utc = cls.partner_obj.create( + { + "name": "Partner UTC", + "tz": "UTC", + } + ) + cls.partner_brussels = cls.partner_obj.create( + { + "name": "Partner Brussels", + "tz": "Europe/Brussels", + } + ) + cls.partner_new_york = cls.partner_obj.create( + { + "name": "Partner New York", + "tz": "America/New_York", + } + ) diff --git a/partner_tz/tests/test_tz.py b/partner_tz/tests/test_tz.py new file mode 100644 index 000000000000..24d6688e759b --- /dev/null +++ b/partner_tz/tests/test_tz.py @@ -0,0 +1,97 @@ +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from datetime import time + +from freezegun import freeze_time + +from odoo.fields import Datetime +from odoo.tests.common import TransactionCase + +from ..tools import ( + tz_to_tz_naive_datetime, + tz_to_tz_time, + tz_to_utc_naive_datetime, + tz_to_utc_time, + utc_to_tz_naive_datetime, + utc_to_tz_time, +) +from .common import CommonPartnerTz + + +class TestPartnerTz(CommonPartnerTz, TransactionCase): + @freeze_time("2023-02-07 13:00:00") + def test_naive_from_utc(self): + # Winter now in Brussels (UTC +1) + now = Datetime.now() + + tz_now = tz_to_tz_naive_datetime("UTC", self.partner_brussels.tz, now) + self.assertEqual(Datetime.to_datetime("2023-02-07 14:00:00"), tz_now) + + @freeze_time("2023-02-07 13:00:00") + def test_naive_from_new_york(self): + # Winter now in Brussels (UTC +1 + # 13:00:00 + 6 = 19:00:00 + now = Datetime.now() + + tz_now = tz_to_tz_naive_datetime( + self.partner_new_york.tz, self.partner_brussels.tz, now + ) + self.assertEqual(Datetime.to_datetime("2023-02-07 19:00:00"), tz_now) + + @freeze_time("2023-02-07 13:00:00") + def test_naive_to_utc(self): + # 13:00:00 - 1 = 12:00:00 + now = Datetime.now() + tz_now = tz_to_utc_naive_datetime(self.partner_brussels.tz, now) + self.assertEqual(Datetime.to_datetime("2023-02-07 12:00:00"), tz_now) + + @freeze_time("2023-02-07 13:00:00") + def test_utc_to_naive(self): + # 13:00:00 + 1 = 14:00:00 + now = Datetime.now() + tz_now = utc_to_tz_naive_datetime(self.partner_brussels.tz, now) + self.assertEqual(Datetime.to_datetime("2023-02-07 14:00:00"), tz_now) + + @freeze_time("2023-02-07") + def test_time_from_new_york(self): + # Time : 13:00 + # 13:00 + 6 = 19:00 + the_time = time(13, 0) + + tz_time = tz_to_tz_time( + self.partner_new_york.tz, self.partner_brussels.tz, the_time + ) + + self.assertEqual(time(19, 0), tz_time) + + @freeze_time("2023-02-07") + def test_time_from_brussels(self): + # Time : 13:00 + # 13:00 - 6 = 7:00 + the_time = time(13, 0) + + tz_time = tz_to_tz_time( + self.partner_brussels.tz, self.partner_new_york.tz, the_time + ) + + self.assertEqual(time(7, 0), tz_time) + + @freeze_time("2023-02-07") + def test_time_to_utc(self): + # Time : 13:00 + # 13:00 - 6 = 7:00 + the_time = time(13, 0) + + tz_time = tz_to_utc_time(self.partner_brussels.tz, the_time) + + self.assertEqual(time(12, 0), tz_time) + + @freeze_time("2023-02-07") + def test_utc_to_time(self): + # Time : 13:00 + # 13:00 + 1 = 14:00 + the_time = time(13, 0) + + tz_time = utc_to_tz_time(self.partner_brussels.tz, the_time) + + self.assertEqual(time(14, 0), tz_time) diff --git a/partner_tz/tools/__init__.py b/partner_tz/tools/__init__.py new file mode 100644 index 000000000000..36420787f3ed --- /dev/null +++ b/partner_tz/tools/__init__.py @@ -0,0 +1,6 @@ +from .tz_utils import tz_to_tz_naive_datetime +from .tz_utils import tz_to_utc_naive_datetime +from .tz_utils import utc_to_tz_naive_datetime +from .tz_utils import tz_to_tz_time +from .tz_utils import tz_to_utc_time +from .tz_utils import utc_to_tz_time diff --git a/partner_tz/tools/tz_utils.py b/partner_tz/tools/tz_utils.py new file mode 100644 index 000000000000..e1b5a8c5d0d4 --- /dev/null +++ b/partner_tz/tools/tz_utils.py @@ -0,0 +1,58 @@ +# Copyright 2020 Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +from datetime import date, datetime + +import pytz + +UTC_TZ = pytz.timezone("UTC") + + +def tz_to_tz_naive_datetime(from_tz, to_tz, date_time): + """ + Convert tz-naive datetime from a specifc tz to a tz-naive datetime of a specific tz + + :param from_tz: pytz.timezone object or tz selection value + :param to_tz: pytz.timezone object or tz selection value + :param date_time: tz-naive datetime.datetime object + :return: tz-naive datetime.datetime object + """ + if isinstance(from_tz, str): + from_tz = pytz.timezone(from_tz) + if isinstance(to_tz, str): + to_tz = pytz.timezone(to_tz) + return from_tz.localize(date_time).astimezone(to_tz).replace(tzinfo=None) + + +def tz_to_utc_naive_datetime(from_tz, date_time): + return tz_to_tz_naive_datetime(from_tz, UTC_TZ, date_time) + + +def utc_to_tz_naive_datetime(to_tz, date_time): + return tz_to_tz_naive_datetime(UTC_TZ, to_tz, date_time) + + +def tz_to_tz_time(from_tz, to_tz, time, base_date=None): + """ + Convert datetime.time from a specific tz to a datetime.time of a specific tz + + :param from_tz: pytz.timezone object or tz selection value + :param to_tz: pytz.timezone object or tz selection value + :param time: datetime.time object + :param base_date: OPTIONAL datetime.date or datetime.datetime object to use + for the conversion + :return: datetime.time object + """ + # Combine time with a date + if base_date is None: + base_date = date.today() + date_time = datetime.combine(base_date, time) + new_date_time = tz_to_tz_naive_datetime(from_tz, to_tz, date_time) + return new_date_time.time() + + +def tz_to_utc_time(from_tz, time, base_date=None): + return tz_to_tz_time(from_tz, UTC_TZ, time, base_date=base_date) + + +def utc_to_tz_time(to_tz, time, base_date=None): + return tz_to_tz_time(UTC_TZ, to_tz, time, base_date=base_date) diff --git a/partner_tz/views/res_partner.xml b/partner_tz/views/res_partner.xml new file mode 100644 index 000000000000..0f79f51a26c5 --- /dev/null +++ b/partner_tz/views/res_partner.xml @@ -0,0 +1,13 @@ + + + + res.partner.form.inherit + res.partner + + + + + + + +