Browse Source

base_jsonify: Output 'false' into json only for boolean fields

pull/1379/head
Laurent Mignon (ACSONE) 6 years ago
committed by Pierrick Brun
parent
commit
3ca15ceada
  1. 13
      base_jsonify/models/models.py
  2. 22
      base_jsonify/tests/test_get_parser.py

13
base_jsonify/models/models.py

@ -50,12 +50,6 @@ class Base(models.AbstractModel):
"""
result = []
empty_value = {
'char': None,
'int': None,
# 'float': None, TODO: 0.0 != False
'text': None,
}
for rec in self:
res = {}
@ -73,8 +67,9 @@ class Base(models.AbstractModel):
else:
raise UserError(_('Wrong parser configuration'))
else:
res[json_key] = rec[field_name]
if not res[json_key] and field_type in empty_value:
res[json_key] = empty_value[field_type]
value = rec[field_name]
if value is False and field_type != 'boolean':
value = None
res[json_key] = value
result.append(res)
return result

22
base_jsonify/tests/test_get_parser.py

@ -66,7 +66,7 @@ class TestParser(TransactionCase):
})
],
})
expected_json = [{
expected_json = {
u'lang': u'en_US',
u'comment': None,
u'credit_limit': 0.0,
@ -90,7 +90,23 @@ class TestParser(TransactionCase):
u'name': u'Sebatien Beau',
u'email': None
}]
}]
}
json_partner = partner.jsonify(parser)
self.assertDictEqual(json_partner[0], expected_json[0])
self.assertDictEqual(json_partner[0], expected_json)
json_partner = partner.jsonify(parser)
self.assertDictEqual(json_partner[0], expected_json)
# Check that only boolean fields have boolean values into json
# By default if a field is not set into Odoo, the value is always False
# This value is not the expected one into the json
partner.write({'child_ids': [(6, 0, [])],
'active': False,
'lang': False})
json_partner = partner.jsonify(parser)
expected_json['active'] = False
expected_json['lang'] = None
expected_json['children'] = []
self.assertDictEqual(json_partner[0], expected_json)
Loading…
Cancel
Save