Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.0][IMP] base_jsonify: Serialize Date and Datetime into ISO 8601 format #1714

Merged
merged 3 commits into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions base_jsonify/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,15 @@ def jsonify(self, parser):
if value is False and field_type != 'boolean':
value = None
elif field_type == "date":
value = fields.Date.to_string(value)
value = fields.Date.to_date(value).isoformat()
elif field_type == "datetime":
value = fields.Datetime.to_string(value)
# 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
13 changes: 10 additions & 3 deletions 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 @@ -70,8 +72,13 @@ def test_json_export(self):
'country_id': self.env.ref('base.fr').id
})
],
'date': fields.Date.today()
'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 = {
'lang': 'en_US',
'comment': None,
Expand All @@ -96,8 +103,8 @@ def test_json_export(self):
'name': 'Sebatien Beau',
'email': None
}],
'create_date': fields.Datetime.to_string(partner.create_date),
'date': fields.Date.to_string(partner.date)
"create_date": "2019-10-31T15:39:49+01:00",
"date": "2019-10-31",
}
json_partner = partner.jsonify(parser)

Expand Down