Browse Source

Merge PR #1714 into 12.0

Signed-off-by moylop260
12.0
OCA-git-bot 5 years ago
parent
commit
558738769e
  1. 10
      base_jsonify/models/models.py
  2. 13
      base_jsonify/tests/test_get_parser.py

10
base_jsonify/models/models.py

@ -72,9 +72,15 @@ class Base(models.AbstractModel):
if value is False and field_type != 'boolean': if value is False and field_type != 'boolean':
value = None value = None
elif field_type == "date": elif field_type == "date":
value = fields.Date.to_string(value)
value = fields.Date.to_date(value).isoformat()
elif field_type == "datetime": 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 res[json_key] = value
result.append(res) result.append(res)
return result return result

13
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',
@ -70,8 +72,13 @@ class TestParser(TransactionCase):
'country_id': self.env.ref('base.fr').id '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 = { expected_json = {
'lang': 'en_US', 'lang': 'en_US',
'comment': None, 'comment': None,
@ -96,8 +103,8 @@ class TestParser(TransactionCase):
'name': 'Sebatien Beau', 'name': 'Sebatien Beau',
'email': None '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) json_partner = partner.jsonify(parser)

Loading…
Cancel
Save