base_jsonify: return one record on demand
If you use this module extensively - as I do - you've done this tons of time. For sure :) This little change simplifies your code base where needed.pull/2418/head
parent
5f5b50eb18
commit
2b46ff032c
|
@ -26,7 +26,7 @@ class Base(models.AbstractModel):
|
||||||
field_name, json_key = field_name.split(":")
|
field_name, json_key = field_name.split(":")
|
||||||
return field_name, json_key, subparser
|
return field_name, json_key, subparser
|
||||||
|
|
||||||
def jsonify(self, parser):
|
def jsonify(self, parser, one=False):
|
||||||
"""Convert the record according to the given parser.
|
"""Convert the record according to the given parser.
|
||||||
|
|
||||||
Example of parser:
|
Example of parser:
|
||||||
|
@ -41,7 +41,8 @@ class Base(models.AbstractModel):
|
||||||
]
|
]
|
||||||
|
|
||||||
In order to be consistent with the odoo api the jsonify method always
|
In order to be consistent with the odoo api the jsonify method always
|
||||||
return a list of object even if there is only one element in input
|
return a list of object even if there is only one element in input.
|
||||||
|
You can change this behavior by passing `one=True` to get only one element.
|
||||||
|
|
||||||
By default the key into the json is the name of the field extracted
|
By default the key into the json is the name of the field extracted
|
||||||
from the model. If you need to specify an alternate name to use as
|
from the model. If you need to specify an alternate name to use as
|
||||||
|
@ -52,6 +53,9 @@ class Base(models.AbstractModel):
|
||||||
]
|
]
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
if one:
|
||||||
|
self.ensure_one()
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
|
|
||||||
for rec in self:
|
for rec in self:
|
||||||
|
@ -63,6 +67,8 @@ class Base(models.AbstractModel):
|
||||||
else:
|
else:
|
||||||
res[json_key] = rec._jsonify_value(field_name)
|
res[json_key] = rec._jsonify_value(field_name)
|
||||||
result.append(res)
|
result.append(res)
|
||||||
|
if one:
|
||||||
|
return result[0] if result else {}
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def _jsonify_value(self, field_name):
|
def _jsonify_value(self, field_name):
|
||||||
|
|
|
@ -134,6 +134,20 @@ class TestParser(SavepointCase):
|
||||||
expected_json["children"] = []
|
expected_json["children"] = []
|
||||||
self.assertDictEqual(json_partner[0], expected_json)
|
self.assertDictEqual(json_partner[0], expected_json)
|
||||||
|
|
||||||
|
def test_one(self):
|
||||||
|
parser = [
|
||||||
|
"name",
|
||||||
|
]
|
||||||
|
expected_json = {
|
||||||
|
"name": "Akretion",
|
||||||
|
}
|
||||||
|
json_partner = self.partner.jsonify(parser, one=True)
|
||||||
|
self.assertDictEqual(json_partner, expected_json)
|
||||||
|
# cannot call on multiple records
|
||||||
|
with self.assertRaises(ValueError) as err:
|
||||||
|
self.env["res.partner"].search([]).jsonify(parser, one=True)
|
||||||
|
self.assertIn("Expected singleton", str(err.exception))
|
||||||
|
|
||||||
def test_json_export_callable_parser(self):
|
def test_json_export_callable_parser(self):
|
||||||
self.partner.__class__.jsonify_custom = jsonify_custom
|
self.partner.__class__.jsonify_custom = jsonify_custom
|
||||||
parser = [
|
parser = [
|
||||||
|
|
Loading…
Reference in New Issue