Skip to content

Commit

Permalink
[IMP] storage_backend: Add move_files for filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
rousseldenis authored and MiquelRForgeFlow committed Sep 12, 2023
1 parent 3c3d948 commit be8c16e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions storage_backend/components/filesystem_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import logging
import os
import shutil

from odoo import _
from odoo.exceptions import AccessError
Expand Down Expand Up @@ -63,3 +64,13 @@ def delete(self, relative_path):
os.remove(full_path)
except FileNotFoundError:
_logger.warning("File not found in %s", full_path)

def move_files(self, files, destination_path):
result = []
for file_path in files:
if not os.path.exists(destination_path):
os.makedirs(destination_path)
filename = os.path.basename(file_path)
destination_file = os.path.join(destination_path, filename)
result.append(shutil.move(file_path, destination_file))
return result
13 changes: 13 additions & 0 deletions storage_backend/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ def _test_find_files(
res = backend.find_files(pattern)
self.assertEqual(sorted(res), sorted(expected_filepaths))

def _test_move_files(
self,
backend,
adapter_dotted_path,
filename,
destination_path,
expected_filepaths,
):
with mock.patch(adapter_dotted_path + ".move_files") as mocked:
mocked.return_value = expected_filepaths
res = backend.move_files(filename, destination_path)
self.assertEqual(sorted(res), sorted(expected_filepaths))


class CommonCase(TransactionComponentCase):
@classmethod
Expand Down
10 changes: 10 additions & 0 deletions storage_backend/tests/test_filesystem.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2017 Akretion (http://www.akretion.com).
# @author Sébastien BEAU <[email protected]>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
import os

from odoo.exceptions import AccessError

Expand Down Expand Up @@ -29,6 +30,15 @@ def test_find_files(self):
backend, ADAPTER_PATH, mocked_filepaths, r".*\.good$", expected
)

def test_move_files(self):
backend = self.backend.sudo()
base_dir = backend._get_adapter()._basedir()
expected = [base_dir + "/" + self.filename]
destination_path = os.path.join(base_dir, "destination")
self._test_move_files(
backend, ADAPTER_PATH, self.filename, destination_path, expected
)


class FileSystemDemoUserAccessCase(CommonCase):
@classmethod
Expand Down

0 comments on commit be8c16e

Please sign in to comment.