From 810ba38fd9a779cd8937aa664ab1050d04410c1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20BEAU?= Date: Thu, 19 May 2016 08:57:22 +0200 Subject: [PATCH] add base_jsonify module --- base_jsonify/README.rst | 93 +++++++++++++++++++++++++++ base_jsonify/__init__.py | 7 ++ base_jsonify/__openerp__.py | 29 +++++++++ base_jsonify/demo/export_demo.xml | 11 ++++ base_jsonify/demo/ir.exports.line.csv | 16 +++++ base_jsonify/models/__init__.py | 8 +++ base_jsonify/models/ir_export.py | 40 ++++++++++++ base_jsonify/models/models.py | 49 ++++++++++++++ base_jsonify/tests/__init__.py | 6 ++ base_jsonify/tests/test_get_parser.py | 78 ++++++++++++++++++++++ 10 files changed, 337 insertions(+) create mode 100644 base_jsonify/README.rst create mode 100644 base_jsonify/__init__.py create mode 100644 base_jsonify/__openerp__.py create mode 100644 base_jsonify/demo/export_demo.xml create mode 100644 base_jsonify/demo/ir.exports.line.csv create mode 100644 base_jsonify/models/__init__.py create mode 100644 base_jsonify/models/ir_export.py create mode 100644 base_jsonify/models/models.py create mode 100644 base_jsonify/tests/__init__.py create mode 100644 base_jsonify/tests/test_get_parser.py diff --git a/base_jsonify/README.rst b/base_jsonify/README.rst new file mode 100644 index 000000000..dc0b04ad1 --- /dev/null +++ b/base_jsonify/README.rst @@ -0,0 +1,93 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :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. + +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 + +Also the module provide a method "get_json_parser" on the ir.exports object +that generate a parser from an ir.exports configuration + + +Installation +============ + +To install this module, you need to install it + +Configuration +============= + +No configuration required + +Usage +===== + +This is a technical module not function feature is added + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/{repo_id}/{branch} + +.. repo_id is available in https://github.com/OCA/maintainer-tools/blob/master/tools/repos_with_ids.txt +.. branch is "8.0" for example + +Known issues / Roadmap +====================== + +Nothing yet + +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. + +Credits +======= + +Images +------ + +* Odoo Community Association: `Icon `_. + +Contributors +------------ + +* BEAU Sébastien + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +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. + +To contribute to this module, please visit https://odoo-community.org. diff --git a/base_jsonify/__init__.py b/base_jsonify/__init__.py new file mode 100644 index 000000000..d34d08cb1 --- /dev/null +++ b/base_jsonify/__init__.py @@ -0,0 +1,7 @@ +# -*- 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/__openerp__.py new file mode 100644 index 000000000..ad47b797d --- /dev/null +++ b/base_jsonify/__openerp__.py @@ -0,0 +1,29 @@ +# -*- 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). + +{ + "name": "Base Jsonify", + "summary": "Base module that provide the jsonify method on all object", + "version": "8.0.1.0.0", + "category": "Uncategorized", + "website": "https://odoo-community.org/", + "author": "Akretion, Odoo Community Association (OCA)", + "license": "AGPL-3", + "application": False, + "installable": True, + "external_dependencies": { + "python": [], + "bin": [], + }, + "depends": [ + "base", + ], + "data": [ + ], + "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..4501b77d3 --- /dev/null +++ b/base_jsonify/demo/export_demo.xml @@ -0,0 +1,11 @@ + + + + + + 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..15dcb2f3d --- /dev/null +++ b/base_jsonify/models/__init__.py @@ -0,0 +1,8 @@ +# -*- 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 diff --git a/base_jsonify/models/ir_export.py b/base_jsonify/models/ir_export.py new file mode 100644 index 000000000..e1ac0ccc4 --- /dev/null +++ b/base_jsonify/models/ir_export.py @@ -0,0 +1,40 @@ +# -*- 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 openerp import api, models + + +def update_dict(data, fields): + field = fields[0] + if len(fields) == 1: + if field == '.id': + field = 'id' + data[field] = True + else: + if field not in data: + data[field] = {} + update_dict(data[field], fields[1:]) + + +def convert_dict(dict_parser): + 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): + self.ensure_one() + dict_parser = {} + for line in self.export_fields: + update_dict(dict_parser, line.name.split('/')) + return convert_dict(dict_parser) diff --git a/base_jsonify/models/models.py b/base_jsonify/models/models.py new file mode 100644 index 000000000..3c9834d0b --- /dev/null +++ b/base_jsonify/models/models.py @@ -0,0 +1,49 @@ +# -*- 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 openerp import api, models +from openerp.exceptions import Warning as UserError +from openerp.tools.translate import _ + + +@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 consitent with the odoo api the jsonify method always + return a list of object even if there is only one element in input + """ + result = [] + for rec in self: + res = {} + for field in parser: + if isinstance(field, tuple): + field_name, subparser = field + field_type = rec._fields[field_name].type + if field_type in ('one2many', 'many2many'): + res[field_name] = rec[field_name].jsonify(subparser) + elif field_type == 'many2one': + data = rec[field_name].jsonify(subparser) + if data: + res[field_name] = data[0] + else: + res[field_name] = None + else: + raise UserError(_('Wrong parser configuration')) + else: + res[field] = rec[field] + result.append(res) + return result + + +models.Model.jsonify = jsonify diff --git a/base_jsonify/tests/__init__.py b/base_jsonify/tests/__init__.py new file mode 100644 index 000000000..8ed730c5e --- /dev/null +++ b/base_jsonify/tests/__init__.py @@ -0,0 +1,6 @@ +# -*- 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 diff --git a/base_jsonify/tests/test_get_parser.py b/base_jsonify/tests/test_get_parser.py new file mode 100644 index 000000000..a99eb6e7a --- /dev/null +++ b/base_jsonify/tests/test_get_parser.py @@ -0,0 +1,78 @@ +# -*- coding: utf-8 -*- +# © +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp.tests.common import TransactionCase + + +class TestParser(TransactionCase): + + def setUp(self): + super(TestParser, self).setUp() + self.expected_parser = [ + u'lang', + u'comment', + u'credit_limit', + u'name', + u'color', + (u'child_ids', [ + (u'child_ids', [u'name']), + u'email', + (u'country_id', [u'code', u'name']), + u'name', + u'id', + ]), + (u'country_id', [u'code', u'name']), + u'active', + (u'category_id', [u'name']) + ] + + # TODO adapt data for 8.0 + def fixme_test_getting_parser(self): + exporter = self.env.ref('base_jsonify.ir_exp_partner') + parser = exporter.get_json_parser() + self.assertEqual(parser, self.expected_parser) + + def fixme_test_json_export(self): + expected_json = [{ + u'lang': False, + u'comment': False, + u'credit_limit': 0.0, + u'name': u'Camptocamp', + u'color': 0, + u'country_id': {u'code': u'FR', u'name': u'France'}, + u'child_ids': [{ + u'id': 29, + u'country_id': { + u'code': u'FR', + u'name': u'France' + }, + u'child_ids': [], + u'email': u'ayaan.agarwal@bestdesigners.example.com', + u'name': u'Ayaan Agarwal' + }, { + u'id': 35, + u'country_id': { + u'code': u'FR', + u'name': u'France'}, + u'child_ids': [], + u'email': u'benjamin.flores@nebula.example.com', + u'name': u'Benjamin Flores' + }, { + u'id': 28, + u'country_id': { + u'code': u'FR', + u'name': u'France'}, + u'child_ids': [], + u'email': u'phillipp.miller@mediapole.example.com', + u'name': u'Phillipp Miller' + }], + u'active': True, + u'category_id': [ + {u'name': u'Gold'}, + {u'name': u'Services'} + ] + }] + partner = self.env.ref('base.res_partner_12') + json_partner = partner.jsonify(self.expected_parser) + self.assertEqual(json_partner, expected_json)