Skip to content

Commit

Permalink
Merge pull request #1341 from OCA/14.0
Browse files Browse the repository at this point in the history
Syncing from upstream OCA/server-tools (14.0)
  • Loading branch information
bt-admin authored Nov 6, 2023
2 parents 4c8094a + 9f5af43 commit a953382
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ addon | version | maintainers | summary
[html_image_url_extractor](html_image_url_extractor/) | 14.0.1.0.1 | | Extract images found in any HTML field
[html_text](html_text/) | 14.0.1.0.1 | | Generate excerpts from any HTML field
[iap_alternative_provider](iap_alternative_provider/) | 14.0.1.0.0 | [![sebastienbeau](https://github.com/sebastienbeau.png?size=30px)](https://github.com/sebastienbeau) | Base module for providing alternative provider for iap apps
[jsonifier](jsonifier/) | 14.0.1.1.1 | | JSON-ify data for all models
[jsonifier](jsonifier/) | 14.0.1.2.0 | | JSON-ify data for all models
[jsonifier_stored](jsonifier_stored/) | 14.0.1.0.0 | [![simahawk](https://github.com/simahawk.png?size=30px)](https://github.com/simahawk) [![mmequignon](https://github.com/mmequignon.png?size=30px)](https://github.com/mmequignon) | Pre-compute and store JSON data on any model
[letsencrypt](letsencrypt/) | 14.0.1.0.2 | | Request SSL certificates from letsencrypt.org
[mail_cleanup](mail_cleanup/) | 14.0.1.0.0 | | Mark as read or delete mails after a set time
Expand Down
34 changes: 33 additions & 1 deletion jsonifier/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ JSONifier
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:472791d0286b61243e4a8cab99ce36b0e7e73a3ece42a4e10aac56306c0e128a
!! source digest: sha256:51951d9ec373383c021c251d42eef97c05a6b3fc9b67f5497a518d217298a9ab
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down Expand Up @@ -200,6 +200,38 @@ NOTE: this module was named `base_jsonify` till version 14.0.1.5.0.
.. contents::
:local:

Usage
=====

with_fieldname parameter
==========================

The with_fieldname option of jsonify() method, when true, will inject on
the same level of the data "_fieldname_$field" keys that will
contain the field name, in the language of the current user.


Examples of with_fieldname usage:

.. code-block:: python
# example 1
parser = [('name')]
a.jsonify(parser=parser)
[{'name': 'SO3996'}]
>>> a.jsonify(parser=parser, with_fieldname=False)
[{'name': 'SO3996'}]
>>> a.jsonify(parser=parser, with_fieldname=True)
[{'fieldname_name': 'Order Reference', 'name': 'SO3996'}}]
# example 2 - with a subparser-
parser=['name', 'create_date', ('order_line', ['id' , 'product_uom', 'is_expense'])]
>>> a.jsonify(parser=parser, with_fieldname=False)
[{'name': 'SO3996', 'create_date': '2015-06-02T12:18:26.279909+00:00', 'order_line': [{'id': 16649, 'product_uom': 'stuks', 'is_expense': False}, {'id': 16651, 'product_uom': 'stuks', 'is_expense': False}, {'id': 16650, 'product_uom': 'stuks', 'is_expense': False}]}]
>>> a.jsonify(parser=parser, with_fieldname=True)
[{'fieldname_name': 'Order Reference', 'name': 'SO3996', 'fieldname_create_date': 'Creation Date', 'create_date': '2015-06-02T12:18:26.279909+00:00', 'fieldname_order_line': 'Order Lines', 'order_line': [{'fieldname_id': 'ID', 'id': 16649, 'fieldname_product_uom': 'Unit of Measure', 'product_uom': 'stuks', 'fieldname_is_expense': 'Is expense', 'is_expense': False}]}]
Bug Tracker
===========

Expand Down
2 changes: 1 addition & 1 deletion jsonifier/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{
"name": "JSONifier",
"summary": "JSON-ify data for all models",
"version": "14.0.1.1.1",
"version": "14.0.1.2.0",
"category": "Uncategorized",
"website": "https://github.com/OCA/server-tools",
"author": "Akretion, ACSONE, Camptocamp, Odoo Community Association (OCA)",
Expand Down
15 changes: 13 additions & 2 deletions jsonifier/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ def _jsonify_record(self, parser, rec, root):
value, json_key = self._jsonify_record_handle_resolver(
rec, field, resolver, json_key
)
# whatever json value we have found in subparser or not ass a sister key
# on the same level _fieldname_{json_key}
if rec.env.context.get("with_fieldname"):
json_key_fieldname = "_fieldname_" + json_key
# check if we are in a subparser has already the fieldname sister keys
fieldname_value = rec._fields[field_dict["name"]].string
self._add_json_key(root, json_key_fieldname, fieldname_value)
self._add_json_key(root, json_key, value)
return root

Expand Down Expand Up @@ -152,10 +159,11 @@ def _jsonify_record_handle_subparser(self, rec, field_dict, strict, subparser):
{"model": self._name, "fname": field_name},
)
raise SwallableException()

value = [self._jsonify_record(subparser, r, {}) for r in rec[field_name]]

if field.type in ("many2one", "reference"):
value = value[0] if value else None

return value

def _jsonify_record_handle_resolver(self, rec, field, resolver, json_key):
Expand All @@ -168,7 +176,7 @@ def _jsonify_record_handle_resolver(self, rec, field, resolver, json_key):
value, json_key = value["_value"], value["_json_key"]
return value, json_key

def jsonify(self, parser, one=False):
def jsonify(self, parser, one=False, with_fieldname=False):
"""Convert the record according to the given parser.
Example of (simple) parser:
Expand Down Expand Up @@ -213,6 +221,9 @@ def jsonify(self, parser, one=False):
for lang in parsers:
translate = lang or parser.get("language_agnostic")
records = records.with_context(lang=lang) if translate else records
records = (
records.with_context(with_fieldname=True) if with_fieldname else records
)
for record, json in zip(records, results):
self._jsonify_record(parsers[lang], record, json)

Expand Down
28 changes: 28 additions & 0 deletions jsonifier/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
with_fieldname parameter
==========================

The with_fieldname option of jsonify() method, when true, will inject on
the same level of the data "_fieldname_$field" keys that will
contain the field name, in the language of the current user.


Examples of with_fieldname usage:

.. code-block:: python
# example 1
parser = [('name')]
a.jsonify(parser=parser)
[{'name': 'SO3996'}]
>>> a.jsonify(parser=parser, with_fieldname=False)
[{'name': 'SO3996'}]
>>> a.jsonify(parser=parser, with_fieldname=True)
[{'fieldname_name': 'Order Reference', 'name': 'SO3996'}}]
# example 2 - with a subparser-
parser=['name', 'create_date', ('order_line', ['id' , 'product_uom', 'is_expense'])]
>>> a.jsonify(parser=parser, with_fieldname=False)
[{'name': 'SO3996', 'create_date': '2015-06-02T12:18:26.279909+00:00', 'order_line': [{'id': 16649, 'product_uom': 'stuks', 'is_expense': False}, {'id': 16651, 'product_uom': 'stuks', 'is_expense': False}, {'id': 16650, 'product_uom': 'stuks', 'is_expense': False}]}]
>>> a.jsonify(parser=parser, with_fieldname=True)
[{'fieldname_name': 'Order Reference', 'name': 'SO3996', 'fieldname_create_date': 'Creation Date', 'create_date': '2015-06-02T12:18:26.279909+00:00', 'fieldname_order_line': 'Order Lines', 'order_line': [{'fieldname_id': 'ID', 'id': 16649, 'fieldname_product_uom': 'Unit of Measure', 'product_uom': 'stuks', 'fieldname_is_expense': 'Is expense', 'is_expense': False}]}]
53 changes: 42 additions & 11 deletions jsonifier/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ <h1 class="title">JSONifier</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:472791d0286b61243e4a8cab99ce36b0e7e73a3ece42a4e10aac56306c0e128a
!! source digest: sha256:51951d9ec373383c021c251d42eef97c05a6b3fc9b67f5497a518d217298a9ab
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/server-tools/tree/14.0/jsonifier"><img alt="OCA/server-tools" src="https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/server-tools-14-0/server-tools-14-0-jsonifier"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/server-tools&amp;target_branch=14.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module adds a ‘jsonify’ method to every model of the ORM.
Expand Down Expand Up @@ -503,35 +503,66 @@ <h1 class="title">JSONifier</h1>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#bug-tracker" id="toc-entry-1">Bug Tracker</a></li>
<li><a class="reference internal" href="#credits" id="toc-entry-2">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="toc-entry-3">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="toc-entry-4">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="toc-entry-5">Maintainers</a></li>
<li><a class="reference internal" href="#usage" id="toc-entry-1">Usage</a></li>
<li><a class="reference internal" href="#with-fieldname-parameter" id="toc-entry-2">with_fieldname parameter</a></li>
<li><a class="reference internal" href="#bug-tracker" id="toc-entry-3">Bug Tracker</a></li>
<li><a class="reference internal" href="#credits" id="toc-entry-4">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="toc-entry-5">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="toc-entry-6">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="toc-entry-7">Maintainers</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="usage">
<h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
</div>
<div class="section" id="with-fieldname-parameter">
<h1><a class="toc-backref" href="#toc-entry-2">with_fieldname parameter</a></h1>
<p>The with_fieldname option of jsonify() method, when true, will inject on
the same level of the data “_fieldname_$field” keys that will
contain the field name, in the language of the current user.</p>
<blockquote>
Examples of with_fieldname usage:</blockquote>
<pre class="code python literal-block">
<span class="c1"># example 1</span><span class="w">
</span><span class="n">parser</span> <span class="o">=</span> <span class="p">[(</span><span class="s1">'name'</span><span class="p">)]</span><span class="w">
</span><span class="n">a</span><span class="o">.</span><span class="n">jsonify</span><span class="p">(</span><span class="n">parser</span><span class="o">=</span><span class="n">parser</span><span class="p">)</span><span class="w">
</span><span class="p">[{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'SO3996'</span><span class="p">}]</span><span class="w">
</span><span class="o">&gt;&gt;&gt;</span> <span class="n">a</span><span class="o">.</span><span class="n">jsonify</span><span class="p">(</span><span class="n">parser</span><span class="o">=</span><span class="n">parser</span><span class="p">,</span> <span class="n">with_fieldname</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span><span class="w">
</span><span class="p">[{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'SO3996'</span><span class="p">}]</span><span class="w">
</span><span class="o">&gt;&gt;&gt;</span> <span class="n">a</span><span class="o">.</span><span class="n">jsonify</span><span class="p">(</span><span class="n">parser</span><span class="o">=</span><span class="n">parser</span><span class="p">,</span> <span class="n">with_fieldname</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span><span class="w">
</span><span class="p">[{</span><span class="s1">'fieldname_name'</span><span class="p">:</span> <span class="s1">'Order Reference'</span><span class="p">,</span> <span class="s1">'name'</span><span class="p">:</span> <span class="s1">'SO3996'</span><span class="p">}}]</span><span class="w">


</span><span class="c1"># example 2 - with a subparser-</span><span class="w">
</span><span class="n">parser</span><span class="o">=</span><span class="p">[</span><span class="s1">'name'</span><span class="p">,</span> <span class="s1">'create_date'</span><span class="p">,</span> <span class="p">(</span><span class="s1">'order_line'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'id'</span> <span class="p">,</span> <span class="s1">'product_uom'</span><span class="p">,</span> <span class="s1">'is_expense'</span><span class="p">])]</span><span class="w">
</span><span class="o">&gt;&gt;&gt;</span> <span class="n">a</span><span class="o">.</span><span class="n">jsonify</span><span class="p">(</span><span class="n">parser</span><span class="o">=</span><span class="n">parser</span><span class="p">,</span> <span class="n">with_fieldname</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span><span class="w">
</span><span class="p">[{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'SO3996'</span><span class="p">,</span> <span class="s1">'create_date'</span><span class="p">:</span> <span class="s1">'2015-06-02T12:18:26.279909+00:00'</span><span class="p">,</span> <span class="s1">'order_line'</span><span class="p">:</span> <span class="p">[{</span><span class="s1">'id'</span><span class="p">:</span> <span class="mi">16649</span><span class="p">,</span> <span class="s1">'product_uom'</span><span class="p">:</span> <span class="s1">'stuks'</span><span class="p">,</span> <span class="s1">'is_expense'</span><span class="p">:</span> <span class="kc">False</span><span class="p">},</span> <span class="p">{</span><span class="s1">'id'</span><span class="p">:</span> <span class="mi">16651</span><span class="p">,</span> <span class="s1">'product_uom'</span><span class="p">:</span> <span class="s1">'stuks'</span><span class="p">,</span> <span class="s1">'is_expense'</span><span class="p">:</span> <span class="kc">False</span><span class="p">},</span> <span class="p">{</span><span class="s1">'id'</span><span class="p">:</span> <span class="mi">16650</span><span class="p">,</span> <span class="s1">'product_uom'</span><span class="p">:</span> <span class="s1">'stuks'</span><span class="p">,</span> <span class="s1">'is_expense'</span><span class="p">:</span> <span class="kc">False</span><span class="p">}]}]</span><span class="w">
</span><span class="o">&gt;&gt;&gt;</span> <span class="n">a</span><span class="o">.</span><span class="n">jsonify</span><span class="p">(</span><span class="n">parser</span><span class="o">=</span><span class="n">parser</span><span class="p">,</span> <span class="n">with_fieldname</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span><span class="w">
</span><span class="p">[{</span><span class="s1">'fieldname_name'</span><span class="p">:</span> <span class="s1">'Order Reference'</span><span class="p">,</span> <span class="s1">'name'</span><span class="p">:</span> <span class="s1">'SO3996'</span><span class="p">,</span> <span class="s1">'fieldname_create_date'</span><span class="p">:</span> <span class="s1">'Creation Date'</span><span class="p">,</span> <span class="s1">'create_date'</span><span class="p">:</span> <span class="s1">'2015-06-02T12:18:26.279909+00:00'</span><span class="p">,</span> <span class="s1">'fieldname_order_line'</span><span class="p">:</span> <span class="s1">'Order Lines'</span><span class="p">,</span> <span class="s1">'order_line'</span><span class="p">:</span> <span class="p">[{</span><span class="s1">'fieldname_id'</span><span class="p">:</span> <span class="s1">'ID'</span><span class="p">,</span> <span class="s1">'id'</span><span class="p">:</span> <span class="mi">16649</span><span class="p">,</span> <span class="s1">'fieldname_product_uom'</span><span class="p">:</span> <span class="s1">'Unit of Measure'</span><span class="p">,</span> <span class="s1">'product_uom'</span><span class="p">:</span> <span class="s1">'stuks'</span><span class="p">,</span> <span class="s1">'fieldname_is_expense'</span><span class="p">:</span> <span class="s1">'Is expense'</span><span class="p">,</span> <span class="s1">'is_expense'</span><span class="p">:</span> <span class="kc">False</span><span class="p">}]}]</span>
</pre>
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#toc-entry-1">Bug Tracker</a></h1>
<h1><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/server-tools/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/server-tools/issues/new?body=module:%20jsonifier%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
<h1><a class="toc-backref" href="#toc-entry-2">Credits</a></h1>
<h1><a class="toc-backref" href="#toc-entry-4">Credits</a></h1>
<div class="section" id="authors">
<h2><a class="toc-backref" href="#toc-entry-3">Authors</a></h2>
<h2><a class="toc-backref" href="#toc-entry-5">Authors</a></h2>
<ul class="simple">
<li>Akretion</li>
<li>ACSONE</li>
<li>Camptocamp</li>
</ul>
</div>
<div class="section" id="contributors">
<h2><a class="toc-backref" href="#toc-entry-4">Contributors</a></h2>
<h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<ul class="simple">
<li>BEAU Sébastien &lt;<a class="reference external" href="mailto:sebastien.beau&#64;akretion.com">sebastien.beau&#64;akretion.com</a>&gt;</li>
<li>Raphaël Reverdy &lt;<a class="reference external" href="mailto:raphael.reverdy&#64;akretion.com">raphael.reverdy&#64;akretion.com</a>&gt;</li>
Expand All @@ -542,7 +573,7 @@ <h2><a class="toc-backref" href="#toc-entry-4">Contributors</a></h2>
</ul>
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-5">Maintainers</a></h2>
<h2><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
Expand Down
Loading

0 comments on commit a953382

Please sign in to comment.