diff --git a/base_jsonify/README.rst b/base_jsonify/README.rst new file mode 100644 index 000000000..7d5542ec6 --- /dev/null +++ b/base_jsonify/README.rst @@ -0,0 +1,117 @@ +============ +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 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 + +**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..0650744f6 --- /dev/null +++ b/base_jsonify/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/base_jsonify/__manifest__.py b/base_jsonify/__manifest__.py new file mode 100644 index 000000000..02047eb9a --- /dev/null +++ b/base_jsonify/__manifest__.py @@ -0,0 +1,25 @@ +# 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": "12.0.1.0.0", + "category": "Uncategorized", + "website": "https://github.com/OCA/server-tools", + "author": "Akretion, Odoo Community Association (OCA)", + "license": "AGPL-3", + "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/i18n/base_jsonify.pot b/base_jsonify/i18n/base_jsonify.pot new file mode 100644 index 000000000..84777e05b --- /dev/null +++ b/base_jsonify/i18n/base_jsonify.pot @@ -0,0 +1,74 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * base_jsonify +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 10.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: base_jsonify +#: model:ir.model.fields,field_description:base_jsonify.field_ir_exports_line_alias +msgid "Alias" +msgstr "" + +#. module: base_jsonify +#: model:ir.ui.view,arch_db:base_jsonify.view_ir_exports +msgid "Configuration" +msgstr "" + +#. module: base_jsonify +#: model:ir.actions.act_window,name:base_jsonify.act_ui_exports_view +#: model:ir.ui.menu,name:base_jsonify.ui_exports +msgid "Export Fields" +msgstr "" + +#. module: base_jsonify +#: model:ir.ui.view,arch_db:base_jsonify.view_ir_exports +msgid "Index" +msgstr "" + +#. module: base_jsonify +#: code:addons/base_jsonify/models/ir_exports_line.py:28 +#, python-format +msgid "Name and Alias must have the same hierarchy depth" +msgstr "" + +#. module: base_jsonify +#: code:addons/base_jsonify/models/ir_exports_line.py:33 +#, python-format +msgid "The alias must reference the same field as in name '%s' not in '%s'" +msgstr "" + +#. module: base_jsonify +#: model:ir.model.fields,help:base_jsonify.field_ir_exports_line_alias +msgid "The complete path to the field where you can specify an alias on the a step as field:alias" +msgstr "" + +#. module: base_jsonify +#: code:addons/base_jsonify/models/models.py:71 +#, python-format +msgid "Wrong parser configuration" +msgstr "" + +#. module: base_jsonify +#: model:ir.model,name:base_jsonify.model_base +msgid "base" +msgstr "" + +#. module: base_jsonify +#: model:ir.model,name:base_jsonify.model_ir_exports +msgid "ir.exports" +msgstr "" + +#. module: base_jsonify +#: model:ir.model,name:base_jsonify.model_ir_exports_line +msgid "ir.exports.line" +msgstr "" + diff --git a/base_jsonify/models/__init__.py b/base_jsonify/models/__init__.py new file mode 100644 index 000000000..f02a8724e --- /dev/null +++ b/base_jsonify/models/__init__.py @@ -0,0 +1,3 @@ +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..e14db51bd --- /dev/null +++ b/base_jsonify/models/ir_export.py @@ -0,0 +1,63 @@ +# © 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. + + Example: + + { + "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): + """Convert dict returned by update_dict to list consistent w/ Odoo API. + + The list is composed of strings (field names or aliases) or tuples. + """ + parser = [] + for field, value in dict_parser.items(): + 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 ir.exports record and return it. + + The final parser can be used to "jsonify" records of 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..ddbb41d74 --- /dev/null +++ b/base_jsonify/models/ir_exports_line.py @@ -0,0 +1,34 @@ +# 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..f05513044 --- /dev/null +++ b/base_jsonify/models/models.py @@ -0,0 +1,76 @@ +# © 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): + """Deduct 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 given parser. + + 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..115e75c0d --- /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..7efb46853 --- /dev/null +++ b/base_jsonify/static/description/index.html @@ -0,0 +1,454 @@ + + + + + + +Base Jsonify + + + +
+

Base Jsonify

+ + +

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

+

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:

+
+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..b7966f071 --- /dev/null +++ b/base_jsonify/tests/__init__.py @@ -0,0 +1,2 @@ +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..6e421141d --- /dev/null +++ b/base_jsonify/tests/test_get_parser.py @@ -0,0 +1,111 @@ +# Copyright 2017 ACSONE SA/NV +# 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 = [ + 'active', + ('category_id', ['name']), + ('child_ids', [( + 'child_ids', ['name']), + ('country_id', ['code', 'name']), + 'email', 'id', + 'name' + ]), + 'color', + 'comment', + ('country_id', ['code', 'name']), + 'credit_limit', + 'lang', + '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] = ('category_id:category', ['name']) + parser = exporter.get_json_parser() + self.assertEqual(parser, expected_parser) + + def test_json_export(self): + parser = [ + 'lang', + 'comment', + 'credit_limit', + 'name', + 'color', + ('child_ids:children', [ + ('child_ids:children', ['name']), + 'email', + ('country_id:country', ['code', 'name']), + 'name', + 'id', + ]), + ('country_id:country', ['code', 'name']), + 'active', + ('category_id', ['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 = { + 'lang': 'en_US', + 'comment': None, + 'credit_limit': 0.0, + 'name': 'Akretion', + 'color': 0, + 'country': { + 'code': 'FR', + 'name': 'France' + }, + 'active': True, + 'category_id': [ + {'name': 'Inovator'} + ], + 'children': [{ + 'id': partner.child_ids.id, + 'country': { + 'code': 'FR', + 'name': 'France' + }, + 'children': [], + 'name': 'Sebatien Beau', + '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..2916f8487 --- /dev/null +++ b/base_jsonify/tests/test_ir_exports_line.py @@ -0,0 +1,44 @@ +# Copyright 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 + + + + +