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
10 changes: 2 additions & 8 deletions edi_component_oca/models/edi_exchange_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@
class EdiExchangeRecord(models.Model):
_inherit = "edi.exchange.record"

def _trigger_edi_event_make_name(self, name, suffix=None):
return "on_edi_exchange_{name}{suffix}".format(
name=name,
suffix=("_" + suffix) if suffix else "",
)

def _trigger_edi_event(self, name, suffix=None, target=None, **kw):
"""Trigger a component event linked to this backend and edi exchange."""
name = self._trigger_edi_event_make_name(name, suffix=suffix)
method_name = self._trigger_edi_event_make_name(name, suffix=suffix)
target = target or self
target._event(name).notify(self, **kw)
target._event(method_name).notify(self, **kw)
return super()._trigger_edi_event(name, suffix=suffix, target=target, **kw)
6 changes: 6 additions & 0 deletions edi_core_oca/models/edi_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ class EdiConfiguration(models.Model):
help="""Used to do something specific here.
Receives: operation, edi_action, vals, old_vals.""",
)
is_global = fields.Boolean(
string="Global Configuration",
help="If checked, this configuration will be executed for all records, "
"regardless of the partner.",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"regardless of the partner.",
"regardless of any related odoo record.",

default=False,
)

@api.constrains("backend_id", "type_id")
def _constrains_backend(self):
Expand Down
20 changes: 18 additions & 2 deletions edi_core_oca/models/edi_exchange_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,24 @@ def _notify_related_record(self, message, level="info"):
rec._notify_related_record(message, level)

def _trigger_edi_event(self, name, suffix=None, target=None, **kw):
"""Hook to be implemented in other modules"""
pass
event_name = self._trigger_edi_event_make_name(name, suffix)
target = target or self

global_configs = self.env["edi.configuration"].search(
[
("trigger", "=", event_name),
("is_global", "=", True),
]
)

for conf in global_configs:
conf.edi_exec_snippet_do(target, **kw)

def _trigger_edi_event_make_name(self, name, suffix=None):
return "on_edi_exchange_{name}{suffix}".format(
name=name,
suffix=("_" + suffix) if suffix else "",
)

def _notify_done(self):
self._notify_related_record(self._exchange_status_message("process_ok"))
Expand Down
1 change: 1 addition & 0 deletions edi_core_oca/views/edi_configuration_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
name="model_id"
options="{'no_create': True, 'no_create_edit': True}"
/>
<field name="is_global" />
</group>
</group>
<notebook>
Expand Down
6 changes: 1 addition & 5 deletions edi_storage_oca/README.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
.. image:: https://odoo-community.org/readme-banner-image
:target: https://odoo-community.org/get-involved?utm_source=readme
:alt: Odoo Community Association

===========================
EDI Storage backend support
===========================
Expand All @@ -17,7 +13,7 @@ EDI Storage backend support
.. |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/license-LGPL--3-blue.png
.. |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
Expand Down
1 change: 1 addition & 0 deletions edi_storage_oca/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"depends": ["edi_core_oca", "fs_storage"],
"data": [
"data/cron.xml",
"data/edi_configuration.xml",
"security/ir_model_access.xml",
"views/edi_backend_views.xml",
],
Expand Down
24 changes: 24 additions & 0 deletions edi_storage_oca/data/edi_configuration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<odoo noupdate="1">
<record id="edi_config_trigger_storage_done" model="edi.configuration.trigger">
<field name="name">On record exchange done</field>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aren't these triggers generic enough to stay in the core module? 🤔

<field name="code">on_edi_exchange_done</field>
<field name="description">Trigger when a record exchange is done</field>
</record>
<record id="edi_config_trigger_storage_error" model="edi.configuration.trigger">
<field name="name">On record exchange error</field>
<field name="code">on_edi_exchange_error</field>
<field name="description">Trigger when a record exchange has an error</field>
</record>
<record id="edi_config_storage_done" model="edi.configuration">
<field name="name">Storage Move File on Done</field>
<field name="is_global" eval="True" />
<field name="trigger_id" ref="edi_config_trigger_storage_done" />
<field name="snippet_do">record.on_edi_exchange_done()</field>
</record>
<record id="edi_config_storage_error" model="edi.configuration">
<field name="name">Storage Move File on Error</field>
<field name="is_global" eval="True" />
<field name="trigger_id" ref="edi_config_trigger_storage_error" />
<field name="snippet_do">record.on_edi_exchange_error()</field>
</record>
</odoo>
70 changes: 70 additions & 0 deletions edi_storage_oca/models/edi_exchange_record.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# Copyright 2024 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import functools
import os
from pathlib import PurePath

from odoo import fields, models

from .. import utils


class EDIExchangeRecord(models.Model):
_inherit = "edi.exchange.record"
Expand All @@ -13,3 +19,67 @@ class EDIExchangeRecord(models.Model):
string="FS Storage",
help="Record created from a file found in this FS storage",
)

def _move_file(self, storage, from_dir_str, to_dir_str, filename):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this to fs_storage as we are dragging this from project to project, module to module

from_dir = PurePath(from_dir_str)
to_dir = PurePath(to_dir_str)
# - storage.list_files now includes path in fs_storage, breaking change
# - we remove path
files = utils.list_files(storage, from_dir.as_posix())
files = [os.path.basename(f) for f in files]
if filename not in files:
return False
self._add_post_commit_hook(
utils.move_files,
storage,
[(from_dir / filename).as_posix()],
to_dir.as_posix(),
)
return True

def _add_post_commit_hook(
self, move_func, storage, sftp_filepath, sftp_destination_path
):
"""Add hook after commit to move the file when transaction is over."""
self.env.cr.postcommit.add(
functools.partial(move_func, storage, sftp_filepath, sftp_destination_path)
)

def on_edi_exchange_done(self):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def on_edi_exchange_done(self):
def _storage_on_edi_exchange_done(self):

storage = self.storage_id
res = False
if self.direction == "input" and storage:
file = self.exchange_filename
pending_dir = self.type_id._storage_fullpath(
self.backend_id.input_dir_pending
).as_posix()
done_dir = self.type_id._storage_fullpath(
self.backend_id.input_dir_done
).as_posix()
error_dir = self.type_id._storage_fullpath(
self.backend_id.input_dir_error
).as_posix()
if not done_dir:
return res
res = self._move_file(storage, pending_dir, done_dir, file)
if not res:
# If a file previously failed it should have been previously
# moved to the error dir, therefore it is not present in the
# pending dir and we need to retry from error dir.
res = self._move_file(storage, error_dir, done_dir, file)
return res

def on_edi_exchange_error(self):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def on_edi_exchange_error(self):
def _storage_on_edi_exchange_error(self):

I'm not a fan of adding this custom logic to the record, having it on the component was better.
However, TBH, I have no better place to suggest 🤷

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree with you, I think it's much cleaner to keep using components and move that logic into a glue module, as I did in my first approach.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can, but at least we should prefix w/ proper naming (eg: _storage_*).

@etobella your POV?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@etobella friendly reminder!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we need to add a component, I prefer a glue module. I know it is not the best solution, but it is better to avoid dependancies that are not needed. About the naming, edi_storage_event or something like that looks fine for me.

storage = self.storage_id
res = False
if self.direction == "input" and storage:
file = self.exchange_filename
pending_dir = self.type_id._storage_fullpath(
self.backend_id.input_dir_pending
).as_posix()
error_dir = self.type_id._storage_fullpath(
self.backend_id.input_dir_error
).as_posix()
if error_dir:
res = self._move_file(storage, pending_dir, error_dir, file)
return res
30 changes: 12 additions & 18 deletions edi_storage_oca/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
<title>README.rst</title>
<title>EDI Storage backend support</title>
<style type="text/css">

/*
Expand Down Expand Up @@ -360,21 +360,16 @@
</style>
</head>
<body>
<div class="document">
<div class="document" id="edi-storage-backend-support">
<h1 class="title">EDI Storage backend support</h1>


<a class="reference external image-reference" href="https://odoo-community.org/get-involved?utm_source=readme">
<img alt="Odoo Community Association" src="https://odoo-community.org/readme-banner-image" />
</a>
<div class="section" id="edi-storage-backend-support">
<h1>EDI Storage backend support</h1>
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:ae52c0238c8bab278e4e2d6a48d86d4222672b893df1f171be046f3b7a3c58ae
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<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/license-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/edi-framework/tree/18.0/edi_storage_oca"><img alt="OCA/edi-framework" src="https://img.shields.io/badge/github-OCA%2Fedi--framework-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/edi-framework-18-0/edi-framework-18-0-edi_storage_oca"><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/edi-framework&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<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/edi-framework/tree/18.0/edi_storage_oca"><img alt="OCA/edi-framework" src="https://img.shields.io/badge/github-OCA%2Fedi--framework-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/edi-framework-18-0/edi-framework-18-0-edi_storage_oca"><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/edi-framework&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>Allow exchange files using storage backends from OCA/storage.</p>
<p>This module adds a storage backend relation on the EDI backend. There
you can configure the backend to be used (most often and SFTP) and the
Expand Down Expand Up @@ -411,34 +406,34 @@ <h1>EDI Storage backend support</h1>
</ul>
</div>
<div class="section" id="usage">
<h2><a class="toc-backref" href="#toc-entry-1">Usage</a></h2>
<h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
<p>Go to “EDI -&gt; EDI backend” then configure your backend to use a storage
backend.</p>
</div>
<div class="section" id="known-issues-roadmap">
<h2><a class="toc-backref" href="#toc-entry-2">Known issues / Roadmap</a></h2>
<h1><a class="toc-backref" href="#toc-entry-2">Known issues / Roadmap</a></h1>
<ul class="simple">
<li>clean deprecated methods in the storage</li>
</ul>
</div>
<div class="section" id="bug-tracker">
<h2><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h2>
<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/edi-framework/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/edi-framework/issues/new?body=module:%20edi_storage_oca%0Aversion:%2018.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">
<h2><a class="toc-backref" href="#toc-entry-4">Credits</a></h2>
<h1><a class="toc-backref" href="#toc-entry-4">Credits</a></h1>
<div class="section" id="authors">
<h3><a class="toc-backref" href="#toc-entry-5">Authors</a></h3>
<h2><a class="toc-backref" href="#toc-entry-5">Authors</a></h2>
<ul class="simple">
<li>ACSONE</li>
</ul>
</div>
<div class="section" id="contributors">
<h3><a class="toc-backref" href="#toc-entry-6">Contributors</a></h3>
<h2><a class="toc-backref" href="#toc-entry-6">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>Foram Shah &lt;<a class="reference external" href="mailto:foram.shah&#64;initos.com">foram.shah&#64;initos.com</a>&gt;</li>
Expand All @@ -451,12 +446,12 @@ <h3><a class="toc-backref" href="#toc-entry-6">Contributors</a></h3>
</ul>
</div>
<div class="section" id="other-credits">
<h3><a class="toc-backref" href="#toc-entry-7">Other credits</a></h3>
<h2><a class="toc-backref" href="#toc-entry-7">Other credits</a></h2>
<p>The migration of this module from 15.0 to 16.0 was financially supported
by Camptocamp.</p>
</div>
<div class="section" id="maintainers">
<h3><a class="toc-backref" href="#toc-entry-8">Maintainers</a></h3>
<h2><a class="toc-backref" href="#toc-entry-8">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" />
Expand All @@ -469,6 +464,5 @@ <h3><a class="toc-backref" href="#toc-entry-8">Maintainers</a></h3>
</div>
</div>
</div>
</div>
</body>
</html>
1 change: 1 addition & 0 deletions edi_storage_oca/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import test_edi_backend_storage
from . import test_exchange_type
from . import test_edi_event_listenner
Loading
Loading