Skip to content

Commit

Permalink
[IMP] fs_storage: compute options protocol
Browse files Browse the repository at this point in the history
Before the change, reading the options protocol might not be in sync
with the record protocol.
This is particularly the case if the record is save in the middle of editing
as the value resets, which can be quite confusing.
  • Loading branch information
len-foss committed Dec 14, 2023
1 parent 50d53d6 commit c9ccb24
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions fs_storage/models/fs_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,13 @@ def __init__(self, env, ids=(), prefetch_ids=()):
options_protocol = fields.Selection(
string="Describes Protocol",
selection="_get_options_protocol",
default="odoofs",
compute="_compute_protocol_descr",
help="The protocol used to access the content of filesystem.\n"
"This list is the one supported by the fsspec library (see "
"https://filesystem-spec.readthedocs.io/en/latest). A filesystem protocol"
"is added by default and refers to the odoo local filesystem.\n"
"Pay attention that according to the protocol, some options must be"
"provided through the options field.",
store=False,
)
options_properties = fields.Text(
string="Available properties",
Expand Down Expand Up @@ -236,6 +235,7 @@ def _inverse_json_options(self) -> None:
def _compute_protocol_descr(self) -> None:
for rec in self:
rec.protocol_descr = fsspec.get_filesystem_class(rec.protocol).__doc__
rec.options_protocol = rec.protocol

@api.model
def _get_options_protocol(self) -> list[tuple[str, str]]:
Expand Down
15 changes: 15 additions & 0 deletions fs_storage/tests/test_fs_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import warnings
from unittest import mock

from odoo.tests import Form
from odoo.tests.common import TransactionCase
from odoo.tools import mute_logger

Expand Down Expand Up @@ -151,3 +152,17 @@ def test_recursive_add_odoo_storage_path_to_options(self):
.get("target_options")
.get("odoo_storage_path"),
)

def test_interface_values(self):
protocol = "file" # should be available by default in the list of protocols
with Form(self.env["fs.storage"]) as new_storage:
new_storage.name = "Test storage"
new_storage.code = "code"
new_storage.protocol = protocol
self.assertEqual(new_storage.protocol, protocol)
# the options should follow the protocol
self.assertEqual(new_storage.options_protocol, protocol)
description = new_storage.protocol_descr
self.assertTrue("Interface to files on local storage" in description)
# this is still true after saving
self.assertEqual(new_storage.options_protocol, protocol)

0 comments on commit c9ccb24

Please sign in to comment.