Browse Source
base_jsonify: cleanups
base_jsonify: cleanups
remove copyrights from init and update copyrightspull/1472/head
hpar
7 years ago
committed by
Simone Orsi
11 changed files with 91 additions and 101 deletions
-
7base_jsonify/README.rst
-
5base_jsonify/__init__.py
-
5base_jsonify/__manifest__.py
-
16base_jsonify/demo/export_demo.xml
-
5base_jsonify/models/__init__.py
-
4base_jsonify/models/ir_export.py
-
4base_jsonify/models/ir_exports_line.py
-
126base_jsonify/models/models.py
-
4base_jsonify/tests/__init__.py
-
12base_jsonify/tests/test_get_parser.py
-
4base_jsonify/tests/test_ir_exports_line.py
@ -1,7 +1,2 @@ |
|||||
# -*- coding: utf-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 models |
@ -1,12 +1,13 @@ |
|||||
# -*- coding: utf-8 -*- |
# -*- coding: utf-8 -*- |
||||
# © 2016 Akretion (http://www.akretion.com) |
|
||||
|
# © 2017 Akretion (http://www.akretion.com) |
||||
# Sébastien BEAU <sebastien.beau@akretion.com> |
# Sébastien BEAU <sebastien.beau@akretion.com> |
||||
|
# Raphaël Reverdy <raphael.reverdy@akretion.com> |
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
||||
|
|
||||
{ |
{ |
||||
"name": "Base Jsonify", |
"name": "Base Jsonify", |
||||
"summary": "Base module that provide the jsonify method on all object", |
"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", |
"category": "Uncategorized", |
||||
"website": "https://odoo-community.org/", |
"website": "https://odoo-community.org/", |
||||
"author": "Akretion, Odoo Community Association (OCA)", |
"author": "Akretion, Odoo Community Association (OCA)", |
@ -1,11 +1,7 @@ |
|||||
<?xml version="1.0" encoding="UTF-8"?> |
<?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> |
|
||||
|
<odoo> |
||||
|
<record id="ir_exp_partner" model="ir.exports"> |
||||
|
<field name="name">Partner Export</field> |
||||
|
<field name="resource">res.partner</field> |
||||
|
</record> |
||||
|
</odoo> |
@ -1,9 +1,4 @@ |
|||||
# -*- coding: utf-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 models |
||||
from . import ir_export |
from . import ir_export |
||||
from . import ir_exports_line |
from . import ir_exports_line |
@ -1,76 +1,80 @@ |
|||||
# -*- coding: utf-8 -*- |
# -*- coding: utf-8 -*- |
||||
# © 2016 Akretion (http://www.akretion.com) |
|
||||
|
# © 2017 Akretion (http://www.akretion.com) |
||||
# Sébastien BEAU <sebastien.beau@akretion.com> |
# Sébastien BEAU <sebastien.beau@akretion.com> |
||||
|
# Raphaël Reverdy <raphael.reverdy@akretion.com> |
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
# 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 |
@ -1,7 +1,3 @@ |
|||||
# -*- coding: utf-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 test_get_parser |
from . import test_get_parser |
||||
from . import test_ir_exports_line |
from . import test_ir_exports_line |
Write
Preview
Loading…
Cancel
Save
Reference in new issue