diff --git a/edi_exchange_template_oca_json/README.rst b/edi_exchange_template_oca_json/README.rst new file mode 100644 index 000000000..13023558b --- /dev/null +++ b/edi_exchange_template_oca_json/README.rst @@ -0,0 +1,76 @@ +========================================== +EDI Exchange Template - Add JSON generator +========================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:81ee8002397b75724a8d89327a5ca3eb2eaa0a717e2ad37894751baff75a3425 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fedi--framework-lightgray.png?logo=github + :target: https://github.com/OCA/edi-framework/tree/18.0/edi_exchange_template_oca_json + :alt: OCA/edi-framework +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/edi-framework-18-0/edi-framework-18-0-edi_exchange_template_oca_json + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/edi-framework&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Provide extra JSON generator type for EDI exchange templates. + +**Table of contents** + +.. contents:: + :local: + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +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 +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Camptocamp + +Contributors +------------ + +- Italo Lopes + +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/edi-framework `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/edi_exchange_template_oca_json/__init__.py b/edi_exchange_template_oca_json/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/edi_exchange_template_oca_json/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/edi_exchange_template_oca_json/__manifest__.py b/edi_exchange_template_oca_json/__manifest__.py new file mode 100644 index 000000000..5ac97a453 --- /dev/null +++ b/edi_exchange_template_oca_json/__manifest__.py @@ -0,0 +1,14 @@ +# Copyright 2025 Camptocamp SA +# @author Italo LOPES +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). +{ + "name": "EDI Exchange Template - Add JSON generator", + "summary": """EDI Exchange Template - Add JSON generator.""", + "version": "18.0.1.0.0", + "development_status": "Beta", + "license": "LGPL-3", + "author": "Camptocamp, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/edi-framework", + "depends": ["edi_exchange_template_oca", "jsonifier"], + "data": [], +} diff --git a/edi_exchange_template_oca_json/models/__init__.py b/edi_exchange_template_oca_json/models/__init__.py new file mode 100644 index 000000000..8e5495fb6 --- /dev/null +++ b/edi_exchange_template_oca_json/models/__init__.py @@ -0,0 +1 @@ +from . import edi_exchange_template_output diff --git a/edi_exchange_template_oca_json/models/edi_exchange_template_output.py b/edi_exchange_template_oca_json/models/edi_exchange_template_output.py new file mode 100644 index 000000000..3a311993a --- /dev/null +++ b/edi_exchange_template_oca_json/models/edi_exchange_template_output.py @@ -0,0 +1,43 @@ +# Copyright 2025 Camptocamp SA +# @author Italo LOPES +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + + +from odoo import fields, models + + +class EDIExchangeOutputTemplate(models.Model): + _inherit = "edi.exchange.template.output" + + generator = fields.Selection( + selection_add=[("json", "JSON")], + ondelete={"json": "set default"}, + ) + + def _generate_json(self, exchange_record, **kw): + """Generate JSON output.""" + result = self._render_json_values(exchange_record, **kw) + return result + + def _post_process_output(self, output): + result = super()._post_process_output(output) + if self.generator == "json": + result = self._post_process_json_output(result) + return result + + def _render_json_values(self, exchange_record, **kw): + """Render JSON values.""" + parser = self._get_json_parser(exchange_record) + result = exchange_record.record.jsonify(parser=parser) + return result + + def _get_json_parser(self, exchange_record): + """Retrieve parser to use for JSON generation.""" + json_parser = self._evaluate_code_snippet() + if not json_parser: + json_parser = ["display_name"] + return json_parser + + def _post_process_json_output(self, output): + """Post-process JSON output.""" + return str(output) diff --git a/edi_exchange_template_oca_json/pyproject.toml b/edi_exchange_template_oca_json/pyproject.toml new file mode 100644 index 000000000..4231d0ccc --- /dev/null +++ b/edi_exchange_template_oca_json/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/edi_exchange_template_oca_json/readme/CONTRIBUTORS.md b/edi_exchange_template_oca_json/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..16455d65b --- /dev/null +++ b/edi_exchange_template_oca_json/readme/CONTRIBUTORS.md @@ -0,0 +1 @@ +- Italo Lopes \<\> diff --git a/edi_exchange_template_oca_json/readme/DESCRIPTION.md b/edi_exchange_template_oca_json/readme/DESCRIPTION.md new file mode 100644 index 000000000..7887e8481 --- /dev/null +++ b/edi_exchange_template_oca_json/readme/DESCRIPTION.md @@ -0,0 +1 @@ +Provide extra JSON generator type for EDI exchange templates. diff --git a/edi_exchange_template_oca_json/static/description/icon.png b/edi_exchange_template_oca_json/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/edi_exchange_template_oca_json/static/description/icon.png differ diff --git a/edi_exchange_template_oca_json/static/description/index.html b/edi_exchange_template_oca_json/static/description/index.html new file mode 100644 index 000000000..055778fc0 --- /dev/null +++ b/edi_exchange_template_oca_json/static/description/index.html @@ -0,0 +1,420 @@ + + + + + +EDI Exchange Template - Add JSON generator + + + +
+

EDI Exchange Template - Add JSON generator

+ + +

Beta License: LGPL-3 OCA/edi-framework Translate me on Weblate Try me on Runboat

+

Provide extra JSON generator type for EDI exchange templates.

+

Table of contents

+ +
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +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 +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Camptocamp
  • +
+
+ +
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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/edi-framework project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/edi_exchange_template_oca_json/tests/__init__.py b/edi_exchange_template_oca_json/tests/__init__.py new file mode 100644 index 000000000..9041c54e7 --- /dev/null +++ b/edi_exchange_template_oca_json/tests/__init__.py @@ -0,0 +1 @@ +from . import test_edi_backend_output_json diff --git a/edi_exchange_template_oca_json/tests/test_edi_backend_output_json.py b/edi_exchange_template_oca_json/tests/test_edi_backend_output_json.py new file mode 100644 index 000000000..578458e03 --- /dev/null +++ b/edi_exchange_template_oca_json/tests/test_edi_backend_output_json.py @@ -0,0 +1,65 @@ +# Copyright 2025 Camptocamp SA +# @author Italo LOPES +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + + +from odoo.addons.edi_exchange_template_oca.tests.test_edi_backend_output import ( + TestEDIBackendOutputBase, +) + + +class TestEDIBackendOutputJsonBase(TestEDIBackendOutputBase): + @classmethod + def _setup_records(cls): + res = super()._setup_records() + cls.type_out_json = cls._create_exchange_type( + name="Template output JSON", + direction="output", + code="test_type_out_json", + exchange_file_ext="txt", + exchange_filename_pattern="{record.ref}-{type.code}-{dt}", + ) + model = cls.env["edi.exchange.template.output"] + cls.tmpl_out_json = model.create( + { + "generator": "json", + "name": "Out JSON", + "backend_type_id": cls.backend.backend_type_id.id, + "code": "test_type_out_json", + "type_id": cls.type_out_json.id, + "output_type": "json", + "code_snippet": """ +field_list = { + "fields": [ + { + "name": "name" + }, + { + "name": "ref" + } + ] +} +result = field_list + """, + } + ) + vals = { + "model": cls.partner._name, + "res_id": cls.partner.id, + "type_id": cls.tmpl_out_json.id, + } + cls.record_json = cls.backend.create_record("test_type_out_json", vals) + return res + + +class TestEDIBackendOutputJson(TestEDIBackendOutputJsonBase): + def test_00_get_template_json(self): + self.assertEqual( + self.backend._get_output_template(self.record_json), self.tmpl_out_json + ) + + def test_01_generate_json(self): + self.backend.exchange_generate(self.record_json) + expected = f"[{{'name': '{self.partner.name}', 'ref': '{self.partner.ref}'}}]" + file_content = self.record_json._get_file_content() + self.assertEqual(file_content.strip(), expected)