Browse Source

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

12.0
Laurent Mignon (ACSONE) 5 years ago
parent
commit
b5543b68cb
  1. 14
      base_jsonify/models/models.py
  2. 4
      base_jsonify/tests/test_get_parser.py

14
base_jsonify/models/models.py

@ -3,8 +3,6 @@
# Raphaël Reverdy <raphael.reverdy@akretion.com> # Raphaël Reverdy <raphael.reverdy@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import pytz
from odoo import api, fields, models from odoo import api, fields, models
from odoo.exceptions import UserError from odoo.exceptions import UserError
from odoo.tools.translate import _ from odoo.tools.translate import _
@ -76,11 +74,13 @@ class Base(models.AbstractModel):
elif field_type == "date": elif field_type == "date":
value = fields.Date.to_date(value).isoformat() value = fields.Date.to_date(value).isoformat()
elif field_type == "datetime": 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 res[json_key] = value
result.append(res) result.append(res)
return result return result

4
base_jsonify/tests/test_get_parser.py

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

Loading…
Cancel
Save