Skip to content

Commit ed161b3

Browse files
[FIX] edi_storage_oca: add event listener
1 parent 26aeaf0 commit ed161b3

4 files changed

Lines changed: 81 additions & 1 deletion

File tree

edi_storage_oca/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"license": "LGPL-3",
1313
"website": "https://github.com/OCA/edi-framework",
1414
"author": "ACSONE,Odoo Community Association (OCA)",
15-
"depends": ["edi_core_oca", "fs_storage"],
15+
"depends": ["edi_core_oca", "fs_storage", "edi_component_oca"],
1616
"data": [
1717
"data/cron.xml",
1818
"security/ir_model_access.xml",

edi_storage_oca/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
from . import edi_exchange_type
33
from . import edi_exchange_record
44
from . import edi_oca_storage_handler
5+
from . import edi_event_listener
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Copyright 2021 ForgeFlow S.L. (https://www.forgeflow.com)
2+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
3+
4+
import functools
5+
import os
6+
from pathlib import PurePath
7+
8+
from odoo.addons.component.core import Component
9+
10+
from .. import utils
11+
12+
13+
class EdiStorageListener(Component):
14+
_name = "edi.storage.component.listener"
15+
_inherit = "base.event.listener"
16+
17+
def _move_file(self, storage, from_dir_str, to_dir_str, filename):
18+
from_dir = PurePath(from_dir_str)
19+
to_dir = PurePath(to_dir_str)
20+
# - storage.list_files now includes path in fs_storage, breaking change
21+
# - we remove path
22+
files = utils.list_files(storage, from_dir.as_posix())
23+
files = [os.path.basename(f) for f in files]
24+
if filename not in files:
25+
return False
26+
self._add_post_commit_hook(
27+
utils.move_files,
28+
storage,
29+
[(from_dir / filename).as_posix()],
30+
to_dir.as_posix(),
31+
)
32+
return True
33+
34+
def _add_post_commit_hook(
35+
self, move_func, storage, sftp_filepath, sftp_destination_path
36+
):
37+
"""Add hook after commit to move the file when transaction is over."""
38+
self.env.cr.postcommit.add(
39+
functools.partial(move_func, storage, sftp_filepath, sftp_destination_path)
40+
)
41+
42+
def on_edi_exchange_done(self, record):
43+
storage = record.storage_id
44+
res = False
45+
if record.direction == "input" and storage:
46+
file = record.exchange_filename
47+
pending_dir = record.type_id._storage_fullpath(
48+
record.backend_id.input_dir_pending
49+
).as_posix()
50+
done_dir = record.type_id._storage_fullpath(
51+
record.backend_id.input_dir_done
52+
).as_posix()
53+
error_dir = record.type_id._storage_fullpath(
54+
record.backend_id.input_dir_error
55+
).as_posix()
56+
if not done_dir:
57+
return res
58+
res = self._move_file(storage, pending_dir, done_dir, file)
59+
if not res:
60+
# If a file previously failed it should have been previously
61+
# moved to the error dir, therefore it is not present in the
62+
# pending dir and we need to retry from error dir.
63+
res = self._move_file(storage, error_dir, done_dir, file)
64+
return res
65+
66+
def on_edi_exchange_error(self, record):
67+
storage = record.storage_id
68+
res = False
69+
if record.direction == "input" and storage:
70+
file = record.exchange_filename
71+
pending_dir = record.type_id._storage_fullpath(
72+
record.backend_id.input_dir_pending
73+
).as_posix()
74+
error_dir = record.type_id._storage_fullpath(
75+
record.backend_id.input_dir_error
76+
).as_posix()
77+
if error_dir:
78+
res = self._move_file(storage, pending_dir, error_dir, file)
79+
return res

edi_storage_oca/tests/test_edi_event_listener.py

Whitespace-only changes.

0 commit comments

Comments
 (0)