|
5 | 5 | import logging |
6 | 6 | import os |
7 | 7 | import sys |
| 8 | +import typing |
8 | 9 | import warnings |
9 | 10 | from pathlib import Path |
10 | 11 | from typing import Any |
@@ -277,6 +278,55 @@ def get_team_metadata(self): |
277 | 278 | response = self.controller.get_team() |
278 | 279 | return TeamSerializer(response.data).serialize() |
279 | 280 |
|
| 281 | + def get_editor_context(self, project: Union[NotEmptyStr, int], component_id: str): |
| 282 | + """ |
| 283 | + Retrieves the editor context for a given project and component ID. |
| 284 | +
|
| 285 | + :param project: The identifier of the project, which can be a string or an integer representing the project ID. |
| 286 | + :type project: Union[str, int] |
| 287 | +
|
| 288 | + :param component_id: The ID of the component for which the context is to be retrieved. |
| 289 | + :type component_id: str |
| 290 | +
|
| 291 | + :return: The context associated with the `webComponent`. |
| 292 | + :rtype: Any |
| 293 | +
|
| 294 | + :raises AppException: If the project type is not `MULTIMODAL` or no `webComponent` context is found. |
| 295 | + """ |
| 296 | + |
| 297 | + def retrieve_context( |
| 298 | + component_data: List[dict], component_pk: str |
| 299 | + ) -> Tuple[bool, typing.Any]: |
| 300 | + for component in component_data: |
| 301 | + if ( |
| 302 | + component["type"] == "webComponent" |
| 303 | + and component["id"] == component_pk |
| 304 | + ): |
| 305 | + return True, component.get("context") |
| 306 | + if component["type"] == "group" and "children" in component: |
| 307 | + found, val = retrieve_context(component["children"], component_pk) |
| 308 | + if found: |
| 309 | + return found, val |
| 310 | + return False, None |
| 311 | + |
| 312 | + project = ( |
| 313 | + self.controller.get_project(project) |
| 314 | + if isinstance(project, int) |
| 315 | + else self.controller.get_project_by_id(project) |
| 316 | + ) |
| 317 | + if project.type != ProjectType.MULTIMODAL: |
| 318 | + raise AppException( |
| 319 | + "This function is only supported for Multimodal projects." |
| 320 | + ) |
| 321 | + |
| 322 | + editor_template = self.controller.projects.get_editor_template(project) |
| 323 | + components = editor_template.get("components", []) |
| 324 | + |
| 325 | + _found, _context = retrieve_context(components, component_id) |
| 326 | + if not _found: |
| 327 | + raise AppException("No component context found for project.") |
| 328 | + return _context |
| 329 | + |
280 | 330 | def search_team_contributors( |
281 | 331 | self, |
282 | 332 | email: EmailStr = None, |
|
0 commit comments