mirror of
https://github.com/ynput/ayon-core.git
synced 2025-12-24 21:04:40 +01:00
added helper class 'WebactionContext' to pass webaction info
This commit is contained in:
parent
ea5d5cfc71
commit
1f716ce103
4 changed files with 105 additions and 157 deletions
|
|
@ -2,6 +2,7 @@ from __future__ import annotations
|
|||
|
||||
import copy
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional, Any
|
||||
|
||||
from ayon_core.tools.common_models import (
|
||||
|
|
@ -12,6 +13,18 @@ from ayon_core.tools.common_models import (
|
|||
TaskTypeItem,
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class WebactionContext:
|
||||
"""Context used for methods related to webactions."""
|
||||
identifier: str
|
||||
project_name: str
|
||||
folder_id: str
|
||||
task_id: str
|
||||
addon_name: str
|
||||
addon_version: str
|
||||
|
||||
|
||||
class ActionItem:
|
||||
"""Item representing single action to trigger.
|
||||
|
||||
|
|
@ -417,25 +430,15 @@ class AbstractLauncherFrontEnd(AbstractLauncherCommon):
|
|||
@abstractmethod
|
||||
def trigger_webaction(
|
||||
self,
|
||||
identifier,
|
||||
project_name,
|
||||
folder_id,
|
||||
task_id,
|
||||
action_label,
|
||||
addon_name,
|
||||
addon_version,
|
||||
form_data=None,
|
||||
context: WebactionContext,
|
||||
action_label: str,
|
||||
form_data: Optional[dict[str, Any]] = None,
|
||||
):
|
||||
"""Trigger action on given context.
|
||||
|
||||
Args:
|
||||
identifier (str): Action identifier.
|
||||
project_name (Union[str, None]): Project name.
|
||||
folder_id (Union[str, None]): Folder id.
|
||||
task_id (Union[str, None]): Task id.
|
||||
context (WebactionContext): Webaction context.
|
||||
action_label (str): Action label.
|
||||
addon_name (str): Addon name.
|
||||
addon_version (str): Addon version.
|
||||
form_data (Optional[dict[str, Any]]): Form values of action.
|
||||
|
||||
"""
|
||||
|
|
@ -443,27 +446,32 @@ class AbstractLauncherFrontEnd(AbstractLauncherCommon):
|
|||
|
||||
@abstractmethod
|
||||
def get_action_config_values(
|
||||
self,
|
||||
action_id,
|
||||
project_name,
|
||||
folder_id,
|
||||
task_id,
|
||||
addon_name,
|
||||
addon_version,
|
||||
):
|
||||
self, context: WebactionContext
|
||||
) -> dict[str, Any]:
|
||||
"""Get action config values.
|
||||
|
||||
Args:
|
||||
context (WebactionContext): Webaction context.
|
||||
|
||||
Returns:
|
||||
dict[str, Any]: Action config values.
|
||||
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def set_action_config_values(
|
||||
self,
|
||||
action_id,
|
||||
project_name,
|
||||
folder_id,
|
||||
task_id,
|
||||
addon_name,
|
||||
addon_version,
|
||||
values,
|
||||
context: WebactionContext,
|
||||
values: dict[str, Any],
|
||||
):
|
||||
"""Set action config values.
|
||||
|
||||
Args:
|
||||
context (WebactionContext): Webaction context.
|
||||
values (dict[str, Any]): Action config values.
|
||||
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
|
|
|
|||
|
|
@ -149,65 +149,16 @@ class BaseLauncherController(
|
|||
task_id,
|
||||
)
|
||||
|
||||
def trigger_webaction(
|
||||
self,
|
||||
identifier,
|
||||
project_name,
|
||||
folder_id,
|
||||
task_id,
|
||||
action_label,
|
||||
addon_name,
|
||||
addon_version,
|
||||
form_data=None,
|
||||
):
|
||||
def trigger_webaction(self, context, action_label, form_data=None):
|
||||
self._actions_model.trigger_webaction(
|
||||
identifier,
|
||||
project_name,
|
||||
folder_id,
|
||||
task_id,
|
||||
action_label,
|
||||
addon_name,
|
||||
addon_version,
|
||||
form_data,
|
||||
context, action_label, form_data
|
||||
)
|
||||
|
||||
def get_action_config_values(
|
||||
self,
|
||||
action_id,
|
||||
project_name,
|
||||
folder_id,
|
||||
task_id,
|
||||
addon_name,
|
||||
addon_version,
|
||||
):
|
||||
return self._actions_model.get_action_config_values(
|
||||
action_id,
|
||||
project_name,
|
||||
folder_id,
|
||||
task_id,
|
||||
addon_name,
|
||||
addon_version,
|
||||
)
|
||||
def get_action_config_values(self, context):
|
||||
return self._actions_model.get_action_config_values(context)
|
||||
|
||||
def set_action_config_values(
|
||||
self,
|
||||
action_id,
|
||||
project_name,
|
||||
folder_id,
|
||||
task_id,
|
||||
addon_name,
|
||||
addon_version,
|
||||
values,
|
||||
):
|
||||
return self._actions_model.set_action_config_values(
|
||||
action_id,
|
||||
project_name,
|
||||
folder_id,
|
||||
task_id,
|
||||
addon_name,
|
||||
addon_version,
|
||||
values,
|
||||
)
|
||||
def set_action_config_values(self, context, values):
|
||||
return self._actions_model.set_action_config_values(context, values)
|
||||
|
||||
# General methods
|
||||
def refresh(self):
|
||||
|
|
|
|||
|
|
@ -21,8 +21,7 @@ from ayon_core.pipeline.actions import (
|
|||
LauncherActionSelection,
|
||||
register_launcher_action_path,
|
||||
)
|
||||
|
||||
from ayon_core.tools.launcher.abstract import ActionItem
|
||||
from ayon_core.tools.launcher.abstract import ActionItem, WebactionContext
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
@ -202,19 +201,16 @@ class ActionsModel:
|
|||
}
|
||||
)
|
||||
|
||||
def trigger_webaction(
|
||||
self,
|
||||
identifier,
|
||||
project_name,
|
||||
folder_id,
|
||||
task_id,
|
||||
action_label,
|
||||
addon_name,
|
||||
addon_version,
|
||||
form_data,
|
||||
):
|
||||
def trigger_webaction(self, context, action_label, form_data):
|
||||
entity_type = None
|
||||
entity_ids = []
|
||||
identifier = context.identifier
|
||||
folder_id = context.folder_id
|
||||
task_id = context.task_id
|
||||
project_name = context.project_name
|
||||
addon_name = context.addon_name
|
||||
addon_version = context.addon_version
|
||||
|
||||
if task_id:
|
||||
entity_type = "task"
|
||||
entity_ids.append(task_id)
|
||||
|
|
@ -229,13 +225,13 @@ class ActionsModel:
|
|||
"variant": self._variant,
|
||||
}
|
||||
url = f"actions/execute?{urlencode(query)}"
|
||||
context = {
|
||||
request_data = {
|
||||
"projectName": project_name,
|
||||
"entityType": entity_type,
|
||||
"entityIds": entity_ids,
|
||||
}
|
||||
if form_data is not None:
|
||||
context["formData"] = form_data
|
||||
request_data["formData"] = form_data
|
||||
|
||||
trigger_id = uuid.uuid4().hex
|
||||
failed = False
|
||||
|
|
@ -257,7 +253,9 @@ class ActionsModel:
|
|||
headers = None
|
||||
else:
|
||||
headers["referer"] = conn.get_base_url()
|
||||
response = ayon_api.raw_post(url, headers=headers, json=context)
|
||||
response = ayon_api.raw_post(
|
||||
url, headers=headers, json=request_data
|
||||
)
|
||||
response.raise_for_status()
|
||||
handle_response = self._handle_webaction_response(response.data)
|
||||
|
||||
|
|
@ -287,30 +285,24 @@ class ActionsModel:
|
|||
data,
|
||||
)
|
||||
|
||||
def get_action_config_values(
|
||||
self,
|
||||
identifier,
|
||||
project_name,
|
||||
folder_id,
|
||||
task_id,
|
||||
addon_name,
|
||||
addon_version,
|
||||
):
|
||||
selection = self._prepare_selection(project_name, folder_id, task_id)
|
||||
def get_action_config_values(self, context: WebactionContext):
|
||||
selection = self._prepare_selection(
|
||||
context.project_name, context.folder_id, context.task_id
|
||||
)
|
||||
if not selection.is_project_selected:
|
||||
return {}
|
||||
|
||||
context = self._get_webaction_context(selection)
|
||||
request_data = self._get_webaction_request_data(selection)
|
||||
|
||||
query = {
|
||||
"addonName": addon_name,
|
||||
"addonVersion": addon_version,
|
||||
"identifier": identifier,
|
||||
"addonName": context.addon_name,
|
||||
"addonVersion": context.addon_version,
|
||||
"identifier": context.identifier,
|
||||
"variant": self._variant,
|
||||
}
|
||||
url = f"actions/config?{urlencode(query)}"
|
||||
try:
|
||||
response = ayon_api.post(url, **context)
|
||||
response = ayon_api.post(url, **request_data)
|
||||
response.raise_for_status()
|
||||
except Exception:
|
||||
self.log.warning(
|
||||
|
|
@ -320,32 +312,25 @@ class ActionsModel:
|
|||
return {}
|
||||
return response.data
|
||||
|
||||
def set_action_config_values(
|
||||
self,
|
||||
identifier,
|
||||
project_name,
|
||||
folder_id,
|
||||
task_id,
|
||||
addon_name,
|
||||
addon_version,
|
||||
values,
|
||||
):
|
||||
selection = self._prepare_selection(project_name, folder_id, task_id)
|
||||
def set_action_config_values(self, context, values):
|
||||
selection = self._prepare_selection(
|
||||
context.project_name, context.folder_id, context.task_id
|
||||
)
|
||||
if not selection.is_project_selected:
|
||||
return {}
|
||||
|
||||
context = self._get_webaction_context(selection)
|
||||
context["value"] = values
|
||||
request_data = self._get_webaction_request_data(selection)
|
||||
request_data["value"] = values
|
||||
|
||||
query = {
|
||||
"addonName": addon_name,
|
||||
"addonVersion": addon_version,
|
||||
"identifier": identifier,
|
||||
"addonName": context.addon_name,
|
||||
"addonVersion": context.addon_version,
|
||||
"identifier": context.identifier,
|
||||
"variant": self._variant,
|
||||
}
|
||||
url = f"actions/config?{urlencode(query)}"
|
||||
try:
|
||||
response = ayon_api.post(url, **context)
|
||||
response = ayon_api.post(url, **request_data)
|
||||
response.raise_for_status()
|
||||
except Exception:
|
||||
self.log.warning(
|
||||
|
|
@ -371,7 +356,7 @@ class ActionsModel:
|
|||
project_settings=project_settings,
|
||||
)
|
||||
|
||||
def _get_webaction_context(self, selection: LauncherActionSelection):
|
||||
def _get_webaction_request_data(self, selection: LauncherActionSelection):
|
||||
if not selection.is_project_selected:
|
||||
return None
|
||||
|
||||
|
|
@ -404,18 +389,18 @@ class ActionsModel:
|
|||
if not selection.is_project_selected:
|
||||
return []
|
||||
|
||||
context = self._get_webaction_context(selection)
|
||||
request_data = self._get_webaction_request_data(selection)
|
||||
project_name = selection.project_name
|
||||
entity_id = None
|
||||
if context["entityIds"]:
|
||||
entity_id = context["entityIds"][0]
|
||||
if request_data["entityIds"]:
|
||||
entity_id = request_data["entityIds"][0]
|
||||
|
||||
cache: CacheItem = self._webaction_items[project_name][entity_id]
|
||||
if cache.is_valid:
|
||||
return cache.get_data()
|
||||
|
||||
try:
|
||||
response = ayon_api.post("actions/list", **context)
|
||||
response = ayon_api.post("actions/list", **request_data)
|
||||
response.raise_for_status()
|
||||
except Exception:
|
||||
self.log.warning("Failed to collect webactions.", exc_info=True)
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ from ayon_core.lib.attribute_definitions import (
|
|||
from ayon_core.tools.flickcharm import FlickCharm
|
||||
from ayon_core.tools.utils import get_qt_icon
|
||||
from ayon_core.tools.attribute_defs import AttributeDefinitionsDialog
|
||||
from ayon_core.tools.launcher.abstract import WebactionContext
|
||||
|
||||
from .resources import get_options_image_path
|
||||
|
||||
|
|
@ -428,13 +429,15 @@ class ActionsWidget(QtWidgets.QWidget):
|
|||
return
|
||||
form_data = dialog.get_values()
|
||||
self._controller.trigger_webaction(
|
||||
identifier,
|
||||
event["project_name"],
|
||||
event["folder_id"],
|
||||
event["task_id"],
|
||||
WebactionContext(
|
||||
identifier,
|
||||
event["project_name"],
|
||||
event["folder_id"],
|
||||
event["task_id"],
|
||||
event["addon_name"],
|
||||
event["addon_version"],
|
||||
),
|
||||
event["action_label"],
|
||||
event["addon_name"],
|
||||
event["addon_version"],
|
||||
form_data,
|
||||
)
|
||||
|
||||
|
|
@ -502,12 +505,20 @@ class ActionsWidget(QtWidgets.QWidget):
|
|||
addon_name = index.data(ACTION_ADDON_NAME_ROLE)
|
||||
addon_version = index.data(ACTION_ADDON_VERSION_ROLE)
|
||||
|
||||
args = [action_id, project_name, folder_id, task_id]
|
||||
if action_type == "webaction":
|
||||
args.extend([action_label, addon_name, addon_version])
|
||||
self._controller.trigger_webaction(*args)
|
||||
context = WebactionContext(
|
||||
action_id,
|
||||
project_name,
|
||||
folder_id,
|
||||
task_id,
|
||||
addon_name,
|
||||
addon_version
|
||||
)
|
||||
self._controller.trigger_webaction(context, action_label)
|
||||
else:
|
||||
self._controller.trigger_action(*args)
|
||||
self._controller.trigger_action(
|
||||
action_id, project_name, folder_id, task_id
|
||||
)
|
||||
|
||||
self._start_animation(index)
|
||||
|
||||
|
|
@ -547,7 +558,7 @@ class ActionsWidget(QtWidgets.QWidget):
|
|||
task_id = self._model.get_selected_task_id()
|
||||
addon_name = index.data(ACTION_ADDON_NAME_ROLE)
|
||||
addon_version = index.data(ACTION_ADDON_VERSION_ROLE)
|
||||
values = self._controller.get_action_config_values(
|
||||
context = WebactionContext(
|
||||
action_id,
|
||||
project_name=project_name,
|
||||
folder_id=folder_id,
|
||||
|
|
@ -555,6 +566,7 @@ class ActionsWidget(QtWidgets.QWidget):
|
|||
addon_name=addon_name,
|
||||
addon_version=addon_version,
|
||||
)
|
||||
values = self._controller.get_action_config_values(context)
|
||||
|
||||
dialog = self._create_attrs_dialog(
|
||||
config_fields,
|
||||
|
|
@ -567,15 +579,7 @@ class ActionsWidget(QtWidgets.QWidget):
|
|||
if result != QtWidgets.QDialog.Accepted:
|
||||
return
|
||||
new_values = dialog.get_values()
|
||||
self._controller.set_action_config_values(
|
||||
action_id,
|
||||
project_name=project_name,
|
||||
folder_id=folder_id,
|
||||
task_id=task_id,
|
||||
addon_name=addon_name,
|
||||
addon_version=addon_version,
|
||||
values=new_values,
|
||||
)
|
||||
self._controller.set_action_config_values(context, new_values)
|
||||
|
||||
def _create_attrs_dialog(
|
||||
self,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue