|
| 1 | +# Copyright 2026 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 | +from odoo.addons.edi_storage_oca import utils |
| 10 | + |
| 11 | + |
| 12 | +class EdiStorageListener(Component): |
| 13 | + _name = "edi.storage.component.listener" |
| 14 | + _inherit = "base.event.listener" |
| 15 | + |
| 16 | + def _move_file(self, storage, from_dir_str, to_dir_str, filename): |
| 17 | + from_dir = PurePath(from_dir_str) |
| 18 | + to_dir = PurePath(to_dir_str) |
| 19 | + # - storage.list_files now includes path in fs_storage, breaking change |
| 20 | + # - we remove path |
| 21 | + files = utils.list_files(storage, from_dir.as_posix()) |
| 22 | + files = [os.path.basename(f) for f in files] |
| 23 | + if filename not in files: |
| 24 | + return False |
| 25 | + self._add_post_commit_hook( |
| 26 | + utils.move_files, |
| 27 | + storage, |
| 28 | + [(from_dir / filename).as_posix()], |
| 29 | + to_dir.as_posix(), |
| 30 | + ) |
| 31 | + return True |
| 32 | + |
| 33 | + def _add_post_commit_hook( |
| 34 | + self, move_func, storage, sftp_filepath, sftp_destination_path |
| 35 | + ): |
| 36 | + """Add hook after commit to move the file when transaction is over.""" |
| 37 | + self.env.cr.postcommit.add( |
| 38 | + functools.partial(move_func, storage, sftp_filepath, sftp_destination_path) |
| 39 | + ) |
| 40 | + |
| 41 | + def on_edi_exchange_done(self, record): |
| 42 | + storage = record.storage_id |
| 43 | + res = False |
| 44 | + if record.direction == "input" and storage: |
| 45 | + file = record.exchange_filename |
| 46 | + pending_dir = record.type_id._storage_fullpath( |
| 47 | + record.backend_id.input_dir_pending |
| 48 | + ).as_posix() |
| 49 | + done_dir = record.type_id._storage_fullpath( |
| 50 | + record.backend_id.input_dir_done |
| 51 | + ).as_posix() |
| 52 | + error_dir = record.type_id._storage_fullpath( |
| 53 | + record.backend_id.input_dir_error |
| 54 | + ).as_posix() |
| 55 | + if not done_dir: |
| 56 | + return res |
| 57 | + res = self._move_file(storage, pending_dir, done_dir, file) |
| 58 | + if not res: |
| 59 | + # If a file previously failed it should have been previously |
| 60 | + # moved to the error dir, therefore it is not present in the |
| 61 | + # pending dir and we need to retry from error dir. |
| 62 | + res = self._move_file(storage, error_dir, done_dir, file) |
| 63 | + return res |
| 64 | + |
| 65 | + def on_edi_exchange_error(self, record): |
| 66 | + storage = record.storage_id |
| 67 | + res = False |
| 68 | + if record.direction == "input" and storage: |
| 69 | + file = record.exchange_filename |
| 70 | + pending_dir = record.type_id._storage_fullpath( |
| 71 | + record.backend_id.input_dir_pending |
| 72 | + ).as_posix() |
| 73 | + error_dir = record.type_id._storage_fullpath( |
| 74 | + record.backend_id.input_dir_error |
| 75 | + ).as_posix() |
| 76 | + if error_dir: |
| 77 | + res = self._move_file(storage, pending_dir, error_dir, file) |
| 78 | + return res |
0 commit comments