diff --git a/partner_tz/__init__.py b/partner_tz/__init__.py new file mode 100644 index 000000000..738a2eec0 --- /dev/null +++ b/partner_tz/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import tools diff --git a/partner_tz/__manifest__.py b/partner_tz/__manifest__.py new file mode 100644 index 000000000..85d27d16d --- /dev/null +++ b/partner_tz/__manifest__.py @@ -0,0 +1,16 @@ +# 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": "13.0.1.0.0", + "development_status": "Alpha", + "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/models/__init__.py b/partner_tz/models/__init__.py new file mode 100644 index 000000000..91fed54d4 --- /dev/null +++ b/partner_tz/models/__init__.py @@ -0,0 +1 @@ +from . import res_partner diff --git a/partner_tz/models/res_partner.py b/partner_tz/models/res_partner.py new file mode 100644 index 000000000..ab4526abb --- /dev/null +++ b/partner_tz/models/res_partner.py @@ -0,0 +1,10 @@ +# Copyright 2020 Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +from odoo import fields, models + + +class ResPartner(models.Model): + + _inherit = 'res.partner' + + tz = fields.Selection(default=None) diff --git a/partner_tz/readme/CONTRIBUTORS.rst b/partner_tz/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..e31e2f0c4 --- /dev/null +++ b/partner_tz/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Akim Juillerat diff --git a/partner_tz/readme/DESCRIPTION.rst b/partner_tz/readme/DESCRIPTION.rst new file mode 100644 index 000000000..ca7456a53 --- /dev/null +++ b/partner_tz/readme/DESCRIPTION.rst @@ -0,0 +1,3 @@ +This module removes timezone default value on res.partner and display the field +on form view. +Moreover it provides a few tools function in `tools/tz_utils.py`. diff --git a/partner_tz/tools/__init__.py b/partner_tz/tools/__init__.py new file mode 100644 index 000000000..ed3bafe21 --- /dev/null +++ b/partner_tz/tools/__init__.py @@ -0,0 +1 @@ +from .tz_utils import * diff --git a/partner_tz/tools/tz_utils.py b/partner_tz/tools/tz_utils.py new file mode 100644 index 000000000..4d4076552 --- /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) +import pytz +from datetime import datetime, date + + +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 000000000..70e15379d --- /dev/null +++ b/partner_tz/views/res_partner.xml @@ -0,0 +1,13 @@ + + + + res.partner.form.inherit + res.partner + + + + + + + +