|
50 | 50 | from lib.core.entities.classes import AnnotationClassEntity |
51 | 51 | from lib.core.entities.classes import AttributeGroup |
52 | 52 | from lib.core.entities.integrations import IntegrationEntity |
| 53 | +from lib.core.entities.integrations import IntegrationTypeEnum |
53 | 54 | from lib.core.enums import ImageQuality |
54 | 55 | from lib.core.enums import ProjectType |
55 | 56 | from lib.core.enums import ClassTypeEnum |
|
64 | 65 | from lib.infrastructure.utils import extract_project_folder |
65 | 66 | from lib.infrastructure.validators import wrap_error |
66 | 67 |
|
67 | | - |
68 | 68 | logger = logging.getLogger("sa") |
69 | 69 |
|
70 | 70 | NotEmptyStr = TypeVar("NotEmptyStr", bound=constr(strict=True, min_length=1)) |
@@ -110,6 +110,7 @@ class PriorityScore(TypedDict): |
110 | 110 | class Attachment(TypedDict, total=False): |
111 | 111 | url: Required[str] # noqa |
112 | 112 | name: NotRequired[str] # noqa |
| 113 | + integration: NotRequired[str] # noqa |
113 | 114 |
|
114 | 115 |
|
115 | 116 | class SAClient(BaseInterfaceFacade, metaclass=TrackableMeta): |
@@ -2579,25 +2580,48 @@ def attach_items( |
2579 | 2580 | logger.info("Dropping duplicates.") |
2580 | 2581 | unique_attachments = parse_obj_as(List[AttachmentEntity], unique_attachments) |
2581 | 2582 | uploaded, fails, duplicated = [], [], [] |
2582 | | - if unique_attachments: |
| 2583 | + _unique_attachments = [] |
| 2584 | + if any(i.integration for i in unique_attachments): |
| 2585 | + integtation_item_map = { |
| 2586 | + i.name: i |
| 2587 | + for i in self.controller.integrations.list().data |
| 2588 | + if i.type == IntegrationTypeEnum.CUSTOM |
| 2589 | + } |
| 2590 | + invalid_integrations = set() |
| 2591 | + for attachment in unique_attachments: |
| 2592 | + if attachment.integration: |
| 2593 | + if attachment.integration in integtation_item_map: |
| 2594 | + attachment.integration_id = integtation_item_map[ |
| 2595 | + attachment.integration |
| 2596 | + ].id |
| 2597 | + else: |
| 2598 | + invalid_integrations.add(attachment.integration) |
| 2599 | + continue |
| 2600 | + _unique_attachments.append(attachment) |
| 2601 | + if invalid_integrations: |
| 2602 | + logger.error( |
| 2603 | + f"The ['{','.join(invalid_integrations)}'] integrations specified for the items doesn't exist in the " |
| 2604 | + "list of integrations on the platform. Any associated items will be skipped." |
| 2605 | + ) |
| 2606 | + if _unique_attachments: |
2583 | 2607 | logger.info( |
2584 | | - f"Attaching {len(unique_attachments)} file(s) to project {project}." |
| 2608 | + f"Attaching {len(_unique_attachments)} file(s) to project {project}." |
2585 | 2609 | ) |
2586 | 2610 | project, folder = self.controller.get_project_folder( |
2587 | 2611 | project_name, folder_name |
2588 | 2612 | ) |
2589 | 2613 | response = self.controller.items.attach( |
2590 | 2614 | project=project, |
2591 | 2615 | folder=folder, |
2592 | | - attachments=unique_attachments, |
| 2616 | + attachments=_unique_attachments, |
2593 | 2617 | annotation_status=annotation_status, |
2594 | 2618 | ) |
2595 | 2619 | if response.errors: |
2596 | 2620 | raise AppException(response.errors) |
2597 | 2621 | uploaded, duplicated = response.data |
2598 | 2622 | fails = [ |
2599 | 2623 | attachment.name |
2600 | | - for attachment in unique_attachments |
| 2624 | + for attachment in _unique_attachments |
2601 | 2625 | if attachment.name not in uploaded and attachment.name not in duplicated |
2602 | 2626 | ] |
2603 | 2627 | return uploaded, fails, duplicated |
|
0 commit comments