Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][PW] storage_backend: Port from 14.0 #406

Open
wants to merge 4 commits into
base: 16.0
Choose a base branch
from
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
9 changes: 9 additions & 0 deletions .oca/oca-port/blacklist/storage_backend.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"pull_requests": {
"orphaned_commits": "false-positive",
"78": "false-positive",
"97": "false-positive",
"106": "false-positive",
"298": "false-positive"
}
}
19 changes: 18 additions & 1 deletion storage_backend/components/filesystem_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
# @author Sébastien BEAU <sebastien.beau@akretion.com>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

import logging
import os
import shutil

from odoo import _
from odoo.exceptions import AccessError

from odoo.addons.component.core import Component

_logger = logging.getLogger(__name__)


def is_safe_path(basedir, path):
return os.path.realpath(path).startswith(basedir)
Expand Down Expand Up @@ -56,4 +60,17 @@

def delete(self, relative_path):
full_path = self._fullpath(relative_path)
os.remove(full_path)
try:
os.remove(full_path)
except FileNotFoundError:
_logger.warning("File not found in %s", full_path)

Check warning on line 66 in storage_backend/components/filesystem_adapter.py

View check run for this annotation

Codecov / codecov/patch

storage_backend/components/filesystem_adapter.py#L65-L66

Added lines #L65 - L66 were not covered by tests

def move_files(self, files, destination_path):
result = []

Check warning on line 69 in storage_backend/components/filesystem_adapter.py

View check run for this annotation

Codecov / codecov/patch

storage_backend/components/filesystem_adapter.py#L69

Added line #L69 was not covered by tests
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

Check warning on line 76 in storage_backend/components/filesystem_adapter.py

View check run for this annotation

Codecov / codecov/patch

storage_backend/components/filesystem_adapter.py#L72-L76

Added lines #L72 - L76 were not covered by tests
7 changes: 7 additions & 0 deletions storage_backend/models/storage_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@

def _compute_has_validation(self):
for rec in self:
if not rec.backend_type:
# with server_env
# this code can be triggered
# before a backend_type has been set
# get_adapter() can't work without backend_type
rec.has_validation = False
continue

Check warning on line 80 in storage_backend/models/storage_backend.py

View check run for this annotation

Codecov / codecov/patch

storage_backend/models/storage_backend.py#L79-L80

Added lines #L79 - L80 were not covered by tests
adapter = rec._get_adapter()
rec.has_validation = hasattr(adapter, "validate_config")

Expand Down
13 changes: 13 additions & 0 deletions storage_backend/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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 <sebastien.beau@akretion.com>
# 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
Loading