Browse Source

[FIX] base_jsonify: Serialize datetime into the user's timezone

13.0
Laurent Mignon (ACSONE) 5 years ago
parent
commit
eb25b23b4e
  1. 2
      .isort.cfg
  2. 14
      base_jsonify/models/models.py
  3. 4
      base_jsonify/tests/test_get_parser.py

2
.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=pytz,setuptools
known_third_party=setuptools

14
base_jsonify/models/models.py

@ -3,8 +3,6 @@
# Raphaël Reverdy <raphael.reverdy@akretion.com>
# 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 _
@ -74,11 +72,13 @@ 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)
.replace(tzinfo=pytz.utc)
.isoformat()
)
# Ensures value is a datetime
value = fields.Datetime.to_datetime(value)
# Get the timestamp converted to the client's timezone.
# This call also add the tzinfo into the datetime
# object
value = fields.Datetime.context_timestamp(rec, value)
value = value.isoformat()
res[json_key] = value
result.append(res)
return result

4
base_jsonify/tests/test_get_parser.py

@ -41,6 +41,8 @@ class TestParser(TransactionCase):
self.assertEqual(parser, expected_parser)
def test_json_export(self):
# Enforces TZ to validate the serialization result of a Datetime
self.env.user.tz = "Europe/Brussels"
parser = [
"lang",
"comment",
@ -105,7 +107,7 @@ class TestParser(TransactionCase):
"email": None,
}
],
"create_date": "2019-10-31T14:39:49+00:00",
"create_date": "2019-10-31T15:39:49+01:00",
"date": "2019-10-31",
}
json_partner = partner.jsonify(parser)

Loading…
Cancel
Save