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

[13.0] Add module partner_tz #876

Merged
merged 7 commits into from
Sep 29, 2020
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 partner_tz/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import tools
16 changes: 16 additions & 0 deletions partner_tz/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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",
guewen marked this conversation as resolved.
Show resolved Hide resolved
"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"],
}
1 change: 1 addition & 0 deletions partner_tz/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Akim Juillerat <akim.juillerat@camptocamp.com>
2 changes: 2 additions & 0 deletions partner_tz/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -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`.
8 changes: 8 additions & 0 deletions partner_tz/readme/ROADMAP.rst
Original file line number Diff line number Diff line change
@@ -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.
http://www.geonames.org/export/web-services.html#timezone might be a good
starting point.
6 changes: 6 additions & 0 deletions partner_tz/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -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
58 changes: 58 additions & 0 deletions partner_tz/tools/tz_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 2020 Camptocamp SA
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The functions presented here are very thin wrappers around the original functions from pytz. I think this is of questionable value. Better use the original pytz functions that are generally available and not depending on having this particular module installed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx for your review @NL66278
The value of a function is quite subjective in any case, but to me they make tz conversions more explicit and avoid repeats. Moreover they are only tools that won't change anything when someone installs this module.
So I'm sorry but I don't see why having this file here, and using it in other modules having this module as a dependency would be a problem...

# 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):
guewen marked this conversation as resolved.
Show resolved Hide resolved
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)
13 changes: 13 additions & 0 deletions partner_tz/views/res_partner.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="view_partner_form_inherit" model="ir.ui.view">
<field name="name">res.partner.form.inherit</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form" />
<field name="arch" type="xml">
<field name="vat" position="before">
<field name="tz" />
</field>
</field>
</record>
</odoo>
1 change: 1 addition & 0 deletions setup/partner_tz/odoo/addons/partner_tz
6 changes: 6 additions & 0 deletions setup/partner_tz/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)