Browse Source

base_jsonify: misc fixes on comments/docstrings

pull/1472/head
Pierrick brun 6 years ago
committed by Simone Orsi
parent
commit
d03284db97
  1. 10
      base_jsonify/__manifest__.py
  2. 18
      base_jsonify/models/ir_export.py
  3. 5
      base_jsonify/models/models.py
  4. 5
      base_jsonify/readme/DESCRIPTION.rst

10
base_jsonify/__manifest__.py

@ -1,23 +1,19 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# © 2017 Akretion (http://www.akretion.com)
# Copyright 2017-2018 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> # 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 models",
"version": "10.0.1.0.0", "version": "10.0.1.0.0",
"category": "Uncategorized", "category": "Uncategorized",
"website": "https://odoo-community.org/",
"website": "https://github.com/OCA/server-tools",
"author": "Akretion, Odoo Community Association (OCA)", "author": "Akretion, Odoo Community Association (OCA)",
"license": "AGPL-3", "license": "AGPL-3",
"application": False, "application": False,
"installable": True, "installable": True,
"external_dependencies": {
"python": [],
"bin": [],
},
"depends": [ "depends": [
"base", "base",
], ],

18
base_jsonify/models/ir_export.py

@ -8,6 +8,14 @@ from odoo import api, models
def update_dict(data, fields): def update_dict(data, fields):
"""
Contruct a tree of fields.
ie: {
"name": True,
"resource": True,
}
Order of keys is important.
"""
field = fields[0] field = fields[0]
if len(fields) == 1: if len(fields) == 1:
if field == '.id': if field == '.id':
@ -20,6 +28,11 @@ def update_dict(data, fields):
def convert_dict(dict_parser): 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 = [] parser = []
for field, value in dict_parser.iteritems(): for field, value in dict_parser.iteritems():
if value is True: if value is True:
@ -34,6 +47,10 @@ class IrExport(models.Model):
@api.multi @api.multi
def get_json_parser(self): 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() self.ensure_one()
dict_parser = OrderedDict() dict_parser = OrderedDict()
for line in self.export_fields: for line in self.export_fields:
@ -41,4 +58,5 @@ class IrExport(models.Model):
if line.alias: if line.alias:
names = line.alias.split('/') names = line.alias.split('/')
update_dict(dict_parser, names) update_dict(dict_parser, names)
return convert_dict(dict_parser) return convert_dict(dict_parser)

5
base_jsonify/models/models.py

@ -15,6 +15,9 @@ class Base(models.AbstractModel):
@api.model @api.model
def __parse_field(self, parser_field): def __parse_field(self, parser_field):
"""
Deducts how to handle a field from its parser
"""
field_name = parser_field field_name = parser_field
subparser = None subparser = None
if isinstance(parser_field, tuple): if isinstance(parser_field, tuple):
@ -36,7 +39,7 @@ class Base(models.AbstractModel):
('line_id', ['id', ('product_id', ['name']), 'price_unit']) ('line_id', ['id', ('product_id', ['name']), 'price_unit'])
] ]
In order to be consitent with the odoo api the jsonify method always
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 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 By default the key into the json is the name of the field extracted

5
base_jsonify/readme/DESCRIPTION.rst

@ -1,5 +1,6 @@
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.
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: Example of parser:

Loading…
Cancel
Save