Skip to content

Commit

Permalink
[FIX] base_jsonify: Serialize datetime into the user's timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
lmignon committed Oct 31, 2019
1 parent b75456c commit df0998a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 7 additions & 7 deletions base_jsonify/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
# Raphaël Reverdy <[email protected]>
# 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 _
Expand Down Expand Up @@ -76,11 +74,13 @@ def jsonify(self, parser):
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 changes: 3 additions & 1 deletion base_jsonify/tests/test_get_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def test_getting_parser(self):
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',
Expand Down Expand Up @@ -100,7 +102,7 @@ def test_json_export(self):
'name': 'Sebatien Beau',
'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)
Expand Down

0 comments on commit df0998a

Please sign in to comment.