Commit b08df235 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[backend][assetmodel] Correctly handle assets not associated with user

This is valid for assets like database and protocol template
parent 0a466f6c
......@@ -128,47 +128,63 @@ class AssetModel(QStringListModel):
if not self.__prefix_path or self.__asset_type == AssetType.UNKNOWN:
return
asset_folder = os.path.join(
self.__prefix_path, AssetType.path(self.__asset_type)
)
asset_users = os.scandir(asset_folder)
def _find_json_files(path):
"""Return all json files from folder sorted"""
asset_items = os.scandir(path)
json_files = sorted(
[
item.name
for item in asset_items
if item.is_file() and item.name.endswith("json")
]
)
return json_files
latest_assets_list = []
for asset_user in asset_users:
for asset_folder in os.scandir(asset_user):
if self.asset_type == AssetType.EXPERIMENT:
for root, dirs, files in os.walk(asset_folder, topdown=False):
if dirs:
continue
anchor = "experiments/"
position = root.index(anchor) + len(anchor)
experiment_path = root[position:]
for json_file in [
file for file in files if file.endswith("json")
]:
if self.asset_type in [AssetType.DATABASE, AssetType.PROTOCOLTEMPLATE]:
# These assets have no user associated with them
for asset_folder in os.scandir(self.asset_folder):
json_files = _find_json_files(asset_folder)
if json_files:
latest_assets_list.append(
"{name}/{version}".format(
name=asset_folder.name, version=json_files[-1].split(".")[0]
)
)
else:
# Assets belonging to a user
asset_users = os.scandir(self.asset_folder)
for asset_user in asset_users:
for asset_folder in os.scandir(asset_user):
if self.asset_type == AssetType.EXPERIMENT:
for root, dirs, files in os.walk(asset_folder, topdown=False):
if dirs:
continue
anchor = "experiments/"
position = root.index(anchor) + len(anchor)
experiment_path = root[position:]
for json_file in [
file for file in files if file.endswith("json")
]:
latest_assets_list.append(
"{experiment_path}/{name}".format(
experiment_path=experiment_path,
name=json_file.split(".")[0],
)
)
else:
json_files = _find_json_files(asset_folder)
if json_files:
latest_assets_list.append(
"{experiment_path}/{name}".format(
experiment_path=experiment_path,
name=json_file.split(".")[0],
"{user}/{name}/{version}".format(
user=asset_user.name,
name=asset_folder.name,
version=json_files[-1].split(".")[0],
)
)
else:
asset_items = os.scandir(asset_folder)
json_files = sorted(
[
item.name
for item in asset_items
if item.is_file() and item.name.endswith("json")
]
)
if json_files:
latest_assets_list.append(
"{user}/{name}/{version}".format(
user=asset_user.name,
name=asset_folder.name,
version=json_files[-1].split(".")[0],
)
)
latest_assets_list = sorted(latest_assets_list)
if self.asset_type == AssetType.DATAFORMAT:
......@@ -225,3 +241,9 @@ class AssetModel(QStringListModel):
prefix_path = pyqtProperty(
str, fget=prefixPath, fset=setPrefixPath, notify=prefixPathChanged
)
@property
def asset_folder(self):
"""Returns the folder matching this model asset type"""
return os.path.join(self.prefix_path, AssetType.path(self.asset_type))
......@@ -29,30 +29,16 @@ from ..backend.assetmodel import AssetModel, AssetType
from ..utils import dataformat_basetypes
class TestAssetModel:
"""Test that the mock editor works correctly"""
def test_model_load(self, qtbot, test_prefix):
model = AssetModel()
asset_type = AssetType.DATAFORMAT
with qtbot.waitSignals(
[model.assetTypeChanged, model.prefixPathChanged]
) as blocker:
model.asset_type = asset_type
model.prefix_path = test_prefix
@pytest.fixture(params=[item for item in AssetType if item is not AssetType.UNKNOWN])
def asset_type(request):
return request.param
assert blocker.all_signals_and_args[0].args[0] == asset_type
assert blocker.all_signals_and_args[1].args[0] == test_prefix
asset_list = model.stringList()
basetypes = dataformat_basetypes()
for item in asset_list:
assert len(item.split("/")) == 3 or item in basetypes
class TestAssetModel:
"""Test that the mock editor works correctly"""
def test_experiment_model_load(self, qtbot, test_prefix):
def test_model_load(self, qtbot, test_prefix, asset_type):
model = AssetModel()
asset_type = AssetType.EXPERIMENT
with qtbot.waitSignals(
[model.assetTypeChanged, model.prefixPathChanged]
......@@ -64,9 +50,21 @@ class TestAssetModel:
assert blocker.all_signals_and_args[1].args[0] == test_prefix
asset_list = model.stringList()
for item in asset_list:
assert len(item.split("/")) == 5
assert len(asset_list) > 0
if asset_type == AssetType.DATAFORMAT:
basetypes = dataformat_basetypes()
for item in asset_list:
assert len(item.split("/")) == 3 or item in basetypes
else:
if asset_type == AssetType.EXPERIMENT:
split_size = 5
elif asset_type in [AssetType.DATABASE, AssetType.PROTOCOLTEMPLATE]:
split_size = 2
else:
split_size = 3
for item in asset_list:
assert len(item.split("/")) == split_size
class TestAssetType:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment