Commit 80916b69 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[widgets][editor] Refactor new asset creation

The method now returns an Asset and either None
or the data to be used to load the asset editor.
parent 88276d59
......@@ -40,6 +40,7 @@ from PyQt5.QtWidgets import QVBoxLayout
from beat.backend.python import utils
from ..backend.asset import Asset
from ..backend.asset import AssetType
from .dialogs import AssetCreationDialog
......@@ -85,7 +86,7 @@ class AbstractAssetEditor(QWidget):
layout.addWidget(self.title_label)
layout.addWidget(self.information_group_box)
self.create_action.triggered.connect(self._createNewAsset)
# self.create_action.triggered.connect(self._onCreateActionTriggered)
self.description_lineedit.textChanged.connect(self.dataChanged)
self.dataChanged.connect(self.setDirty)
......@@ -108,61 +109,79 @@ class AbstractAssetEditor(QWidget):
widget.setVisible(visible)
self.information_layout.labelForField(widget).setVisible(visible)
@pyqtSlot()
def _createNewAsset(self):
"""Implement whatever is needed to create a new asset managed by this editor"""
def createNewAsset(self):
ok_pressed, creation_type, asset_info = AssetCreationDialog.getAssetInfo(
self, self.context, self.asset_type
)
if ok_pressed:
return self._createNewAsset(creation_type, asset_info)
return None, None
@pyqtSlot()
def _onCreateActionTriggered(self):
ok_pressed, creation_type, asset_info = AssetCreationDialog.getAssetInfo(
self, self.context, self.asset_type
)
if ok_pressed:
user = self.context.meta["config"].user
error_message = None
status = False
if creation_type == AssetCreationDialog.NEW:
if self.asset_type.split_count() == 1:
asset_name = f"{asset_info}/1"
else:
asset_name = f"{user}/{asset_info}/1"
try:
status = self.asset_type.create_new(self.prefix_path, asset_name)
except RuntimeError as error:
error_message = str(error)
elif creation_type == AssetCreationDialog.NEW_VERSION:
status = self.asset_type.create_new_version(
self.prefix_path, asset_info
)
self._createNewAsset(creation_type, asset_info)
def _createNewAsset(self, creation_type, asset_info):
"""Implement whatever is needed to create a new asset managed by this editor"""
user = self.context.meta["config"].user
error_message = None
status = False
if creation_type == AssetCreationDialog.NEW:
if self.asset_type.split_count() == 1:
asset_name = f"{asset_info}/1"
else:
asset_name = f"{user}/{asset_info}/1"
try:
status = self.asset_type.create_new(self.prefix_path, asset_name)
except RuntimeError as error:
error_message = str(error)
else:
source_asset, target_name = asset_info
if self.asset_type.split_count() == 1:
name, version = source_asset.split("/")
target_asset = f"{target_name}/{version}"
elif self.asset_type.split_count() == 2:
name, version = source_asset.split("/")[-2:]
target_asset = f"{user}/{target_name}/{version}"
else:
items = source_asset.split("/")
items[0] = user
items[4] = target_name
target_asset = "/".join(items)
status = self.asset_type.fork(
self.prefix_path, source_asset, target_asset
asset = Asset(self.prefix_path, self.asset_type, asset_name)
elif creation_type == AssetCreationDialog.NEW_VERSION:
status = self.asset_type.create_new_version(self.prefix_path, asset_info)
version_location = asset_info.rfind("/") + 1
asset_new_version = int(asset_info[version_location:]) + 1
asset_name = asset_info[:version_location] + str(asset_new_version)
asset = Asset(self.prefix_path, self.asset_type, asset_name)
else:
source_asset, target_name = asset_info
if self.asset_type.split_count() == 1:
name, version = source_asset.split("/")
target_asset = f"{target_name}/{version}"
elif self.asset_type.split_count() == 2:
name, version = source_asset.split("/")[-2:]
target_asset = f"{user}/{target_name}/{version}"
else:
items = source_asset.split("/")
items[0] = user
items[4] = target_name
target_asset = "/".join(items)
status = self.asset_type.fork(self.prefix_path, source_asset, target_asset)
asset = Asset(self.prefix_path, self.asset_type, target_asset)
if not status:
error_text = self.tr(
"The {} operation failed".format(
AssetCreationDialog.typeToString(creation_type)
)
)
if not status:
error_text = self.tr(
"The {} operation failed".format(
AssetCreationDialog.typeToString(creation_type)
)
)
if error_message:
error_text += self.tr("\nReason:{}".format(error_message))
if error_message:
error_text += self.tr("\nReason:{}".format(error_message))
QMessageBox.critical(self, self.tr("Error occurred"), error_text)
return None, None
QMessageBox.critical(self, self.tr("Error occurred"), error_text)
return asset, None
def _load_json(self, json_object):
"""To be implemented by subclass to load their specific JSON parts"""
......
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