From e868810b6f67810ccbea7b6a674331e30e053c2d Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Thu, 31 Oct 2019 15:46:33 +0100 Subject: [PATCH] [FIX] base_jsonify: Add tz info into serialized datetime --- .isort.cfg | 2 +- base_jsonify/models/models.py | 8 +++++++- base_jsonify/tests/test_get_parser.py | 7 ++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/.isort.cfg b/.isort.cfg index 98b216f74..da6da63fd 100644 --- a/.isort.cfg +++ b/.isort.cfg @@ -9,4 +9,4 @@ line_length=88 known_odoo=odoo known_odoo_addons=odoo.addons sections=FUTURE,STDLIB,THIRDPARTY,ODOO,ODOO_ADDONS,FIRSTPARTY,LOCALFOLDER -known_third_party=setuptools +known_third_party=pytz,setuptools diff --git a/base_jsonify/models/models.py b/base_jsonify/models/models.py index 5d5c31a29..24ad417f0 100644 --- a/base_jsonify/models/models.py +++ b/base_jsonify/models/models.py @@ -3,6 +3,8 @@ # Raphaƫl Reverdy # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import pytz + from odoo import api, fields, models from odoo.exceptions import UserError from odoo.tools.translate import _ @@ -72,7 +74,11 @@ class Base(models.AbstractModel): elif field_type == "date": value = fields.Date.to_date(value).isoformat() elif field_type == "datetime": - value = fields.Datetime.to_datetime(value).isoformat() + value = ( + fields.Datetime.to_datetime(value) + .replace(tzinfo=pytz.utc) + .isoformat() + ) res[json_key] = value result.append(res) return result diff --git a/base_jsonify/tests/test_get_parser.py b/base_jsonify/tests/test_get_parser.py index 5aec2538e..a27fb720a 100644 --- a/base_jsonify/tests/test_get_parser.py +++ b/base_jsonify/tests/test_get_parser.py @@ -82,6 +82,11 @@ class TestParser(TransactionCase): "date": fields.Date.from_string("2019-10-31"), } ) + # put our own create date to ease tests + self.env.cr.execute( + "update res_partner set create_date=%s where id=%s", + ("2019-10-31 14:39:49", partner.id), + ) expected_json = { "lang": "en_US", "comment": None, @@ -100,7 +105,7 @@ class TestParser(TransactionCase): "email": None, } ], - "create_date": partner.create_date.isoformat(), + "create_date": "2019-10-31T14:39:49+00:00", "date": "2019-10-31", } json_partner = partner.jsonify(parser)