24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280 | class SyncsketchAddon(BaseServerAddon):
settings_model: Type[SyncsketchSettings] = SyncsketchSettings
async def get_simple_actions(
self,
project_name: str | None = None,
variant: str = "production",
) -> list["SimpleActionManifest"]:
if not project_name:
return []
return [
SimpleActionManifest(
label="Push to SyncSketch",
category="SyncSketch",
identifier="syncsketch.push",
entity_type="list",
entity_subtypes=["version"],
icon={
"type": "material-symbols",
"name": "publish",
"color": "#ffffff",
},
order=100,
),
SimpleActionManifest(
label="Pull from SyncSketch",
category="SyncSketch",
identifier="syncsketch.pull",
entity_type="list",
entity_subtypes=["version"],
icon={
"type": "material-symbols",
"name": "download",
"color": "#ffffff",
},
order=100,
),
]
async def execute_action(
self,
executor: "ActionExecutor",
) -> "ExecuteResponseModel":
context = executor.context
if context.project_name is None:
return await executor.get_simple_response(
"SyncSketch actions need project.",
success=False,
)
project_name = context.project_name
if context.entity_type != "list":
return await executor.get_simple_response(
"SyncSketch actions can only be executed on version lists.",
success=False,
)
if context.entity_ids is None:
return await executor.get_simple_response(
"No selection of lists provided.",
success=False,
)
list_entities = []
for list_id in context.entity_ids:
list_entity = await EntityList.load(project_name, list_id)
if list_entity.entity_type == "version":
list_entities.append(list_entity)
if not list_entities:
return await executor.get_simple_response(
"Selection does not contain version lists.",
success=False,
)
if executor.identifier == "syncsketch.push":
sketch_project_name, response = await self._push_prepare_data(
project_name, list_entities, executor
)
if response is not None:
return response
topic = "syncsketch.push.review"
description = (
"Push to SyncSketch can take a while."
" Please wait until the action is finished."
)
elif executor.identifier == "syncsketch.pull":
# Pull does not require a project name as each list either has it
# filled or current project should be used.
sketch_project_name = None
topic = "syncsketch.pull.review"
description = (
"Pull from SyncSketch can take a while."
" Please wait until the action is finished."
)
else:
return await executor.get_simple_response(
f"Unknown action identifier: {executor.identifier}",
success=False,
)
for list_entity in list_entities:
summary = {"listId": list_entity.id}
if sketch_project_name:
summary["syncsketchProject"] = sketch_project_name
await dispatch_event(
topic,
project=project_name,
summary=summary,
finished=False,
description=description,
)
return await executor.get_simple_response(
"SyncSketch sync has been triggered",
success=True,
)
async def _push_prepare_data(
self,
project_name: str,
list_entities: list[EntityList],
executor: "ActionExecutor",
) -> tuple[str | None, ExecuteResponseModel | None]:
"""Prepare response or SyncSketch project name for sync action.
Based on settings can show a form. In case settings define that
current project should be used or if all selected list entities
already have filled project name then no form is showed and event
is triggered.
In case a response is returned, the project should be ignored. The
project is set to None if the selected project is the same as
the AYON project.
Args:
project_name (str): The AYON project name.
list_entities (list[EntityList]): List of selected version lists.
executor (ActionExecutor): The action executor instance.
Returns:
tuple[str | None, ExecuteResponseModel | None]: A tuple containing
SyncSketch project name or action response.
"""
selection = await self._prepare_push_project_selection(
project_name, list_entities, executor
)
if selection.selected_project_name:
output_project = selection.selected_project_name
if output_project.lower() == project_name.lower():
output_project = None
return output_project, None
options = [
FormSelectOption(
value=project_name,
label=project_name,
)
for project_name in selection.project_names
]
default = None
if options:
default = options[0]["value"]
if selection.allow_custom_name:
options.insert(0, FormSelectOption(
value="__custom__project_name__",
label="< Custom project name >",
))
if not options:
return None, await executor.get_simple_response(
(
"Settings is missing SyncSketch project names"
" to select from."
),
success=False,
)
if default is None:
default = options[0]["value"]
form = SimpleForm().select(
"project_name",
label="SyncSketch Project",
options=options,
value=default,
)
if selection.allow_custom_name:
form.text(
"custom_project_name",
label="Custom project name",
value="",
placeholder=(
"Select '< Custom project name >' to enter"
" a custom name"
),
)
return None, await executor.get_form_response(
title="Select SyncSketch Project",
fields=form,
submit_label="Confirm project",
)
async def _prepare_push_project_selection(
self,
project_name: str,
list_entities: list[EntityList],
executor: "ActionExecutor",
) -> ActionProjectSelection:
output = ActionProjectSelection()
form_data = executor.context.form_data
if form_data and form_data.get("project_name"):
project_name = form_data["project_name"]
if project_name == "__custom__project_name__":
project_name = form_data.get("custom_project_name", "").strip()
output.selected_project_name = project_name
return output
project_needed = False
for list_entity in list_entities:
syncsketch_meta = list_entity.payload.data.get("syncsketch")
if not syncsketch_meta or not syncsketch_meta.get("project"):
project_needed = True
break
if not project_needed:
output.selected_project_name = project_name
return output
settings: SyncsketchSettings | None = await self.get_project_settings(
project_name, variant=executor.variant
)
if settings is None:
return output
project_mapping = settings.sync.project_mapping
if project_mapping == "match":
output.project_names.append(project_name)
output.selected_project_name = project_name
elif project_mapping == "selection":
selection_settings = settings.sync.selection
output.project_names = list(selection_settings.project_names)
output.allow_custom_name = selection_settings.allow_custom_name
if (
len(output.project_names) == 1
and not selection_settings.allow_custom_name
):
output.selected_project_name = output.project_names[0]
return output
|