diff --git a/edi_exchange_template_oca/README.rst b/edi_exchange_template_oca/README.rst new file mode 100644 index 000000000..a9995904c --- /dev/null +++ b/edi_exchange_template_oca/README.rst @@ -0,0 +1,89 @@ +===================== +EDI Exchange Template +===================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:77f9c8f1d2157e0270a1eadf85a1d39476fdefc8e14bc5cfe0fb47c3dadf118e + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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-lightgray.png?logo=github + :target: https://github.com/OCA/edi/tree/15.0/edi_exchange_template_oca + :alt: OCA/edi +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/edi-15-0/edi-15-0-edi_exchange_template_oca + :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&target_branch=15.0 + :alt: Try me on Runboat + +|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 + +**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 +~~~~~~~ + +* 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. + +.. |maintainer-simahawk| image:: https://github.com/simahawk.png?size=40px + :target: https://github.com/simahawk + :alt: simahawk + +Current `maintainer `__: + +|maintainer-simahawk| + +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..11ac0bb9e --- /dev/null +++ b/edi_exchange_template_oca/__manifest__.py @@ -0,0 +1,19 @@ +# Copyright 2020 ACSONE +# @author: Simone Orsi +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +{ + "name": "EDI Exchange Template", + "summary": """Allows definition of exchanges via templates.""", + "version": "16.0.1.0.0", + "development_status": "Beta", + "license": "LGPL-3", + "author": "ACSONE,Odoo Community Association (OCA)", + "maintainers": ["simahawk"], + "website": "https://github.com/OCA/edi-framework", + "depends": ["edi_oca", "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..39c0d5816 --- /dev/null +++ b/edi_exchange_template_oca/components/common.py @@ -0,0 +1,23 @@ +# Copyright 2020 ACSONE +# @author: Simone Orsi +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from odoo.addons.component.core import AbstractComponent + + +class EDIExchangeInfoMixin(AbstractComponent): + """Abstract component mixin provide info for exchanges.""" + + # TODO: this should be moved to core and renamed to `data`. + # A `data` component could be used for both incoming and outgoing. + + _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..0e50c6f57 --- /dev/null +++ b/edi_exchange_template_oca/components/output_mixin.py @@ -0,0 +1,42 @@ +# Copyright 2020 ACSONE +# @author: Simone Orsi +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +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/i18n/edi_exchange_template.pot b/edi_exchange_template_oca/i18n/edi_exchange_template.pot new file mode 100644 index 000000000..6e0b1ccfc --- /dev/null +++ b/edi_exchange_template_oca/i18n/edi_exchange_template.pot @@ -0,0 +1,184 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * edi_exchange_template +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 13.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: edi_exchange_template +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_mixin__backend_id +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_output__backend_id +#: model_terms:ir.ui.view,arch_db:edi_exchange_template.edi_exchange_template_output_view_search +msgid "Backend" +msgstr "" + +#. module: edi_exchange_template +#: model_terms:ir.ui.view,arch_db:edi_exchange_template.edi_exchange_template_output_view_search +msgid "Backend type" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_mixin__code +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_output__code +msgid "Code" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_mixin__code_snippet +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_output__code_snippet +msgid "Code Snippet" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_mixin__code_snippet_docs +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_output__code_snippet_docs +msgid "Code Snippet Docs" +msgstr "" + +#. module: edi_exchange_template +#: model_terms:ir.ui.view,arch_db:edi_exchange_template.edi_exchange_template_output_view_form +msgid "Code snippet" +msgstr "" + +#. module: edi_exchange_template +#: model_terms:ir.ui.view,arch_db:edi_exchange_template.edi_exchange_template_output_view_form +msgid "Code snippet docs" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_output__create_uid +msgid "Created by" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_output__create_date +msgid "Created on" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_mixin__display_name +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_output__display_name +msgid "Display Name" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model,name:edi_exchange_template.model_edi_backend +msgid "EDI Backend" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_mixin__backend_type_id +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_output__backend_type_id +msgid "EDI Backend type" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model,name:edi_exchange_template.model_edi_exchange_template_mixin +msgid "EDI Exchange Output Mixin" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model,name:edi_exchange_template.model_edi_exchange_template_output +msgid "EDI Exchange Output Template" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.actions.act_window,name:edi_exchange_template.act_open_edi_exchange_template_output_view +#: model:ir.ui.menu,name:edi_exchange_template.menu_edi_exchange_template_output +#: model_terms:ir.ui.view,arch_db:edi_exchange_template.edi_exchange_template_output_view_form +#: model_terms:ir.ui.view,arch_db:edi_exchange_template.edi_exchange_template_output_view_search +#: model_terms:ir.ui.view,arch_db:edi_exchange_template.edi_exchange_template_output_view_tree +msgid "EDI Exchange Template Output" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.ui.menu,name:edi_exchange_template.menu_edi_exchange_template_root +msgid "EDI Exchange Templates" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_mixin__type_id +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_output__type_id +msgid "EDI Exchange type" +msgstr "" + +#. module: edi_exchange_template +#: model_terms:ir.ui.view,arch_db:edi_exchange_template.edi_exchange_template_output_view_search +msgid "Group By" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_mixin__id +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_output__id +msgid "ID" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model.fields,help:edi_exchange_template.field_edi_exchange_template_output__template_key +msgid "ID of the view defined in xml file" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_mixin____last_update +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_output____last_update +msgid "Last Modified on" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_output__write_uid +msgid "Last Updated by" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_output__write_date +msgid "Last Updated on" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_mixin__name +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_output__name +msgid "Name" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_output__output_type +msgid "Output Type" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_output__template_arch +msgid "QWeb arch" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_output__template_id +msgid "Qweb Template" +msgstr "" + +#. module: edi_exchange_template +#: model_terms:ir.ui.view,arch_db:edi_exchange_template.edi_exchange_template_output_view_form +msgid "Template" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model.fields,field_description:edi_exchange_template.field_edi_exchange_template_output__template_key +msgid "Template key" +msgstr "" + +#. module: edi_exchange_template +#: model:ir.model.fields,help:edi_exchange_template.field_edi_exchange_template_output__template_arch +msgid "This field stores the view arch." +msgstr "" + +#. module: edi_exchange_template +#: model_terms:ir.ui.view,arch_db:edi_exchange_template.edi_exchange_template_output_view_search +msgid "Type" +msgstr "" diff --git a/edi_exchange_template_oca/i18n/edi_exchange_template_oca.pot b/edi_exchange_template_oca/i18n/edi_exchange_template_oca.pot new file mode 100644 index 000000000..98a6eda49 --- /dev/null +++ b/edi_exchange_template_oca/i18n/edi_exchange_template_oca.pot @@ -0,0 +1,192 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * edi_exchange_template_oca +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_mixin__backend_id +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_output__backend_id +#: model_terms:ir.ui.view,arch_db:edi_exchange_template_oca.edi_exchange_template_output_view_search +msgid "Backend" +msgstr "" + +#. module: edi_exchange_template_oca +#: model_terms:ir.ui.view,arch_db:edi_exchange_template_oca.edi_exchange_template_output_view_search +msgid "Backend type" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_mixin__code +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_output__code +msgid "Code" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_mixin__code_snippet +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_output__code_snippet +msgid "Code Snippet" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_mixin__code_snippet_docs +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_output__code_snippet_docs +msgid "Code Snippet Docs" +msgstr "" + +#. module: edi_exchange_template_oca +#: model_terms:ir.ui.view,arch_db:edi_exchange_template_oca.edi_exchange_template_output_view_form +msgid "Code snippet" +msgstr "" + +#. module: edi_exchange_template_oca +#: model_terms:ir.ui.view,arch_db:edi_exchange_template_oca.edi_exchange_template_output_view_form +msgid "Code snippet docs" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_output__create_uid +msgid "Created by" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_output__create_date +msgid "Created on" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_output__display_name +msgid "Display Name" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model,name:edi_exchange_template_oca.model_edi_backend +msgid "EDI Backend" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_mixin__backend_type_id +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_output__backend_type_id +msgid "EDI Backend type" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model,name:edi_exchange_template_oca.model_edi_exchange_template_mixin +msgid "EDI Exchange Output Mixin" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model,name:edi_exchange_template_oca.model_edi_exchange_template_output +msgid "EDI Exchange Output Template" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.actions.act_window,name:edi_exchange_template_oca.act_open_edi_exchange_template_output_view +#: model:ir.ui.menu,name:edi_exchange_template_oca.menu_edi_exchange_template_output +#: model_terms:ir.ui.view,arch_db:edi_exchange_template_oca.edi_exchange_template_output_view_form +#: model_terms:ir.ui.view,arch_db:edi_exchange_template_oca.edi_exchange_template_output_view_search +msgid "EDI Exchange Template Output" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.ui.menu,name:edi_exchange_template_oca.menu_edi_exchange_template_root +msgid "EDI Exchange Templates" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_mixin__type_id +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_output__type_id +msgid "EDI Exchange type" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_output__generator +msgid "Generator" +msgstr "" + +#. module: edi_exchange_template_oca +#: model_terms:ir.ui.view,arch_db:edi_exchange_template_oca.edi_exchange_template_output_view_search +msgid "Group By" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_output__id +msgid "ID" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,help:edi_exchange_template_oca.field_edi_exchange_template_output__template_key +msgid "ID of the view defined in xml file" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_output____last_update +msgid "Last Modified on" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_output__write_uid +msgid "Last Updated by" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_output__write_date +msgid "Last Updated on" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_mixin__name +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_output__name +msgid "Name" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_output__output_type +msgid "Output Type" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_output__template_arch +msgid "QWeb arch" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_output__template_id +#: model:ir.model.fields.selection,name:edi_exchange_template_oca.selection__edi_exchange_template_output__generator__qweb +msgid "Qweb Template" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_output__report_id +#: model:ir.model.fields.selection,name:edi_exchange_template_oca.selection__edi_exchange_template_output__generator__report +msgid "Report" +msgstr "" + +#. module: edi_exchange_template_oca +#: model_terms:ir.ui.view,arch_db:edi_exchange_template_oca.edi_exchange_template_output_view_form +msgid "Template" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,field_description:edi_exchange_template_oca.field_edi_exchange_template_output__template_key +msgid "Template key" +msgstr "" + +#. module: edi_exchange_template_oca +#: model:ir.model.fields,help:edi_exchange_template_oca.field_edi_exchange_template_output__template_arch +msgid "This field stores the view arch." +msgstr "" + +#. module: edi_exchange_template_oca +#: model_terms:ir.ui.view,arch_db:edi_exchange_template_oca.edi_exchange_template_output_view_search +msgid "Type" +msgstr "" 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..393eb8bd3 --- /dev/null +++ b/edi_exchange_template_oca/models/edi_backend.py @@ -0,0 +1,46 @@ +# Copyright 2020 ACSONE SA +# @author Simone Orsi +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from odoo import models + + +class EDIBackend(models.Model): + _inherit = "edi.backend" + + def _exchange_generate(self, exchange_record, **kw): + # Template take precedence over component lookup + tmpl = self._get_output_template(exchange_record) + if tmpl: + return tmpl.exchange_generate(exchange_record, **kw) + return super()._exchange_generate(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. + """ + search = self.output_template_model.search + # TODO: maybe we can add a m2o to output templates + # but then we would need another for input templates if they are introduced. + tmpl = None + if code: + domain = [("code", "=", code)] + return search(domain, limit=1) + for domain in self._get_output_template_domains(exchange_record): + tmpl = search(domain, limit=1) + if tmpl: + break + return tmpl + + def _get_output_template_domains(self, exchange_record): + """Retrieve domains to lookup for templates by priority.""" + backend_type_leaf = [("backend_type_id", "=", self.backend_type_id.id)] + exchange_type_leaf = [("type_id", "=", exchange_record.type_id.id)] + full_match_domain = backend_type_leaf + exchange_type_leaf + partial_match_domain = backend_type_leaf + return full_match_domain, partial_match_domain 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..8a8e1cc5a --- /dev/null +++ b/edi_exchange_template_oca/models/edi_exchange_template_mixin.py @@ -0,0 +1,115 @@ +# Copyright 2020 ACSONE SA +# @author Simone Orsi +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). +import datetime +import logging +import textwrap + +import pytz + +from odoo import fields, models +from odoo.tools import DotDict, 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": safe_eval.datetime, + "dateutil": safe_eval.dateutil, + "time": safe_eval.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.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..05e3e91d8 --- /dev/null +++ b/edi_exchange_template_oca/models/edi_exchange_template_output.py @@ -0,0 +1,138 @@ +# Copyright 2020 ACSONE SA +# @author Simone Orsi +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). +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 records' content.""" + + _name = "edi.exchange.template.output" + _inherit = "edi.exchange.template.mixin" + _description = "EDI Exchange Output Template" + + generator = fields.Selection( + [("qweb", "Qweb Template"), ("report", "Report")], + required=True, + default="qweb", + ) + output_type = fields.Char(required=True) + # TODO: add a good domain (maybe add a new flag or category to ir.ui.view) + # Options: + # 1. add a flag "edi_template" to ir.ui.view + # 2. set model="edi.exchange.template.output" on the view + # As templates are defined using `