diff --git a/edi_exchange_template_oca/README.rst b/edi_exchange_template_oca/README.rst new file mode 100644 index 000000000..2a497ce50 --- /dev/null +++ b/edi_exchange_template_oca/README.rst @@ -0,0 +1,83 @@ +===================== +EDI Exchange Template +===================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png + :target: https://odoo-community.org/page/development-status + :alt: Alpha +.. |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%2Fedi-lightgray.png?logo=github + :target: https://github.com/OCA/edi/tree/13.0/edi_exchange_template + :alt: OCA/edi +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/edi-13-0/edi-13-0-edi_exchange_template + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/226/13.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Provide EDI exchange templates to control input/output records contents. + +Provides following models: + +1. EDI exchange output template, generates output using QWeb templates +2. [TODO] EDI exchange input template + +.. IMPORTANT:: + This is an alpha version, the data model and design can change at any time without warning. + Only for development or testing purpose, do not use in production. + `More details on development status `_ + +**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 smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* ACSONE + +Contributors +~~~~~~~~~~~~ + +* Simone Orsi + +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 `_ 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/__init__.py b/edi_exchange_template_oca/__init__.py new file mode 100644 index 000000000..0f00a6730 --- /dev/null +++ b/edi_exchange_template_oca/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import components diff --git a/edi_exchange_template_oca/__manifest__.py b/edi_exchange_template_oca/__manifest__.py new file mode 100644 index 000000000..219dd0834 --- /dev/null +++ b/edi_exchange_template_oca/__manifest__.py @@ -0,0 +1,18 @@ +# Copyright 2020 ACSONE +# @author: Simone Orsi +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "EDI Exchange Template", + "summary": """Allows definition of exchanges via templates.""", + "version": "13.0.1.0.0", + "development_status": "Alpha", + "license": "AGPL-3", + "author": "ACSONE,Odoo Community Association (OCA)", + "maintainers": ["simahawk"], + "depends": ["edi", "component"], + "data": [ + "security/ir_model_access.xml", + "views/edi_exchange_template_output_views.xml", + ], +} diff --git a/edi_exchange_template_oca/components/__init__.py b/edi_exchange_template_oca/components/__init__.py new file mode 100644 index 000000000..ed5b32801 --- /dev/null +++ b/edi_exchange_template_oca/components/__init__.py @@ -0,0 +1,2 @@ +from . import common +from . import output_mixin diff --git a/edi_exchange_template_oca/components/common.py b/edi_exchange_template_oca/components/common.py new file mode 100644 index 000000000..06d6a876f --- /dev/null +++ b/edi_exchange_template_oca/components/common.py @@ -0,0 +1,20 @@ +# Copyright 2020 ACSONE +# @author: Simone Orsi +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo.addons.component.core import AbstractComponent + + +class EDIExchangeInfoMixin(AbstractComponent): + """Abstract component mixin provide info for exchanges.""" + + _name = "edi.info.provider.mixin" + _collection = "edi.backend" + # Enable validation of work context attributes + _work_context_validate_attrs = [] + + def __init__(self, work_context): + super().__init__(work_context) + for key in self._work_context_validate_attrs: + if not hasattr(work_context, key): + raise AttributeError(f"`{key}` is required for this component!") diff --git a/edi_exchange_template_oca/components/output_mixin.py b/edi_exchange_template_oca/components/output_mixin.py new file mode 100644 index 000000000..4567480ea --- /dev/null +++ b/edi_exchange_template_oca/components/output_mixin.py @@ -0,0 +1,42 @@ +# Copyright 2020 ACSONE +# @author: Simone Orsi +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import datetime + +import pytz + +from odoo import fields + +from odoo.addons.component.core import AbstractComponent + + +class EDIExchangeInfoOutputMixin(AbstractComponent): + """Abstract component mixin to generate GS1 compliant XML files.""" + + _name = "edi.output.mixin" + _inherit = "edi.info.provider.mixin" + # Enable validation of work context attributes + _work_context_validate_attrs = ["exchange_record"] + + @property + def record(self): + return self.work.exchange_record.record + + def generate_info(self): + """Generate and return data for output info. + + :return: odoo.tools.DotDict + """ + raise NotImplementedError() + + # helper methods + @staticmethod + def _utc_now(): + return datetime.datetime.utcnow().isoformat() + + @staticmethod + def date_to_string(dt, utc=True): + if utc: + dt = dt.astimezone(pytz.UTC) + return fields.Date.to_string(dt) diff --git a/edi_exchange_template_oca/models/__init__.py b/edi_exchange_template_oca/models/__init__.py new file mode 100644 index 000000000..a98be2011 --- /dev/null +++ b/edi_exchange_template_oca/models/__init__.py @@ -0,0 +1,3 @@ +from . import edi_backend +from . import edi_exchange_template_mixin +from . import edi_exchange_template_output diff --git a/edi_exchange_template_oca/models/edi_backend.py b/edi_exchange_template_oca/models/edi_backend.py new file mode 100644 index 000000000..559df9756 --- /dev/null +++ b/edi_exchange_template_oca/models/edi_backend.py @@ -0,0 +1,35 @@ +# Copyright 2020 ACSONE SA +# @author Simone Orsi +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import models + + +class EDIBackend(models.Model): + _inherit = "edi.backend" + + def _generate_output(self, exchange_record, **kw): + # Template take precedence over component lookup + tmpl = self._get_output_template(exchange_record) + if tmpl: + return tmpl.generate_output(exchange_record, **kw) + return super()._generate_output(exchange_record, **kw) + + @property + def output_template_model(self): + return self.env["edi.exchange.template.output"] + + def _get_output_template(self, exchange_record, code=None): + """Retrieve output templates by convention. + + Template's code must match the same component usage as per normal components. + """ + tmpl = None + candidates = self._generate_output_component_usage_candidates(exchange_record) + if code: + candidates.insert(code) + for usage in candidates: + tmpl = self.output_template_model.search([("code", "=", usage)], limit=1) + if tmpl: + break + return tmpl diff --git a/edi_exchange_template_oca/models/edi_exchange_template_mixin.py b/edi_exchange_template_oca/models/edi_exchange_template_mixin.py new file mode 100644 index 000000000..ea4dec728 --- /dev/null +++ b/edi_exchange_template_oca/models/edi_exchange_template_mixin.py @@ -0,0 +1,119 @@ +# Copyright 2020 ACSONE SA +# @author Simone Orsi +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import datetime +import logging +import textwrap +import time + +import dateutil +import pytz + +from odoo import fields, models +from odoo.tools import DotDict +from odoo.tools.safe_eval import safe_eval + +_logger = logging.getLogger(__name__) + + +class EDIExchangeTemplateMixin(models.AbstractModel): + """Define a common ground for EDI exchange templates. + """ + + _name = "edi.exchange.template.mixin" + _description = "EDI Exchange Output Mixin" + + name = fields.Char(required=True) + # TODO: make unique, autocompute slugified name + code = fields.Char(required=True, index=True) + backend_type_id = fields.Many2one( + string="EDI Backend type", + comodel_name="edi.backend.type", + ondelete="restrict", + required=True, + ) + type_id = fields.Many2one( + string="EDI Exchange type", + comodel_name="edi.exchange.type", + ondelete="cascade", + auto_join=True, + ) + backend_id = fields.Many2one( + comodel_name="edi.backend", + ondelete="cascade", + # TODO: default to type_id if given, validate otherwise + ) + # TODO: add default content w/ comment on what you can use + code_snippet = fields.Text() + code_snippet_docs = fields.Text( + compute="_compute_code_snippet_docs", + default=lambda self: self._default_code_snippet_docs(), + ) + + def _compute_code_snippet_docs(self): + for rec in self: + rec.code_snippet_docs = textwrap.dedent(rec._default_code_snippet_docs()) + + def _default_code_snippet_docs(self): + return """ + Available vars: + * datetime + * dateutil + * time + * uid + * user + * DotDict + """ + + def _code_snippet_valued(self): + snippet = self.code_snippet or "" + return bool( + [ + not line.startswith("#") + for line in (snippet.splitlines()) + if line.strip("") + ] + ) + + @staticmethod + def _utc_now(): + return datetime.datetime.utcnow().isoformat() + + @staticmethod + def _date_to_string(dt, utc=True): + if utc: + dt = dt.astimezone(pytz.UTC) + return fields.Date.to_string(dt) + + def _get_code_snippet_eval_context(self): + """Prepare the context used when evaluating python code + + :returns: dict -- evaluation context given to safe_eval + """ + return { + "datetime": datetime, + "dateutil": dateutil, + "time": time, + "uid": self.env.uid, + "user": self.env.user, + "DotDict": DotDict, + } + + def _evaluate_code_snippet(self, **render_values): + if not self._code_snippet_valued(): + return {} + eval_ctx = dict(render_values, **self._get_code_snippet_eval_context()) + safe_eval(self.code_snippet, eval_ctx, mode="exec", nocopy=True) + result = eval_ctx.get("result", {}) + if not isinstance(result, dict): + _logger.error("code_snippet should return a dict into `result`") + return {} + return result + + def _get_validator(self, exchange_record): + # TODO: lookup for validator ( + # can be to validate received file or generated file) + pass + + def validate(self, exchange_record): + pass diff --git a/edi_exchange_template_oca/models/edi_exchange_template_output.py b/edi_exchange_template_oca/models/edi_exchange_template_output.py new file mode 100644 index 000000000..1871c8912 --- /dev/null +++ b/edi_exchange_template_oca/models/edi_exchange_template_output.py @@ -0,0 +1,108 @@ +# Copyright 2020 ACSONE SA +# @author Simone Orsi +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import logging + +from odoo import fields, models + +from ..utils import xml_purge_nswrapper + +_logger = logging.getLogger(__name__) + + +class EDIExchangeOutputTemplate(models.Model): + """Define an output template to generate outgoing files + """ + + _name = "edi.exchange.template.output" + _inherit = "edi.exchange.template.mixin" + _description = "EDI Exchange Output Template" + + output_type = fields.Char(required=True) + # TODO: add a good domain (maybe add a new flag or category to ir.ui.view) + template_id = fields.Many2one( + string="Qweb Template", + comodel_name="ir.ui.view", + required=True, + ondelete="restrict", + ) + template_arch = fields.Text( + string="QWeb arch", related="template_id.arch_db", readonly=False, + ) + template_key = fields.Char(related="template_id.xml_id", string="Template key") + + def _default_code_snippet_docs(self): + return ( + super()._default_code_snippet_docs() + + """ + * exchange_record (edi.exchange.record) + * record (real odoo record related to this exchange) + * backend (current backend) + * template (current template record) + * utc_now + * date_to_string + * render_edi_template + * get_info_provider + * info + """ + ) + + def generate_output(self, exchange_record, **kw): + """Generate output for given record using related QWeb template. + """ + tmpl = self.template_id + # TODO: how to validate mandatory render values? + # (eg: sender/receiver are mandatory for BH) + values = self._get_render_values(exchange_record, **kw) + output = tmpl.render(values) + return self._post_process_output(output) + + def _get_render_values(self, exchange_record, **kw): + """Collect values to render current template.""" + values = { + "exchange_record": exchange_record, + "record": exchange_record.record, + "backend": exchange_record.backend_id, + "template": self, + "utc_now": self._utc_now, + "date_to_string": self._date_to_string, + "render_edi_template": self._render_template, + "get_info_provider": self._get_info_provider, + "info": {}, + } + values.update(self._evaluate_code_snippet(**values)) + values.update(kw) + return values + + def _render_template(self, exchange_record, code, **kw): + """Render another template matching `code`. + + This is very handy to render templates from other templates. + """ + tmpl = self.search([("code", "=", code)], limit=1) + tmpl.ensure_one() + return tmpl.generate_output(exchange_record, **kw) + + def _post_process_output(self, output): + """Post process generated output. + """ + if self.output_type == "xml": + # TODO: lookup for components to handle this dynamically + return xml_purge_nswrapper(output) + return output + + def _get_info_provider(self, exchange_record, work_ctx=None, usage=None, **kw): + """Retrieve component providing info to render a template. + + TODO: improve this description. + TODO: add tests + """ + default_work_ctx = dict( + exchange_record=exchange_record, record=exchange_record.record, + ) + default_work_ctx.update(work_ctx or {}) + usage_candidates = [usage or self.code + ".info"] + provider = exchange_record.backend_id._get_component( + usage_candidates, work_ctx=default_work_ctx, **kw + ) + return provider diff --git a/edi_exchange_template_oca/readme/CONTRIBUTORS.rst b/edi_exchange_template_oca/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..f583948be --- /dev/null +++ b/edi_exchange_template_oca/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Simone Orsi diff --git a/edi_exchange_template_oca/readme/DESCRIPTION.rst b/edi_exchange_template_oca/readme/DESCRIPTION.rst new file mode 100644 index 000000000..491f1212a --- /dev/null +++ b/edi_exchange_template_oca/readme/DESCRIPTION.rst @@ -0,0 +1,6 @@ +Provide EDI exchange templates to control input/output records contents. + +Provides following models: + +1. EDI exchange output template, generates output using QWeb templates +2. [TODO] EDI exchange input template diff --git a/edi_exchange_template_oca/security/ir_model_access.xml b/edi_exchange_template_oca/security/ir_model_access.xml new file mode 100644 index 000000000..c611b9dbf --- /dev/null +++ b/edi_exchange_template_oca/security/ir_model_access.xml @@ -0,0 +1,12 @@ + + + + edi_exchange_template_output manager + + + + + + + + diff --git a/edi_exchange_template_oca/static/description/icon.png b/edi_exchange_template_oca/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/edi_exchange_template_oca/static/description/icon.png differ diff --git a/edi_exchange_template_oca/static/description/index.html b/edi_exchange_template_oca/static/description/index.html new file mode 100644 index 000000000..81c8f0f4a --- /dev/null +++ b/edi_exchange_template_oca/static/description/index.html @@ -0,0 +1,430 @@ + + + + + + +EDI Exchange Template + + + +
+

EDI Exchange Template

+ + +

Alpha License: AGPL-3 OCA/edi Translate me on Weblate Try me on Runbot

+

Provide EDI exchange templates to control input/output records contents.

+

Provides following models:

+
    +
  1. EDI exchange output template, generates output using QWeb templates
  2. +
  3. [TODO] EDI exchange input template
  4. +
+
+

Important

+

This is an alpha version, the data model and design can change at any time without warning. +Only for development or testing purpose, do not use in production. +More details on development status

+
+

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 smashing it by providing a detailed and welcomed +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • ACSONE
  • +
+
+
+

Contributors

+ +
+
+

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 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/tests/__init__.py b/edi_exchange_template_oca/tests/__init__.py new file mode 100644 index 000000000..dbf4fc661 --- /dev/null +++ b/edi_exchange_template_oca/tests/__init__.py @@ -0,0 +1 @@ +from . import test_edi_backend_output diff --git a/edi_exchange_template_oca/tests/test_edi_backend_output.py b/edi_exchange_template_oca/tests/test_edi_backend_output.py new file mode 100644 index 000000000..0212886ac --- /dev/null +++ b/edi_exchange_template_oca/tests/test_edi_backend_output.py @@ -0,0 +1,124 @@ +# Copyright 2020 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import base64 + +from lxml import etree + +from odoo.addons.edi.tests.common import EDIBackendCommonTestCase + + +class TestEDIBackendOutputBase(EDIBackendCommonTestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls._setup_records() + + @classmethod + def _setup_records(cls): + cls.type_out1 = cls._create_exchange_type( + name="Template output 1", + direction="output", + code="test_type_out1", + exchange_file_ext="txt", + exchange_filename_pattern="{record.ref}-{type.code}-{dt}", + ) + model = cls.env["edi.exchange.template.output"] + qweb_tmpl = cls.env["ir.ui.view"].create( + { + "type": "qweb", + "key": "edi_exchange.test_output1", + "arch": """ + + - + + """, + } + ) + cls.tmpl_out1 = model.create( + { + "code": "edi.output.demo_backend.test_type_out1", + "name": "Out 1", + "backend_type_id": cls.backend.backend_type_id.id, + "type_id": cls.type_out1.id, + "template_id": qweb_tmpl.id, + "output_type": "txt", + } + ) + vals = { + "model": cls.partner._name, + "res_id": cls.partner.id, + "type_id": cls.type_out1.id, + } + cls.record1 = cls.backend.create_record("test_type_out1", vals) + + cls.type_out2 = cls._create_exchange_type( + name="Template output 2", + direction="output", + code="test_type_out2", + exchange_file_ext="xml", + exchange_filename_pattern="{record.ref}-{type.code}-{dt}", + ) + qweb_tmpl = cls.env["ir.ui.view"].create( + { + "type": "qweb", + "key": "edi_exchange.test_output2", + "arch": """ + + + + + + + + + """, + } + ) + cls.tmpl_out2 = model.create( + { + "code": "edi.output.demo_backend.test_type_out2", + "name": "Out 2", + "backend_type_id": cls.backend.backend_type_id.id, + "type_id": cls.type_out2.id, + "template_id": qweb_tmpl.id, + "output_type": "xml", + "code_snippet": """ +foo = "custom_var" +baz = 2 +result = {"custom_bit": foo, "baz": baz} + """, + } + ) + vals = { + "model": cls.partner._name, + "res_id": cls.partner.id, + "type_id": cls.type_out2.id, + } + cls.record2 = cls.backend.create_record("test_type_out2", vals) + + +# TODO: add more unit tests +class TestEDIBackendOutput(TestEDIBackendOutputBase): + def test_get_template(self): + self.assertEqual( + self.backend._get_output_template(self.record1), self.tmpl_out1 + ) + self.assertEqual( + self.backend._get_output_template(self.record2), self.tmpl_out2 + ) + + def test_generate_file(self): + output = self.backend.generate_output(self.record1) + expected = "{0.ref} - {0.name}".format(self.partner) + self.assertEqual(output.strip(), expected) + file_content = base64.b64decode(self.record1.exchange_file).decode() + self.assertEqual(file_content.strip(), expected) + output = self.backend.generate_output(self.record2) + doc = etree.fromstring(output) + self.assertEqual(doc.tag, "Record") + self.assertEqual(doc.attrib, {"ref": self.partner.ref}) + self.assertEqual(doc.getchildren()[0].tag, "Name") + self.assertEqual(doc.getchildren()[0].text, self.partner.name) + self.assertEqual(doc.getchildren()[1].tag, "Custom") + self.assertEqual(doc.getchildren()[1].text, "2") + self.assertEqual(doc.getchildren()[1].attrib, {"bit": "custom_var"}) diff --git a/edi_exchange_template_oca/utils.py b/edi_exchange_template_oca/utils.py new file mode 100644 index 000000000..4fd56ed44 --- /dev/null +++ b/edi_exchange_template_oca/utils.py @@ -0,0 +1,40 @@ +from lxml import etree + +from odoo.tools import pycompat + + +def xml_purge_nswrapper(xml_content): + """Purge `nswrapper` elements. + + QWeb template does not allow parsing namespaced elements + without declaring namespaces on the root element. + + Hence, by default you cannot define smaller re-usable templates + if the have namespaced elements. + + The trick is to wrap your reusable template with `nswrapper` element + which holds the namespace for that particular sub template. + For instance: + + + + + + Then this method is going to purge these unwanted elements from the result. + """ + if not (xml_content and xml_content.strip()): + return xml_content + root = etree.XML(xml_content) + # deeper elements come after, keep the root element at the end (if any) + for nswrapper in reversed(root.xpath("//nswrapper")): + parent = nswrapper.getparent() + if not parent: + # fmt:off + return "".join([ + pycompat.to_text(etree.tostring(el)) + for el in nswrapper.getchildren() + ]) + # fmt:on + parent.extend(nswrapper.getchildren()) + parent.remove(nswrapper) + return etree.tostring(root) diff --git a/edi_exchange_template_oca/views/edi_exchange_template_output_views.xml b/edi_exchange_template_oca/views/edi_exchange_template_output_views.xml new file mode 100644 index 000000000..e5e42e791 --- /dev/null +++ b/edi_exchange_template_oca/views/edi_exchange_template_output_views.xml @@ -0,0 +1,134 @@ + + + + edi.exchange.template.output + + + + + + + + + + + + edi.exchange.template.output + +
+ +
+
+ + + + + + + + + + + + + + + + + + + + + +
+
+
+
+ + edi.exchange.template.output + + + + + + + + + + + + + + + + EDI Exchange Template Output + ir.actions.act_window + edi.exchange.template.output + tree,form + + [] + {} + + + + + form + + + + + + tree + + + + +