Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.0] base_jsonify: Backport to 11.0 #1

Open
wants to merge 2 commits into
base: 11.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions base_jsonify/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
============
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/12.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-12-0/server-tools-12-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/12.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 <https://github.com/OCA/server-tools/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 <https://github.com/OCA/server-tools/issues/new?body=module:%20base_jsonify%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Akretion

Contributors
~~~~~~~~~~~~

* BEAU Sébastien <[email protected]>
* Raphaël Reverdy <[email protected]>
* Laurent Mignon <[email protected]>
* Aritz Olea <[email protected]>

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 <https://github.com/OCA/server-tools/tree/12.0/base_jsonify>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions base_jsonify/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
26 changes: 26 additions & 0 deletions base_jsonify/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2017-2018 Akretion (http://www.akretion.com)
# Sébastien BEAU <[email protected]>
# Raphaël Reverdy <[email protected]>
# © 2024 FactorLibre - Aritz Olea <[email protected]>
# 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": "11.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',
],
}
7 changes: 7 additions & 0 deletions base_jsonify/demo/export_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="ir_exp_partner" model="ir.exports">
<field name="name">Partner Export</field>
<field name="resource">res.partner</field>
</record>
</odoo>
16 changes: 16 additions & 0 deletions base_jsonify/demo/ir.exports.line.csv
Original file line number Diff line number Diff line change
@@ -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
76 changes: 76 additions & 0 deletions base_jsonify/i18n/base_jsonify.pot
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * base_jsonify
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 11.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-03-18 10:14+0000\n"
"PO-Revision-Date: 2024-03-18 10:14+0000\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:26
#, 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:31
#, 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:69
#, 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 ""

77 changes: 77 additions & 0 deletions base_jsonify/i18n/es.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * base_jsonify
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 11.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-03-18 10:12+0000\n"
"PO-Revision-Date: 2024-03-18 11:13+0100\n"
"Last-Translator: <>\n"
"Language-Team: \n"
"Language: es\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: \n"
"X-Generator: Poedit 3.0.1\n"

#. module: base_jsonify
#: model:ir.model.fields,field_description:base_jsonify.field_ir_exports_line_alias
msgid "Alias"
msgstr "Alias"

#. module: base_jsonify
#: model:ir.ui.view,arch_db:base_jsonify.view_ir_exports
msgid "Configuration"
msgstr "Configuración"

#. 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 "Exportar Campos"

#. module: base_jsonify
#: model:ir.ui.view,arch_db:base_jsonify.view_ir_exports
msgid "Index"
msgstr "Índice"

#. module: base_jsonify
#: code:addons/base_jsonify/models/ir_exports_line.py:26
#, python-format
msgid "Name and Alias must have the same hierarchy depth"
msgstr "Los campos nombre y alias deben tener la misma profuncidad en la jerarquía"

#. module: base_jsonify
#: code:addons/base_jsonify/models/ir_exports_line.py:31
#, python-format
msgid "The alias must reference the same field as in name '%s' not in '%s'"
msgstr "El alias debe referencias el mismo campo como en '%s', no en '%s'"

#. 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 "La ruta completa al campo donde se puede especificar un seudónimo en el paso como field:alias"

#. module: base_jsonify
#: code:addons/base_jsonify/models/models.py:69
#, python-format
msgid "Wrong parser configuration"
msgstr "Configuración incorrecta del analizador sintáctico"

#. module: base_jsonify
#: model:ir.model,name:base_jsonify.model_base
msgid "base"
msgstr "base"

#. module: base_jsonify
#: model:ir.model,name:base_jsonify.model_ir_exports
msgid "ir.exports"
msgstr "ir.exports"

#. module: base_jsonify
#: model:ir.model,name:base_jsonify.model_ir_exports_line
msgid "ir.exports.line"
msgstr "ir.exports.line"
3 changes: 3 additions & 0 deletions base_jsonify/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import models
from . import ir_export
from . import ir_exports_line
63 changes: 63 additions & 0 deletions base_jsonify/models/ir_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# © 2017 Akretion (http://www.akretion.com)
# Sébastien BEAU <[email protected]>
# 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)
Loading
Loading