|
7 | 7 | from typing import Dict |
8 | 8 | from typing import Generator |
9 | 9 | from typing import List |
| 10 | +from typing import Optional |
10 | 11 | from typing import Union |
11 | 12 |
|
12 | 13 | import lib.core as constants |
|
20 | 21 | from lib.core.entities import ProjectEntity |
21 | 22 | from lib.core.entities import VideoEntity |
22 | 23 | from lib.core.entities.items import MultiModalItemEntity |
| 24 | +from lib.core.entities.items import ProjectCategoryEntity |
23 | 25 | from lib.core.exceptions import AppException |
24 | 26 | from lib.core.exceptions import AppValidationException |
25 | 27 | from lib.core.exceptions import BackendError |
|
38 | 40 | from lib.infrastructure.utils import extract_project_folder |
39 | 41 | from typing_extensions import Literal |
40 | 42 |
|
| 43 | + |
41 | 44 | logger = logging.getLogger("sa") |
42 | 45 |
|
43 | 46 |
|
@@ -1272,3 +1275,51 @@ def execute( |
1272 | 1275 | # returning control to the interface function that called it. So no need for |
1273 | 1276 | # error handling in the response |
1274 | 1277 | return self._response |
| 1278 | + |
| 1279 | + |
| 1280 | +class AttacheDetachItemsCategoryUseCase(BaseUseCase): |
| 1281 | + CHUNK_SIZE = 2000 |
| 1282 | + |
| 1283 | + def __init__( |
| 1284 | + self, |
| 1285 | + project: ProjectEntity, |
| 1286 | + folder: FolderEntity, |
| 1287 | + items: List[MultiModalItemEntity], |
| 1288 | + service_provider: BaseServiceProvider, |
| 1289 | + operation: Literal["attach", "detach"], |
| 1290 | + category: Optional[ProjectCategoryEntity] = None, |
| 1291 | + ): |
| 1292 | + super().__init__() |
| 1293 | + self._project = project |
| 1294 | + self._folder = folder |
| 1295 | + self._items = items |
| 1296 | + self._category = category |
| 1297 | + self._operation = operation |
| 1298 | + self._service_provider = service_provider |
| 1299 | + |
| 1300 | + def execute(self): |
| 1301 | + if self._operation == "attach": |
| 1302 | + success_count = 0 |
| 1303 | + for chunk in divide_to_chunks(self._items, self.CHUNK_SIZE): |
| 1304 | + item_id_category_id_map: Dict[int, int] = { |
| 1305 | + i.id: self._category.id for i in chunk |
| 1306 | + } |
| 1307 | + response = self._service_provider.items.bulk_attach_categories( |
| 1308 | + project_id=self._project.id, |
| 1309 | + folder_id=self._folder.id, |
| 1310 | + item_id_category_id_map=item_id_category_id_map, |
| 1311 | + ) |
| 1312 | + success_count += len(response.data) |
| 1313 | + logger.info( |
| 1314 | + f"{self._category.name} category successfully added to {success_count} items." |
| 1315 | + ) |
| 1316 | + elif self._operation == "detach": |
| 1317 | + success_count = 0 |
| 1318 | + for chunk in divide_to_chunks(self._items, self.CHUNK_SIZE): |
| 1319 | + response = self._service_provider.items.bulk_detach_categories( |
| 1320 | + project_id=self._project.id, |
| 1321 | + folder_id=self._folder.id, |
| 1322 | + item_ids=[i.id for i in chunk], |
| 1323 | + ) |
| 1324 | + success_count += len(response.data) |
| 1325 | + logger.info(f"Category successfully removed from {success_count} items.") |
0 commit comments