base_jsonify: ease override jsonify per record

pull/2418/head
Simone Orsi 2020-05-08 12:32:13 +02:00 committed by Sébastien BEAU
parent 48fdf12ad9
commit f947336f62
1 changed files with 33 additions and 24 deletions

View File

@ -54,31 +54,40 @@ class Base(models.AbstractModel):
res = {}
for field in parser:
field_name, json_key, subparser = self.__parse_field(field)
field_type = rec._fields[field_name].type
if subparser:
if field_type in ("one2many", "many2many"):
res[json_key] = rec[field_name].jsonify(subparser)
elif field_type in ("many2one", "reference"):
if rec[field_name]:
res[json_key] = rec[field_name].jsonify(subparser)[0]
else:
res[json_key] = None
else:
raise UserError(_("Wrong parser configuration"))
res[json_key] = rec._jsonify_value_subparser(field_name, subparser)
else:
value = rec[field_name]
if value is False and field_type != "boolean":
value = None
elif field_type == "date":
value = fields.Date.to_date(value).isoformat()
elif field_type == "datetime":
# 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] = rec._jsonify_value(field_name)
result.append(res)
return result
def _jsonify_value(self, field_name):
field_type = self._fields[field_name].type
value = self[field_name]
if value is False and field_type != "boolean":
value = None
elif field_type == "date":
value = fields.Date.to_date(value).isoformat()
elif field_type == "datetime":
# 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(self, value)
value = value.isoformat()
return value
def _jsonify_value_subparser(self, field_name, subparser):
field_type = self._fields[field_name].type
if field_type in ("one2many", "many2many"):
value = self[field_name].jsonify(subparser)
elif field_type in ("many2one", "reference"):
if self[field_name]:
value = self[field_name].jsonify(subparser)[0]
else:
# TODO: we should get this by field (eg: char field -> "")
value = None
else:
raise UserError(_("Wrong parser configuration"))
return value