|
73 | 73 | from lib.app.serializers import WMProjectSerializer |
74 | 74 | from lib.core.entities.work_managament import WMUserTypeEnum |
75 | 75 | from lib.core.jsx_conditions import EmptyQuery |
| 76 | +from lib.core.entities.items import ProjectCategoryEntity |
| 77 | + |
76 | 78 |
|
77 | 79 | logger = logging.getLogger("sa") |
78 | 80 |
|
@@ -1194,6 +1196,154 @@ def clone_project( |
1194 | 1196 | ) |
1195 | 1197 | return data |
1196 | 1198 |
|
| 1199 | + def create_categories( |
| 1200 | + self, project: Union[NotEmptyStr, int], categories: List[str] |
| 1201 | + ): |
| 1202 | + """ |
| 1203 | + Create one or more categories in a project. |
| 1204 | +
|
| 1205 | + :param project: The name or ID of the project. |
| 1206 | + :type project: Union[NotEmptyStr, int] |
| 1207 | +
|
| 1208 | + :param categories: A list of categories to create |
| 1209 | + :type categories: list of str |
| 1210 | +
|
| 1211 | + Request Example: |
| 1212 | + :: |
| 1213 | +
|
| 1214 | + client.create_categories( |
| 1215 | + project="product-review-mm", |
| 1216 | + categories=["Shoes", "T-Shirt"] |
| 1217 | + ) |
| 1218 | + """ |
| 1219 | + project = ( |
| 1220 | + self.controller.get_project_by_id(project).data |
| 1221 | + if isinstance(project, int) |
| 1222 | + else self.controller.get_project(project) |
| 1223 | + ) |
| 1224 | + self.controller.check_multimodal_project_categorization(project) |
| 1225 | + |
| 1226 | + response = ( |
| 1227 | + self.controller.service_provider.work_management.create_project_categories( |
| 1228 | + project_id=project.id, categories=categories |
| 1229 | + ) |
| 1230 | + ) |
| 1231 | + logger.info( |
| 1232 | + f"{len(response.data)} categories successfully added to the project." |
| 1233 | + ) |
| 1234 | + |
| 1235 | + def list_categories(self, project: Union[NotEmptyStr, int]): |
| 1236 | + """ |
| 1237 | + List all categories in the project. |
| 1238 | +
|
| 1239 | + :param project: The name or ID of the project. |
| 1240 | + :type project: Union[NotEmptyStr, int] |
| 1241 | +
|
| 1242 | + :return: List of categories |
| 1243 | + :rtype: list of dict |
| 1244 | +
|
| 1245 | + Request Example: |
| 1246 | + :: |
| 1247 | +
|
| 1248 | + client.list_categories( |
| 1249 | + project="product-review-mm" |
| 1250 | + ) |
| 1251 | +
|
| 1252 | + Response Example: |
| 1253 | + :: |
| 1254 | +
|
| 1255 | + [ |
| 1256 | + { |
| 1257 | + "createdAt": "2025-01-29T13:51:39.000Z", |
| 1258 | + "updatedAt": "2025-01-29T13:51:39.000Z", |
| 1259 | + "id": 328577, |
| 1260 | + "name": "category1", |
| 1261 | + "project_id": 1234 |
| 1262 | + }, |
| 1263 | + { |
| 1264 | + "createdAt": "2025-01-29T13:51:39.000Z", |
| 1265 | + "updatedAt": "2025-01-29T13:51:39.000Z", |
| 1266 | + "id": 328577, |
| 1267 | + "name": "category2", |
| 1268 | + "project_id": 1234 |
| 1269 | + }, |
| 1270 | + ] |
| 1271 | +
|
| 1272 | + """ |
| 1273 | + project = ( |
| 1274 | + self.controller.get_project_by_id(project).data |
| 1275 | + if isinstance(project, int) |
| 1276 | + else self.controller.get_project(project) |
| 1277 | + ) |
| 1278 | + self.controller.check_multimodal_project_categorization(project) |
| 1279 | + |
| 1280 | + response = ( |
| 1281 | + self.controller.service_provider.work_management.list_project_categories( |
| 1282 | + project_id=project.id, entity=ProjectCategoryEntity |
| 1283 | + ) |
| 1284 | + ) |
| 1285 | + return BaseSerializer.serialize_iterable(response.data) |
| 1286 | + |
| 1287 | + def remove_categories( |
| 1288 | + self, |
| 1289 | + project: Union[NotEmptyStr, int], |
| 1290 | + categories: Union[List[str], Literal["*"]], |
| 1291 | + ): |
| 1292 | + """ |
| 1293 | + Remove one or more categories in a project. "*" in the category list will match all categories defined in the project. |
| 1294 | +
|
| 1295 | +
|
| 1296 | + :param project: The name or ID of the project. |
| 1297 | + :type project: Union[NotEmptyStr, int] |
| 1298 | +
|
| 1299 | + :param categories: A list of categories to remove, Accepts "*" to indicate all available categories in the project. |
| 1300 | + :type categories: Union[List[str], Literal["*"]] |
| 1301 | +
|
| 1302 | + Request Example: |
| 1303 | + :: |
| 1304 | +
|
| 1305 | + client.remove_categories( |
| 1306 | + project="product-review-mm", |
| 1307 | + categories=["Shoes", "T-Shirt"] |
| 1308 | + ) |
| 1309 | +
|
| 1310 | + # To remove all categories |
| 1311 | + client.remove_categories( |
| 1312 | + project="product-review-mm", |
| 1313 | + categories="*" |
| 1314 | + ) |
| 1315 | + """ |
| 1316 | + project = ( |
| 1317 | + self.controller.get_project_by_id(project).data |
| 1318 | + if isinstance(project, int) |
| 1319 | + else self.controller.get_project(project) |
| 1320 | + ) |
| 1321 | + self.controller.check_multimodal_project_categorization(project) |
| 1322 | + |
| 1323 | + query = EmptyQuery() |
| 1324 | + if categories == "*": |
| 1325 | + query &= Filter("id", [0], OperatorEnum.GT) |
| 1326 | + elif categories and isinstance(categories, list): |
| 1327 | + categories = [c.lower() for c in categories] |
| 1328 | + all_categories = self.controller.service_provider.work_management.list_project_categories( |
| 1329 | + project_id=project.id, entity=ProjectCategoryEntity |
| 1330 | + ) |
| 1331 | + categories_to_remove = [ |
| 1332 | + c for c in all_categories.data if c.name.lower() in categories |
| 1333 | + ] |
| 1334 | + query &= Filter("id", [c.id for c in categories_to_remove], OperatorEnum.IN) |
| 1335 | + else: |
| 1336 | + raise AppException("Categories should be a list of strings or '*'.") |
| 1337 | + |
| 1338 | + response = ( |
| 1339 | + self.controller.service_provider.work_management.remove_project_categories( |
| 1340 | + project_id=project.id, query=query |
| 1341 | + ) |
| 1342 | + ) |
| 1343 | + logger.info( |
| 1344 | + f"{len(response.data)} categories successfully removed from the project." |
| 1345 | + ) |
| 1346 | + |
1197 | 1347 | def create_folder(self, project: NotEmptyStr, folder_name: NotEmptyStr): |
1198 | 1348 | """ |
1199 | 1349 | Create a new folder in the project. |
|
0 commit comments