diff --git a/base_jsonify/README.rst b/base_jsonify/README.rst index 55e0e61db..d50eb10ba 100644 --- a/base_jsonify/README.rst +++ b/base_jsonify/README.rst @@ -2,9 +2,9 @@ :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html :alt: License: AGPL-3 -============== +============ Base Jsonify -============== +============ This module add the jsonify method to the ORM. This method take as argument the browse record and the "parser" that specify the field to extract. @@ -76,7 +76,7 @@ This is a technical module not function feature is added Known issues / Roadmap ====================== -Nothing yet +No distinction between float 0.0L and no value Bug Tracker =========== @@ -98,6 +98,7 @@ Contributors ------------ * BEAU Sébastien +* Raphaël Reverdy * Laurent Mignon Maintainer diff --git a/base_jsonify/__init__.py b/base_jsonify/__init__.py index d34d08cb1..a0fdc10fe 100644 --- a/base_jsonify/__init__.py +++ b/base_jsonify/__init__.py @@ -1,7 +1,2 @@ # -*- coding: utf-8 -*- -# © 2016 Akretion (http://www.akretion.com) -# Sébastien BEAU -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - - from . import models diff --git a/base_jsonify/__openerp__.py b/base_jsonify/__manifest__.py similarity index 84% rename from base_jsonify/__openerp__.py rename to base_jsonify/__manifest__.py index ad47b797d..c0d746be7 100644 --- a/base_jsonify/__openerp__.py +++ b/base_jsonify/__manifest__.py @@ -1,12 +1,13 @@ # -*- coding: utf-8 -*- -# © 2016 Akretion (http://www.akretion.com) +# © 2017 Akretion (http://www.akretion.com) # Sébastien BEAU +# Raphaël Reverdy # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). { "name": "Base Jsonify", "summary": "Base module that provide the jsonify method on all object", - "version": "8.0.1.0.0", + "version": "10.0.1.0.0", "category": "Uncategorized", "website": "https://odoo-community.org/", "author": "Akretion, Odoo Community Association (OCA)", diff --git a/base_jsonify/demo/export_demo.xml b/base_jsonify/demo/export_demo.xml index 4501b77d3..a9030c6de 100644 --- a/base_jsonify/demo/export_demo.xml +++ b/base_jsonify/demo/export_demo.xml @@ -1,11 +1,7 @@ - - - - - Partner Export - res.partner - - - - + + + Partner Export + res.partner + + \ No newline at end of file diff --git a/base_jsonify/models/__init__.py b/base_jsonify/models/__init__.py index c6621497b..635654e95 100644 --- a/base_jsonify/models/__init__.py +++ b/base_jsonify/models/__init__.py @@ -1,9 +1,4 @@ # -*- coding: utf-8 -*- -# © 2016 Akretion (http://www.akretion.com) -# Sébastien BEAU -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - - from . import models from . import ir_export from . import ir_exports_line diff --git a/base_jsonify/models/ir_export.py b/base_jsonify/models/ir_export.py index 810c79087..feae5cddf 100644 --- a/base_jsonify/models/ir_export.py +++ b/base_jsonify/models/ir_export.py @@ -1,10 +1,10 @@ # -*- coding: utf-8 -*- -# © 2016 Akretion (http://www.akretion.com) +# © 2017 Akretion (http://www.akretion.com) # Sébastien BEAU # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from collections import OrderedDict -from openerp import api, models +from odoo import api, models def update_dict(data, fields): diff --git a/base_jsonify/models/ir_exports_line.py b/base_jsonify/models/ir_exports_line.py index a1e2cd6c3..6e27a844a 100644 --- a/base_jsonify/models/ir_exports_line.py +++ b/base_jsonify/models/ir_exports_line.py @@ -2,8 +2,8 @@ # Copyright 2017 ACSONE SA/NV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from openerp.exceptions import ValidationError -from openerp import api, fields, models, _ +from odoo.exceptions import ValidationError +from odoo import api, fields, models, _ class IrExportsLine(models.Model): diff --git a/base_jsonify/models/models.py b/base_jsonify/models/models.py index 60b41ccb2..5a6afc7a2 100644 --- a/base_jsonify/models/models.py +++ b/base_jsonify/models/models.py @@ -1,76 +1,80 @@ # -*- coding: utf-8 -*- -# © 2016 Akretion (http://www.akretion.com) +# © 2017 Akretion (http://www.akretion.com) # Sébastien BEAU +# Raphaël Reverdy # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from openerp import api, models -from openerp.exceptions import Warning as UserError -from openerp.tools.translate import _ +from odoo import api, models +from odoo.exceptions import UserError +from odoo.tools.translate import _ -def __parse_field(parser_field): - field_name = parser_field - subparser = None - if isinstance(parser_field, tuple): - field_name, subparser = parser_field - json_key = field_name - if ':' in field_name: - field_name, json_key = field_name.split(':') - return field_name, json_key, subparser +class Base(models.AbstractModel): + _inherit = 'base' -@api.multi -def jsonify(self, parser): - """ Convert the record according to the parser given - Example of parser: - parser = [ - 'name', - 'number', - 'create_date', - ('partner_id', ['id', 'display_name', 'ref']) - ('line_id', ['id', ('product_id', ['name']), 'price_unit']) - ] + @api.model + def __parse_field(self, parser_field): + field_name = parser_field + subparser = None + if isinstance(parser_field, tuple): + field_name, subparser = parser_field + json_key = field_name + if ':' in field_name: + field_name, json_key = field_name.split(':') + return field_name, json_key, subparser - In order to be consitent with the odoo api the jsonify method always - return a list of object even if there is only one element in input - 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 key, you - can define your mapping as follow into the parser definition: + @api.multi + def jsonify(self, parser): + """ Convert the record according to the parser given + Example of parser: + parser = [ + 'name', + 'number', + 'create_date', + ('partner_id', ['id', 'display_name', 'ref']) + ('line_id', ['id', ('product_id', ['name']), 'price_unit']) + ] - parser = [ - 'field_name:json_key' - ] + In order to be consitent with the odoo api the jsonify method always + return a list of object even if there is only one element in input - """ - result = [] - empty_value = { - 'char': '', - 'int': 0, - 'float': 0, - } + 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 key, you + can define your mapping as follow into the parser definition: - for rec in self: - res = {} - for field in parser: - field_name, json_key, subparser = __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')) - 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] - result.append(res) - return result + parser = [ + 'field_name:json_key' + ] + """ + result = [] + empty_value = { + 'char': None, + 'int': None, + # 'float': None, TODO: 0.0 != False + 'text': None, + } -models.Model.jsonify = jsonify + for rec in self: + 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')) + 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] + result.append(res) + return result diff --git a/base_jsonify/tests/__init__.py b/base_jsonify/tests/__init__.py index 837c1145d..bf58ee4e9 100644 --- a/base_jsonify/tests/__init__.py +++ b/base_jsonify/tests/__init__.py @@ -1,7 +1,3 @@ # -*- coding: utf-8 -*- -# © 2016 Akretion (http://www.akretion.com) -# Sébastien BEAU -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - from . import test_get_parser from . import test_ir_exports_line diff --git a/base_jsonify/tests/test_get_parser.py b/base_jsonify/tests/test_get_parser.py index 76db9f3dc..e73267fb2 100644 --- a/base_jsonify/tests/test_get_parser.py +++ b/base_jsonify/tests/test_get_parser.py @@ -2,7 +2,7 @@ # © # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from openerp.tests.common import TransactionCase +from odoo.tests.common import TransactionCase class TestParser(TransactionCase): @@ -57,17 +57,18 @@ class TestParser(TransactionCase): partner = self.env['res.partner'].create({ 'name': 'Akretion', 'country_id': self.env.ref('base.fr').id, + 'lang': 'en_US', # default 'category_id': [(0, 0, {'name': 'Inovator'})], 'child_ids': [ (0, 0, { 'name': 'Sebatien Beau', 'country_id': self.env.ref('base.fr').id }) - ] + ], }) expected_json = [{ - u'lang': False, - u'comment': False, + u'lang': u'en_US', + u'comment': None, u'credit_limit': 0.0, u'name': u'Akretion', u'color': 0, @@ -87,8 +88,9 @@ class TestParser(TransactionCase): }, u'children': [], u'name': u'Sebatien Beau', - u'email': False + u'email': None }] }] json_partner = partner.jsonify(parser) + self.assertDictEqual(json_partner[0], expected_json[0]) diff --git a/base_jsonify/tests/test_ir_exports_line.py b/base_jsonify/tests/test_ir_exports_line.py index 3622ced8c..020f9b25c 100644 --- a/base_jsonify/tests/test_ir_exports_line.py +++ b/base_jsonify/tests/test_ir_exports_line.py @@ -2,8 +2,8 @@ # © 2017 ACSONE SA/NV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from openerp.exceptions import ValidationError -from openerp.tests.common import TransactionCase +from odoo.exceptions import ValidationError +from odoo.tests.common import TransactionCase class TestIrExportsLine(TransactionCase):