Skip to content
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
43 changes: 39 additions & 4 deletions fs_storage/models/fs_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ def __init__(self, env, ids=(), prefetch_ids=()):
"* List File : List all files from root directory",
)

is_cacheable = fields.Boolean(
help="If True, once instantiated, the filesystem will be cached and reused.\n"
"By default, the filesystem is cacheable but in some cases, like "
"when using OAuth2 authentication, the filesystem cannot be cached "
"because it depends on the user and the token can change.\n"
"In this case, you can set this field to False to avoid caching the "
"filesystem.",
default=True,
)

_sql_constraints = [
(
"code_uniq",
Expand Down Expand Up @@ -208,6 +218,23 @@ def get_by_code(self, code) -> FSStorage:
res = self.browse(res_id)
return res

@api.model
@tools.ormcache("code")
def get_protocol_by_code(self, code):
record = self.get_by_code(code)
return record.protocol if record else None

@api.model
@tools.ormcache("code")
def _is_fs_cacheable(self, code):
"""Return True if the filesystem is cacheable."""
# This method is used to check if the filesystem is cacheable.
# It is used to avoid caching filesystems that are not cacheable.
# For example, the msgd protocol is not cacheable because it uses
# OAuth2 authentication and the token can change.
fs_storage = self.get_by_code(code)
return fs_storage and fs_storage.is_cacheable

@api.model
@tools.ormcache()
def get_storage_codes(self):
Expand All @@ -216,15 +243,23 @@ def get_storage_codes(self):

@api.model
@tools.ormcache("code")
def get_fs_by_code(self, code):
def _get_fs_by_code_from_cache(self, code):
return self.get_fs_by_code(code, force_no_cache=True)

@api.model
def get_fs_by_code(self, code, force_no_cache=False):
"""Return the filesystem associated to the given code.

:param code: the code of the filesystem
"""
use_cache = not force_no_cache and self._is_fs_cacheable(code)
fs = None
fs_storage = self.get_by_code(code)
if fs_storage:
fs = fs_storage.fs
if use_cache:
fs = self._get_fs_by_code_from_cache(code)
else:
fs_storage = self.get_by_code(code)
if fs_storage:
fs = fs_storage.fs
return fs

def copy(self, default=None):
Expand Down
1 change: 1 addition & 0 deletions fs_storage/views/fs_storage_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<group>
<field name="code" />
<field name="protocol" />
<field name="is_cacheable" />
<field name="directory_path" />
<field name="eval_options_from_env" />
<field
Expand Down