Skip to content
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
5 changes: 4 additions & 1 deletion edi_exchange_template_oca/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ Authors
Contributors
~~~~~~~~~~~~

* Simone Orsi <simahawk@gmail.com>
- Simone Orsi <simahawk@gmail.com>
- John Herholz <j.longneck@gmail.com>
- Italo Lopes <italo.lopes@camptocamp.com>
- Hadrien HUVELLE <hadrien.huvelle@camptocamp.com>

Maintainers
~~~~~~~~~~~
Expand Down
17 changes: 13 additions & 4 deletions edi_exchange_template_oca/models/edi_exchange_template_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,21 @@ def _evaluate_code_snippet(self, **render_values):
if not isinstance(result, dict):
_logger.error("code_snippet should return a dict into `result`")
return {}
validator = getattr(
self,
f"_evaluate_code_snippet_validate_{self.generator}",
lambda result: False,
)
err_msg = validator(result)
if err_msg:
_logger.error("code_snippet validation error: %s", err_msg)
return {}
return result

def _get_validator(self, exchange_record):
# TODO: lookup for validator (
# can be to validate received file or generated file)
pass
def _evaluate_code_snippet_validate_json(self, result):
if "payload" not in result.keys():
return "JSON code_snippet should return a dict with a 'payload' key"
return False

def validate(self, exchange_record):
pass
22 changes: 20 additions & 2 deletions edi_exchange_template_oca/models/edi_exchange_template_output.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright 2020 ACSONE SA
# @author Simone Orsi <simahawk@gmail.com>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
import json

import lxml.etree as etree

from odoo import fields, models
Expand All @@ -16,7 +18,7 @@ class EDIExchangeOutputTemplate(models.Model):
_description = "EDI Exchange Output Template"

generator = fields.Selection(
[("qweb", "Qweb Template"), ("report", "Report")],
[("json", "JSON"), ("qweb", "Qweb Template"), ("report", "Report")],
required=True,
default="qweb",
)
Expand Down Expand Up @@ -86,6 +88,12 @@ def _generate_report(self, exchange_record, **kw):
res_ids = values.get("res_ids", [])
return self.env["ir.actions.report"]._render(report, res_ids, data=values)[0]

def _generate_json(self, exchange_record, **kw):
# We expect to have json data into `payload` key.
# See validator `_evaluate_code_snippet_validate_json` in the mixin.
result = self._get_render_values(exchange_record, **kw)
return result

def _get_render_values(self, exchange_record, **kw):
"""Collect values to render current template."""
values = {
Expand Down Expand Up @@ -113,13 +121,23 @@ def _render_template(self, exchange_record, code, **kw):

def _post_process_output(self, output):
"""Post process generated output."""
processor = getattr(
self,
f"_post_process_output_{self.generator}",
lambda result: result,
)
return processor(output)

def _post_process_output_qweb(self, output):
if self.output_type == "xml":
# TODO: lookup for components to handle this dynamically
output = xml_purge_nswrapper(output)
if self.prettify:
output = self._prettify_xml(output)
return output

def _post_process_output_json(self, output):
return json.dumps(output["payload"])

def _prettify_xml(self, xml_string):
return etree.tostring(etree.fromstring(xml_string), pretty_print=True)

Expand Down
5 changes: 4 additions & 1 deletion edi_exchange_template_oca/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
* Simone Orsi <simahawk@gmail.com>
- Simone Orsi \<<simahawk@gmail.com>\>
- John Herholz \<<j.longneck@gmail.com>\>
- Italo Lopes \<<italo.lopes@camptocamp.com>\>
- Hadrien HUVELLE \<<hadrien.huvelle@camptocamp.com>\>
3 changes: 3 additions & 0 deletions edi_exchange_template_oca/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,9 @@ <h2><a class="toc-backref" href="#toc-entry-3">Authors</a></h2>
<h2><a class="toc-backref" href="#toc-entry-4">Contributors</a></h2>
<ul class="simple">
<li>Simone Orsi &lt;<a class="reference external" href="mailto:simahawk&#64;gmail.com">simahawk&#64;gmail.com</a>&gt;</li>
<li>John Herholz &lt;<a class="reference external" href="mailto:j.longneck&#64;gmail.com">j.longneck&#64;gmail.com</a>&gt;</li>
<li>Italo Lopes &lt;<a class="reference external" href="mailto:italo.lopes&#64;camptocamp.com">italo.lopes&#64;camptocamp.com</a>&gt;</li>
<li>Hadrien HUVELLE &lt;<a class="reference external" href="mailto:hadrien.huvelle&#64;camptocamp.com">hadrien.huvelle&#64;camptocamp.com</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
41 changes: 41 additions & 0 deletions edi_exchange_template_oca/tests/test_edi_backend_output.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2020 ACSONE SA/NV (<http://acsone.eu>)
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
import json

from lxml import etree

Expand Down Expand Up @@ -119,6 +120,39 @@ def _setup_records(cls):
"type_id": cls.type_out2.id,
}
cls.record3 = cls.backend.create_record("test_type_out3", vals)

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": """
result = {
'payload': {
"name": record.name,
"ref": record.ref
}
}
""",
}
)
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


Expand All @@ -135,6 +169,9 @@ def test_get_template(self):
self.backend._get_output_template(self.record2, code=self.tmpl_out1.code),
self.tmpl_out1,
)
self.assertEqual(
self.backend._get_output_template(self.record_json), self.tmpl_out_json
)

def test_generate_file(self):
output = self.backend.exchange_generate(self.record1)
Expand All @@ -152,6 +189,10 @@ def test_generate_file(self):
self.assertEqual(doc.getchildren()[1].tag, "Custom")
self.assertEqual(doc.getchildren()[1].text, "2")
self.assertEqual(doc.getchildren()[1].attrib, {"bit": "custom_var"})
self.backend.exchange_generate(self.record_json)
expected = json.dumps({"name": self.partner.name, "ref": self.partner.ref})
file_content = self.record_json._get_file_content()
self.assertEqual(file_content.strip(), expected)

def test_prettify(self):
self.tmpl_out2.template_id.arch = (
Expand Down