============
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-LGPL--3-blue.png
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github
:target: https://github.com/OCA/server-tools/tree/14.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-14-0/server-tools-14-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/14.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 a simple 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 consistent with the Odoo API the jsonify method always
returns a list of objects even if there is only one element in the recordset.
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'])
]
If you need to parse the value of a field in a custom way,
you can pass a callable or the name of a method on the model:
.. code-block:: python
parser = [
('name', "jsonify_name") # method name
('number', lambda rec, field_name: rec[field_name] * 2)) # callable
]
Also the module provide a method "get_json_parser" on the ir.exports object
that generate a parser from an ir.exports configuration.
Further features are available for advanced uses.
It defines a simple "resolver" model that has a "python_code" field and a resolve
function so that arbitrary functions can be configured to transform fields,
or process the resulting dictionary.
It is also to specify a lang to extract the translation of any given field.
To use these features, a full parser follows the following structure:
.. code-block:: python
parser = {
"resolver": 3,
"language_agnostic": True,
"langs": {
False: [
{'name': 'description'},
{'name': 'number', 'resolver': 5},
({'name': 'partner_id', 'target': 'partner'}, [{'name': 'display_name'}])
],
'fr_FR': [
{'name': 'description', 'target': 'descriptions_fr'},
({'name': 'partner_id', 'target': 'partner'}, [{'name': 'description', 'target': 'description_fr'}])
],
}
}
One would get a result having this structure (note that the translated fields are merged in the same dictionary):
.. code-block:: python
exported_json == {
"description": "English description",
"description_fr": "French description, voilà",
"number": 42,
"partner": {
"display_name": "partner name",
"description_fr": "French description of that partner",
},
}
Note that a resolver can be passed either as a recordset or as an id, so as to be fully serializable.
A slightly simpler version in case the translation of fields is not needed,
but other features like custom resolvers are:
.. code-block:: python
parser = {
"resolver": 3,
"fields": [
{'name': 'description'},
{'name': 'number', 'resolver': 5},
({'name': 'partner_id', 'target': 'partners'}, [{'name': 'display_name'}]),
],
}
By passing the `fields` key instead of `langs`, we have essentially the same behaviour as simple parsers,
with the added benefit of being able to use resolvers.
Standard use-cases of resolvers are:
- give field-specific defaults (e.g. `""` instead of `None`)
- cast a field type (e.g. `int()`)
- alias a particular field for a specific export
- ...
A simple parser is simply translated into a full parser at export.
If the global resolver is given, then the json_dict goes through:
.. code-block:: python
resolver.resolve(dict, record)
Which allows to add external data from the context or transform the dictionary
if necessary. Similarly if given for a field the resolver evaluates the result.
It is possible for a target to have a marshaller by ending the target with '=list':
in that case the result is put into a list.
.. code-block:: python
parser = {
fields: [
{'name': 'name'},
{'name': 'field_1', 'target': 'customTags=list'},
{'name': 'field_2', 'target': 'customTags=list'},
]
}
Would result in the following JSON structure:
.. code-block:: python
{
'name': 'record_name',
'customTags': ['field_1_value', 'field_2_value'],
}
The intended use-case is to be compatible with APIs that require all translated
parameters to be exported simultaneously, and ask for custom properties to be
put in a sub-dictionary.
Since it is often the case that some of these requirements are optional,
new requirements could be met without needing to add field or change any code.
Note that the export values with the simple parser depends on the record's lang;
this is in contrast with full parsers which are designed to be language agnostic.
**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:%2014.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 <sebastien.beau@akretion.com>
* Raphaël Reverdy <raphael.reverdy@akretion.com>
* Laurent Mignon <laurent.mignon@acsone.eu>
* Nans Lefebvre <nans.lefebvre@acsone.eu>
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/14.0/base_jsonify>`_ project on GitHub.
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
Raw data
{
"_id": null,
"home_page": "https://github.com/OCA/server-tools",
"name": "odoo14-addon-base-jsonify",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "",
"author": "Akretion, Odoo Community Association (OCA)",
"author_email": "support@odoo-community.org",
"download_url": "",
"platform": "",
"description": "============\nBase JSONify\n============\n\n.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n !! This file is generated by oca-gen-addon-readme !!\n !! changes will be overwritten. !!\n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png\n :target: https://odoo-community.org/page/development-status\n :alt: Beta\n.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png\n :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html\n :alt: License: LGPL-3\n.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github\n :target: https://github.com/OCA/server-tools/tree/14.0/base_jsonify\n :alt: OCA/server-tools\n.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png\n :target: https://translation.odoo-community.org/projects/server-tools-14-0/server-tools-14-0-base_jsonify\n :alt: Translate me on Weblate\n.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png\n :target: https://runbot.odoo-community.org/runbot/149/14.0\n :alt: Try me on Runbot\n\n|badge1| |badge2| |badge3| |badge4| |badge5| \n\nThis module adds a 'jsonify' method to every model of the ORM.\nIt works on the current recordset and requires a single argument 'parser'\nthat specify the field to extract.\n\nExample of a simple parser:\n\n\n.. code-block:: python\n\n parser = [\n 'name',\n 'number',\n 'create_date',\n ('partner_id', ['id', 'display_name', 'ref'])\n ('line_id', ['id', ('product_id', ['name']), 'price_unit'])\n ]\n\nIn order to be consistent with the Odoo API the jsonify method always\nreturns a list of objects even if there is only one element in the recordset.\n\nBy default the key into the JSON is the name of the field extracted\nfrom the model. If you need to specify an alternate name to use as key, you\ncan define your mapping as follow into the parser definition:\n\n.. code-block:: python\n\n parser = [\n 'field_name:json_key'\n ]\n\n.. code-block:: python\n\n\n parser = [\n 'name',\n 'number',\n 'create_date:creationDate',\n ('partner_id:partners', ['id', 'display_name', 'ref'])\n ('line_id:lines', ['id', ('product_id', ['name']), 'price_unit'])\n ]\n\nIf you need to parse the value of a field in a custom way,\nyou can pass a callable or the name of a method on the model:\n\n.. code-block:: python\n\n parser = [\n ('name', \"jsonify_name\") # method name\n ('number', lambda rec, field_name: rec[field_name] * 2)) # callable\n ]\n\nAlso the module provide a method \"get_json_parser\" on the ir.exports object\nthat generate a parser from an ir.exports configuration.\n\nFurther features are available for advanced uses.\nIt defines a simple \"resolver\" model that has a \"python_code\" field and a resolve\nfunction so that arbitrary functions can be configured to transform fields,\nor process the resulting dictionary.\nIt is also to specify a lang to extract the translation of any given field.\n\nTo use these features, a full parser follows the following structure:\n\n.. code-block:: python\n\n parser = {\n \"resolver\": 3,\n \"language_agnostic\": True,\n \"langs\": {\n False: [\n {'name': 'description'},\n {'name': 'number', 'resolver': 5},\n ({'name': 'partner_id', 'target': 'partner'}, [{'name': 'display_name'}])\n ],\n 'fr_FR': [\n {'name': 'description', 'target': 'descriptions_fr'},\n ({'name': 'partner_id', 'target': 'partner'}, [{'name': 'description', 'target': 'description_fr'}])\n ],\n }\n }\n\n\nOne would get a result having this structure (note that the translated fields are merged in the same dictionary):\n\n.. code-block:: python\n\n exported_json == {\n \"description\": \"English description\",\n \"description_fr\": \"French description, voil\u00e0\",\n \"number\": 42,\n \"partner\": {\n \"display_name\": \"partner name\",\n \"description_fr\": \"French description of that partner\",\n },\n }\n\n\nNote that a resolver can be passed either as a recordset or as an id, so as to be fully serializable.\nA slightly simpler version in case the translation of fields is not needed,\nbut other features like custom resolvers are:\n\n.. code-block:: python\n\n parser = {\n \"resolver\": 3,\n \"fields\": [\n {'name': 'description'},\n {'name': 'number', 'resolver': 5},\n ({'name': 'partner_id', 'target': 'partners'}, [{'name': 'display_name'}]),\n ],\n }\n\n\nBy passing the `fields` key instead of `langs`, we have essentially the same behaviour as simple parsers,\nwith the added benefit of being able to use resolvers.\n\nStandard use-cases of resolvers are:\n- give field-specific defaults (e.g. `\"\"` instead of `None`)\n- cast a field type (e.g. `int()`)\n- alias a particular field for a specific export\n- ...\n\nA simple parser is simply translated into a full parser at export.\n\nIf the global resolver is given, then the json_dict goes through:\n\n.. code-block:: python\n\n resolver.resolve(dict, record)\n\nWhich allows to add external data from the context or transform the dictionary\nif necessary. Similarly if given for a field the resolver evaluates the result.\n\nIt is possible for a target to have a marshaller by ending the target with '=list':\nin that case the result is put into a list.\n\n.. code-block:: python\n\n parser = {\n fields: [\n {'name': 'name'},\n {'name': 'field_1', 'target': 'customTags=list'},\n {'name': 'field_2', 'target': 'customTags=list'},\n ]\n }\n\n\nWould result in the following JSON structure:\n\n.. code-block:: python\n\n {\n 'name': 'record_name',\n 'customTags': ['field_1_value', 'field_2_value'],\n }\n\nThe intended use-case is to be compatible with APIs that require all translated\nparameters to be exported simultaneously, and ask for custom properties to be\nput in a sub-dictionary.\nSince it is often the case that some of these requirements are optional,\nnew requirements could be met without needing to add field or change any code.\n\nNote that the export values with the simple parser depends on the record's lang;\nthis is in contrast with full parsers which are designed to be language agnostic.\n\n**Table of contents**\n\n.. contents::\n :local:\n\nBug Tracker\n===========\n\nBugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_.\nIn case of trouble, please check there if your issue has already been reported.\nIf you spotted it first, help us smashing it by providing a detailed and welcomed\n`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20base_jsonify%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.\n\nDo not contact contributors directly about support or help with technical issues.\n\nCredits\n=======\n\nAuthors\n~~~~~~~\n\n* Akretion\n\nContributors\n~~~~~~~~~~~~\n\n* BEAU S\u00e9bastien <sebastien.beau@akretion.com>\n* Rapha\u00ebl Reverdy <raphael.reverdy@akretion.com>\n* Laurent Mignon <laurent.mignon@acsone.eu>\n* Nans Lefebvre <nans.lefebvre@acsone.eu>\n\nMaintainers\n~~~~~~~~~~~\n\nThis module is maintained by the OCA.\n\n.. image:: https://odoo-community.org/logo.png\n :alt: Odoo Community Association\n :target: https://odoo-community.org\n\nOCA, or the Odoo Community Association, is a nonprofit organization whose\nmission is to support the collaborative development of Odoo features and\npromote its widespread use.\n\nThis module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/14.0/base_jsonify>`_ project on GitHub.\n\nYou are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.\n\n\n",
"bugtrack_url": null,
"license": "LGPL-3",
"summary": "Base module that provide the jsonify method on all models",
"version": "14.0.1.5.0",
"project_urls": {
"Homepage": "https://github.com/OCA/server-tools"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4360c276261b7ff0f36601693cb312aef936e0e3cf2bc8e4186fb0af3ebed084",
"md5": "9ea62c90d971ac5eef3be99656190b29",
"sha256": "3d1a7c9d23b763c02e34a2e157cd2a1e2570ede2ba316fa0f8c2c94edb68948f"
},
"downloads": -1,
"filename": "odoo14_addon_base_jsonify-14.0.1.5.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9ea62c90d971ac5eef3be99656190b29",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 45254,
"upload_time": "2022-01-25T20:08:15",
"upload_time_iso_8601": "2022-01-25T20:08:15.884857Z",
"url": "https://files.pythonhosted.org/packages/43/60/c276261b7ff0f36601693cb312aef936e0e3cf2bc8e4186fb0af3ebed084/odoo14_addon_base_jsonify-14.0.1.5.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-01-25 20:08:15",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "OCA",
"github_project": "server-tools",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "acme",
"specs": []
},
{
"name": "astor",
"specs": []
},
{
"name": "cryptography",
"specs": []
},
{
"name": "dataclasses",
"specs": []
},
{
"name": "dnspython",
"specs": []
},
{
"name": "josepy",
"specs": []
},
{
"name": "mako",
"specs": []
},
{
"name": "odoo_test_helper",
"specs": []
},
{
"name": "odoorpc",
"specs": []
},
{
"name": "openpyxl",
"specs": []
},
{
"name": "openupgradelib",
"specs": []
},
{
"name": "pygount",
"specs": []
},
{
"name": "pysftp",
"specs": []
},
{
"name": "sentry_sdk",
"specs": [
[
"<=",
"1.9.0"
]
]
},
{
"name": "unidecode",
"specs": []
}
],
"lcname": "odoo14-addon-base-jsonify"
}