Browse Source

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

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

4
base_jsonify/models/models.py

@ -72,9 +72,9 @@ 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)
value = fields.Datetime.to_datetime(value).isoformat()
res[json_key] = value res[json_key] = value
result.append(res) result.append(res)
return result return result

6
base_jsonify/tests/test_get_parser.py

@ -70,7 +70,7 @@ 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")
}) })
expected_json = { expected_json = {
'lang': 'en_US', 'lang': 'en_US',
@ -96,8 +96,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': partner.create_date.isoformat(),
'date': "2019-10-31"
} }
json_partner = partner.jsonify(parser) json_partner = partner.jsonify(parser)

Loading…
Cancel
Save