Commit 425f0298 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[widgets][assetwidget] Refactor load and new asset creation

This allows to support edition of "not yet created" asset
like for example the experiment which uses a two steps
setup.

This new implementation also opens the newly created asset
for edition.

Fixes #226
parent 80916b69
......@@ -178,13 +178,13 @@ class TestAssetWidget:
assert json_data[description_key] == description
def test_new(self, qtbot, monkeypatch, test_prefix, beat_context, asset_type):
if asset_type.can_create():
asset_widget = AssetWidget()
qtbot.addWidget(asset_widget)
asset_widget.set_context(beat_context)
asset_name = "test_name"
user = beat_context.meta["config"].user
asset_widget = AssetWidget()
qtbot.addWidget(asset_widget)
asset_widget.set_context(beat_context)
asset_name = "test_name"
user = beat_context.meta["config"].user
if asset_type.can_create():
monkeypatch.setattr(
AssetCreationDialog,
"getAssetInfo",
......@@ -193,7 +193,7 @@ class TestAssetWidget:
),
)
asset_widget.editors_type[asset_type].create_action.trigger()
asset_widget.type_editor_map[asset_type].create_action.trigger()
asset_model = AssetModel()
asset_model.asset_type = asset_type
......@@ -205,6 +205,37 @@ class TestAssetWidget:
target_name = f"{asset_name}/1"
assert target_name in asset_model.stringList()
assert os.path.exists(asset_model.json_path(target_name))
elif asset_type == AssetType.EXPERIMENT:
toolchain_name = "user/two_loops/1"
monkeypatch.setattr(
AssetCreationDialog,
"getAssetInfo",
classmethod(
lambda *args, **kwargs: (
True,
AssetCreationDialog.NEW,
(toolchain_name, asset_name),
)
),
)
asset_widget.type_editor_map[asset_type].create_action.trigger()
asset_model = AssetModel()
asset_model.asset_type = asset_type
asset_model.prefix_path = test_prefix
target_name = f"{user}/{toolchain_name}/{asset_name}"
assert target_name not in asset_model.stringList()
with pytest.raises(ValueError):
assert not os.path.exists(asset_model.json_path(target_name))
assert asset_widget.save_button.isEnabled()
qtbot.mouseClick(asset_widget.save_button, QtCore.Qt.LeftButton)
asset_model.reload()
assert target_name in asset_model.stringList()
assert os.path.exists(asset_model.json_path(target_name))
def test_new_version(
self,
......@@ -233,7 +264,7 @@ class TestAssetWidget:
),
)
asset_widget.editors_type[asset_type].create_action.trigger()
asset_widget.type_editor_map[asset_type].create_action.trigger()
asset_model = AssetModel()
asset_model.asset_type = asset_type
......@@ -278,7 +309,7 @@ class TestAssetWidget:
),
)
asset_widget.editors_type[asset_type].create_action.trigger()
asset_widget.type_editor_map[asset_type].create_action.trigger()
asset_model = AssetModel()
asset_model.asset_type = asset_type
......
......@@ -25,7 +25,6 @@
import os
import click
import simplejson as json
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtCore import pyqtSlot
......@@ -145,12 +144,12 @@ class AssetWidget(QWidget):
self.json_widget.setReadOnly(True)
self.editors = QStackedWidget()
self.editors_type = {}
self.type_editor_map = {}
for asset_type in AssetType:
editor = widget_for_asset_type(asset_type)
self.editors.addWidget(editor)
self.editors_type[asset_type] = editor
self.type_editor_map[asset_type] = editor
self.tab_widget = QTabWidget()
self.tab_widget.addTab(self.editors, self.tr("Editor"))
......@@ -185,6 +184,9 @@ class AssetWidget(QWidget):
self.edit_code_action.triggered.connect(self.__editCode)
self.edit_documentation_action.triggered.connect(self.__editDocumentation)
for action in self.create_actions():
action.triggered.connect(self.__onCreateActionTriggered)
self.set_current_editor(AssetType.UNKNOWN)
@property
......@@ -196,7 +198,7 @@ class AssetWidget(QWidget):
def set_current_editor(self, asset_type):
"""Set the current editor"""
self.editors.setCurrentWidget(self.editors_type[asset_type])
self.editors.setCurrentWidget(self.type_editor_map[asset_type])
self.edit_code_action.setEnabled(asset_type.has_code())
self.edit_documentation_action.setEnabled(asset_type is not AssetType.UNKNOWN)
......@@ -206,7 +208,7 @@ class AssetWidget(QWidget):
return self.context.meta["config"].path
def __update_content(self):
def __update_content(self, json_data):
"""Update the content of this widget"""
try:
......@@ -215,13 +217,11 @@ class AssetWidget(QWidget):
# Nothing was connected yet
pass
with open(self.current_asset.declaration_path) as json_file:
json_data = json_file.read()
self.json_widget.setText(json_data)
editor = self.editors_type[self.current_asset.type]
editor.load_json(json.loads(json_data))
editor.dataChanged.connect(self.update_timer.start)
self.set_current_editor(self.current_asset.type)
editor = self.type_editor_map[self.current_asset.type]
editor.load_json(json_data)
editor.dataChanged.connect(self.update_timer.start)
self.set_current_editor(self.current_asset.type)
self.__updateJsonWidget()
self.asset_name_label.setText(self.current_asset.name)
self.save_button.setEnabled(False)
......@@ -253,7 +253,31 @@ class AssetWidget(QWidget):
)
if answer == QMessageBox.Yes:
self.__update_content()
self.__update_content(self.current_asset.declaration)
@pyqtSlot()
def __onCreateActionTriggered(self):
action = self.sender()
editor = [
editor
for editor in self.type_editor_map.values()
if editor.create_action == action
][0]
self.maybe_save()
asset, json_data = editor.createNewAsset()
if asset and json_data:
self.current_asset = asset
# There's no file created yet
self.__clear_watcher()
self.__update_content(json_data)
self.current_editor.setDirty()
self.save_button.setEnabled(True)
elif asset:
self.loadAsset(asset)
@pyqtSlot()
def __updateJsonWidget(self):
......@@ -317,7 +341,7 @@ class AssetWidget(QWidget):
def create_actions(self):
"""Return the creation actions of all editors"""
action_list = [editor.create_action for editor in self.editors_type.values()]
action_list = [editor.create_action for editor in self.type_editor_map.values()]
return filter(None, action_list)
@pyqtSlot("QString")
......@@ -380,8 +404,8 @@ class AssetWidget(QWidget):
self.tr(f"The asset you are trying to load is invalid\n{errors}"),
)
else:
declaration = asset.declaration
if asset.type == AssetType.ALGORITHM:
declaration = asset.declaration
if declaration.get("api_version", 0) < 2:
answer = QMessageBox.question(
self,
......@@ -407,7 +431,7 @@ class AssetWidget(QWidget):
self.watcher.addPath(asset.declaration_path)
self.current_asset = asset
self.__update_content()
self.__update_content(declaration)
self.currentAssetChanged.emit(asset)
@pyqtSlot()
......@@ -417,7 +441,14 @@ class AssetWidget(QWidget):
json_data = self.current_editor.dump_json()
# Currently merging until we have all editors implemented
original = self.current_asset.declaration
if os.path.exists(self.current_asset.declaration_path):
original = self.current_asset.declaration
else:
os.makedirs(
os.path.dirname(self.current_asset.declaration_path), exist_ok=True
)
original = {}
original.update(json_data)
declaration_path = self.current_asset.declaration_path
......
Supports Markdown
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