add base_jsonify module
parent
01fc5a323e
commit
9af356504f
|
@ -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
|
||||||
|
<https://github.com/OCA/{project_repo}/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 <https://github.com/OCA/maintainer-tools/blob/master/template/module/static/description/icon.svg>`_.
|
||||||
|
|
||||||
|
Contributors
|
||||||
|
------------
|
||||||
|
|
||||||
|
* BEAU Sébastien <sebastien.beau@akretion.com>
|
||||||
|
|
||||||
|
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.
|
|
@ -0,0 +1,7 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# © 2016 Akretion (http://www.akretion.com)
|
||||||
|
# Sébastien BEAU <sebastien.beau@akretion.com>
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||||
|
|
||||||
|
|
||||||
|
from . import models
|
|
@ -0,0 +1,29 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# © 2016 Akretion (http://www.akretion.com)
|
||||||
|
# Sébastien BEAU <sebastien.beau@akretion.com>
|
||||||
|
# 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',
|
||||||
|
],
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<openerp>
|
||||||
|
<data>
|
||||||
|
|
||||||
|
<record id="ir_exp_partner" model="ir.exports">
|
||||||
|
<field name="name">Partner Export</field>
|
||||||
|
<field name="resource">res.partner</field>
|
||||||
|
</record>
|
||||||
|
|
||||||
|
</data>
|
||||||
|
</openerp>
|
|
@ -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
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# © 2016 Akretion (http://www.akretion.com)
|
||||||
|
# Sébastien BEAU <sebastien.beau@akretion.com>
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||||
|
|
||||||
|
|
||||||
|
from . import models
|
||||||
|
from . import ir_export
|
|
@ -0,0 +1,40 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# © 2016 Akretion (http://www.akretion.com)
|
||||||
|
# Sébastien BEAU <sebastien.beau@akretion.com>
|
||||||
|
# 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)
|
|
@ -0,0 +1,49 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# © 2016 Akretion (http://www.akretion.com)
|
||||||
|
# Sébastien BEAU <sebastien.beau@akretion.com>
|
||||||
|
# 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
|
|
@ -0,0 +1,6 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# © 2016 Akretion (http://www.akretion.com)
|
||||||
|
# Sébastien BEAU <sebastien.beau@akretion.com>
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||||
|
|
||||||
|
from . import test_get_parser
|
|
@ -0,0 +1,78 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# © <YEAR(S)> <AUTHOR(S)>
|
||||||
|
# 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)
|
Loading…
Reference in New Issue