diff --git a/base_jsonify/README.rst b/base_jsonify/README.rst new file mode 100644 index 000000000..65ffe69ef --- /dev/null +++ b/base_jsonify/README.rst @@ -0,0 +1,116 @@ +============ +Base Jsonify +============ + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github + :target: https://github.com/OCA/server-tools/tree/10.0/base_jsonify + :alt: OCA/server-tools +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/server-tools-10-0/server-tools-10-0-base_jsonify + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/149/10.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +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. + +Example of parser: + + +.. code-block:: python + + parser = [ + 'name', + 'number', + 'create_date', + ('partner_id', ['id', 'display_name', 'ref']) + ('line_id', ['id', ('product_id', ['name']), 'price_unit']) + ] + +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: + +.. code-block:: python + + parser = [ + 'field_name:json_key' + ] + +.. code-block:: python + + + parser = [ + 'name', + 'number', + 'create_date:creationDate', + ('partner_id:partners', ['id', 'display_name', 'ref']) + ('line_id:lines', ['id', ('product_id', ['name']), 'price_unit']) + ] + +Also the module provide a method "get_json_parser" on the ir.exports object +that generate a parser from an ir.exports configuration + +**Table of contents** + +.. contents:: + :local: + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Akretion + +Contributors +~~~~~~~~~~~~ + +* BEAU Sébastien +* Raphaël Reverdy +* Laurent Mignon + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/server-tools `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/base_jsonify/__init__.py b/base_jsonify/__init__.py new file mode 100644 index 000000000..a0fdc10fe --- /dev/null +++ b/base_jsonify/__init__.py @@ -0,0 +1,2 @@ +# -*- coding: utf-8 -*- +from . import models diff --git a/base_jsonify/__manifest__.py b/base_jsonify/__manifest__.py new file mode 100644 index 000000000..33c626d4c --- /dev/null +++ b/base_jsonify/__manifest__.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# Copyright 2017-2018 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 models", + "version": "10.0.1.0.0", + "category": "Uncategorized", + "website": "https://github.com/OCA/server-tools", + "author": "Akretion, Odoo Community Association (OCA)", + "license": "AGPL-3", + "application": False, + "installable": True, + "depends": [ + "base", + ], + "data": [ + 'views/ir_exports_view.xml', + ], + "demo": [ + 'demo/export_demo.xml', + 'demo/ir.exports.line.csv', + ], +} diff --git a/base_jsonify/demo/export_demo.xml b/base_jsonify/demo/export_demo.xml new file mode 100644 index 000000000..de8566b76 --- /dev/null +++ b/base_jsonify/demo/export_demo.xml @@ -0,0 +1,7 @@ + + + + Partner Export + res.partner + + diff --git a/base_jsonify/demo/ir.exports.line.csv b/base_jsonify/demo/ir.exports.line.csv new file mode 100644 index 000000000..476de3937 --- /dev/null +++ b/base_jsonify/demo/ir.exports.line.csv @@ -0,0 +1,16 @@ +id,export_id/id,name +name,ir_exp_partner,name +active,ir_exp_partner,active +credit_limit,ir_exp_partner,credit_limit +color,ir_exp_partner,color +category_id_name,ir_exp_partner,category_id/name +country_id_name,ir_exp_partner,country_id/name +country_id_code,ir_exp_partner,country_id/code +child_ids_name,ir_exp_partner,child_ids/name +child_ids_id,ir_exp_partner,child_ids/id +child_ids_email,ir_exp_partner,child_ids/email +child_ids_country_id_name,ir_exp_partner,child_ids/country_id/name +child_ids_country_id_code,ir_exp_partner,child_ids/country_id/code +child_ids_child_ids_name,ir_exp_partner,child_ids/child_ids/name +lang,ir_exp_partner,lang +comment,ir_exp_partner,comment diff --git a/base_jsonify/models/__init__.py b/base_jsonify/models/__init__.py new file mode 100644 index 000000000..635654e95 --- /dev/null +++ b/base_jsonify/models/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +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 new file mode 100644 index 000000000..4e71c1d7c --- /dev/null +++ b/base_jsonify/models/ir_export.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +# © 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 odoo import api, models + + +def update_dict(data, fields): + """ + Contruct a tree of fields. + ie: { + "name": True, + "resource": True, + } + Order of keys is important. + """ + field = fields[0] + if len(fields) == 1: + if field == '.id': + field = 'id' + data[field] = True + else: + if field not in data: + data[field] = OrderedDict() + update_dict(data[field], fields[1:]) + + +def convert_dict(dict_parser): + """ + Converts the dict returned by update_dict to a list consistent with the + Odoo API. The list is composed of strings (field names or aliases) or + tuples. + """ + parser = [] + for field, value in dict_parser.iteritems(): + if value is True: + parser.append(field) + else: + parser.append((field, convert_dict(value))) + return parser + + +class IrExport(models.Model): + _inherit = 'ir.exports' + + @api.multi + def get_json_parser(self): + """ + Creates a parser from a ir_exports record and returns it. This parser + can then be used to "jsonify" records of the ir_export's model. + """ + self.ensure_one() + dict_parser = OrderedDict() + for line in self.export_fields: + names = line.name.split('/') + if line.alias: + names = line.alias.split('/') + update_dict(dict_parser, names) + + return convert_dict(dict_parser) diff --git a/base_jsonify/models/ir_exports_line.py b/base_jsonify/models/ir_exports_line.py new file mode 100644 index 000000000..3d4b047ca --- /dev/null +++ b/base_jsonify/models/ir_exports_line.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.exceptions import ValidationError +from odoo import api, fields, models, _ + + +class IrExportsLine(models.Model): + _inherit = 'ir.exports.line' + _order = 'name' + + alias = fields.Char( + 'Alias', + help='The complete path to the field where you can specify an ' + 'alias on the a step as field:alias' + ) + + @api.constrains('alias', 'name') + def _check_alias(self): + for rec in self: + if not rec.alias: + continue + names = rec.name.split('/') + names_with_alias = rec.alias.split('/') + if len(names) != len(names_with_alias): + raise ValidationError( + _("Name and Alias must have the same hierarchy depth")) + for name, name_with_alias in zip(names, names_with_alias): + field_name = name_with_alias.split(':')[0] + if name != field_name: + raise ValidationError( + _("The alias must reference the same field as in " + "name '%s' not in '%s'") % (name, name_with_alias) + ) diff --git a/base_jsonify/models/models.py b/base_jsonify/models/models.py new file mode 100644 index 000000000..3fd8b9815 --- /dev/null +++ b/base_jsonify/models/models.py @@ -0,0 +1,78 @@ +# -*- coding: utf-8 -*- +# © 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 odoo import api, models +from odoo.exceptions import UserError +from odoo.tools.translate import _ + + +class Base(models.AbstractModel): + + _inherit = 'base' + + @api.model + def __parse_field(self, parser_field): + """ + Deducts how to handle a field from its parser + """ + 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 + + @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']) + ] + + 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 + + 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: + + parser = [ + 'field_name:json_key' + ] + + """ + result = [] + + 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: + value = rec[field_name] + if value is False and field_type != 'boolean': + value = None + res[json_key] = value + result.append(res) + return result diff --git a/base_jsonify/readme/CONTRIBUTORS.rst b/base_jsonify/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..717c76041 --- /dev/null +++ b/base_jsonify/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* BEAU Sébastien +* Raphaël Reverdy +* Laurent Mignon diff --git a/base_jsonify/readme/DESCRIPTION.rst b/base_jsonify/readme/DESCRIPTION.rst new file mode 100644 index 000000000..ff92efef6 --- /dev/null +++ b/base_jsonify/readme/DESCRIPTION.rst @@ -0,0 +1,43 @@ +This module adds a 'jsonify' method to every model of the ORM. +It works on the current recordset and requires a single argument 'parser' +that specify the field to extract. + +Example of parser: + + +.. code-block:: python + + parser = [ + 'name', + 'number', + 'create_date', + ('partner_id', ['id', 'display_name', 'ref']) + ('line_id', ['id', ('product_id', ['name']), 'price_unit']) + ] + +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: + +.. code-block:: python + + parser = [ + 'field_name:json_key' + ] + +.. code-block:: python + + + parser = [ + 'name', + 'number', + 'create_date:creationDate', + ('partner_id:partners', ['id', 'display_name', 'ref']) + ('line_id:lines', ['id', ('product_id', ['name']), 'price_unit']) + ] + +Also the module provide a method "get_json_parser" on the ir.exports object +that generate a parser from an ir.exports configuration diff --git a/base_jsonify/static/description/index.html b/base_jsonify/static/description/index.html new file mode 100644 index 000000000..a4065b092 --- /dev/null +++ b/base_jsonify/static/description/index.html @@ -0,0 +1,431 @@ + + + + + + +Base Jsonify + + + +
+

Base Jsonify

+ + +

Beta License: AGPL-3 OCA/server-tools Translate me on Weblate Try me on Runbot

+

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.

+

Example of parser:

+
+parser = [
+    'name',
+    'number',
+    'create_date',
+    ('partner_id', ['id', 'display_name', 'ref'])
+    ('line_id', ['id', ('product_id', ['name']), 'price_unit'])
+]
+
+

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:

+
+parser = [
+     'field_name:json_key'
+]
+
+
+parser = [
+    'name',
+    'number',
+    'create_date:creationDate',
+    ('partner_id:partners', ['id', 'display_name', 'ref'])
+    ('line_id:lines', ['id', ('product_id', ['name']), 'price_unit'])
+]
+
+

Also the module provide a method “get_json_parser” on the ir.exports object +that generate a parser from an ir.exports configuration

+

Table of contents

+ +
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Akretion
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/server-tools project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/base_jsonify/tests/__init__.py b/base_jsonify/tests/__init__.py new file mode 100644 index 000000000..bf58ee4e9 --- /dev/null +++ b/base_jsonify/tests/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- +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 new file mode 100644 index 000000000..2527d7be0 --- /dev/null +++ b/base_jsonify/tests/test_get_parser.py @@ -0,0 +1,112 @@ +# -*- coding: utf-8 -*- +# © +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.tests.common import TransactionCase + + +class TestParser(TransactionCase): + + def test_getting_parser(self): + expected_parser = [ + u'active', + (u'category_id', [u'name']), + (u'child_ids', [( + u'child_ids', [u'name']), + (u'country_id', [u'code', u'name']), + u'email', u'id', + u'name' + ]), + u'color', + u'comment', + (u'country_id', [u'code', u'name']), + u'credit_limit', + u'lang', + u'name'] + + exporter = self.env.ref('base_jsonify.ir_exp_partner') + parser = exporter.get_json_parser() + self.assertEqual(parser, expected_parser) + + # modify an ir.exports_line to put an alias for a field + self.env.ref('base_jsonify.category_id_name').write({ + 'alias': 'category_id:category/name' + }) + expected_parser[1] = (u'category_id:category', [u'name']) + parser = exporter.get_json_parser() + self.assertEqual(parser, expected_parser) + + def test_json_export(self): + parser = [ + u'lang', + u'comment', + u'credit_limit', + u'name', + u'color', + (u'child_ids:children', [ + (u'child_ids:children', [u'name']), + u'email', + (u'country_id:country', [u'code', u'name']), + u'name', + u'id', + ]), + (u'country_id:country', [u'code', u'name']), + u'active', + (u'category_id', [u'name']) + ] + 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': u'en_US', + u'comment': None, + u'credit_limit': 0.0, + u'name': u'Akretion', + u'color': 0, + u'country': { + u'code': u'FR', + u'name': u'France' + }, + u'active': True, + u'category_id': [ + {u'name': u'Inovator'} + ], + u'children': [{ + u'id': partner.child_ids.id, + u'country': { + u'code': u'FR', + u'name': u'France' + }, + u'children': [], + u'name': u'Sebatien Beau', + u'email': None + }] + } + json_partner = partner.jsonify(parser) + + 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) diff --git a/base_jsonify/tests/test_ir_exports_line.py b/base_jsonify/tests/test_ir_exports_line.py new file mode 100644 index 000000000..020f9b25c --- /dev/null +++ b/base_jsonify/tests/test_ir_exports_line.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +# © 2017 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.exceptions import ValidationError +from odoo.tests.common import TransactionCase + + +class TestIrExportsLine(TransactionCase): + + def setUp(self): + super(TestIrExportsLine, self).setUp() + self.ir_export = self.env.ref('base_jsonify.ir_exp_partner') + + def test_alias_contrains(self): + ir_export_lines_model = self.env['ir.exports.line'] + with self.assertRaises(ValidationError): + # The field into the name must be also into the alias + ir_export_lines_model.create({ + 'export_id': self.ir_export.id, + 'name': 'name', + 'alias': 'toto:my_alias' + }) + with self.assertRaises(ValidationError): + # The hierarchy into the alias must be the same as the one into + # the name + ir_export_lines_model.create({ + 'export_id': self.ir_export.id, + 'name': 'child_ids/child_ids/name', + 'alias': 'child_ids:children/name' + }) + with self.assertRaises(ValidationError): + # The hierarchy into the alias must be the same as the one into + # the name and must contains the same fields as into the name + ir_export_lines_model.create({ + 'export_id': self.ir_export.id, + 'name': 'child_ids/child_ids/name', + 'alias': 'child_ids:children/category_id:category/name' + }) + line = ir_export_lines_model.create({ + 'export_id': self.ir_export.id, + 'name': 'child_ids/child_ids/name', + 'alias': 'child_ids:children/child_ids:children/name' + }) + self.assertTrue(line) diff --git a/base_jsonify/views/ir_exports_view.xml b/base_jsonify/views/ir_exports_view.xml new file mode 100644 index 000000000..1762eff08 --- /dev/null +++ b/base_jsonify/views/ir_exports_view.xml @@ -0,0 +1,38 @@ + + + + + ir.exports + 50 + +
+ + + + + + + + + + + + + + + + +
+
+
+ + + Export Fields + ir.exports + tree,form + + + + +