Browse Source

[FIX] base_jsonify: Add tz info into serialized datetime

12.0
Laurent Mignon (ACSONE) 5 years ago
parent
commit
74218643b1
  1. 8
      base_jsonify/models/models.py
  2. 9
      base_jsonify/tests/test_get_parser.py

8
base_jsonify/models/models.py

@ -3,6 +3,8 @@
# 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 _
@ -74,7 +76,11 @@ 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).isoformat()
value = (
fields.Datetime.to_datetime(value)
.replace(tzinfo=pytz.utc)
.isoformat()
)
res[json_key] = value res[json_key] = value
result.append(res) result.append(res)
return result return result

9
base_jsonify/tests/test_get_parser.py

@ -72,6 +72,11 @@ class TestParser(TransactionCase):
], ],
'date': fields.Date.from_string("2019-10-31") 'date': fields.Date.from_string("2019-10-31")
}) })
self.env.cr.execute(
"update res_partner set create_date=%s where id=%s",
("2019-10-31 14:39:49", partner.id),
)
partner.refresh()
expected_json = { expected_json = {
'lang': 'en_US', 'lang': 'en_US',
'comment': None, 'comment': None,
@ -96,8 +101,8 @@ class TestParser(TransactionCase):
'name': 'Sebatien Beau', 'name': 'Sebatien Beau',
'email': None 'email': None
}], }],
'create_date': partner.create_date.isoformat(),
'date': "2019-10-31"
"create_date": "2019-10-31T14:39:49+00:00",
"date": "2019-10-31",
} }
json_partner = partner.jsonify(parser) json_partner = partner.jsonify(parser)

Loading…
Cancel
Save