def archive(self, request: Request) -> Response: """Change status of specified tasks and delete them in CVAT. Tasks that have been archived will be skipped as well as task IDs that are not exist. Request:: {"id": [task_id]} Response:: { "archived": [task_id], "errors": [ { "id": task_id, "error": string } ] } """ ids_serializer = LabelingTaskIDSerializer(data=request.data) ids_serializer.is_valid(raise_exception=True) labeling_tasks = LabelingTask.fetch_for_archiving( pk__in=ids_serializer.data['id'], ) cvat_service = CvatService() deleted = [] errors = [] for lt in labeling_tasks: try: cvat_service.delete_task(lt.task_id) except CVATServiceException as err: if lt.status == LabelingTaskStatus.SAVED: deleted.append(lt) logger.error( 'Unable to delete cvat task %d. Reason: %s', lt.task_id, err, ) errors.append({'id': lt.id, 'error': str(err)}) else: deleted.append(lt) LabelingTask.update_statuses(deleted, LabelingTaskStatus.ARCHIVED) return Response( data={ 'archived': [each.id for each in deleted], 'errors': errors, }, status=status.HTTP_200_OK, )
def test_update_statuses(self): tasks = [ self.test_factory.create_labeling_task( status=LabelingTaskStatus.ANNOTATION), self.test_factory.create_labeling_task( status=LabelingTaskStatus.COMPLETED), ] LabelingTask.update_statuses(tasks, LabelingTaskStatus.ARCHIVED) for got in LabelingTask.objects.filter(pk__in=[t.id for t in tasks]).all(): self.assertEqual(got.status, LabelingTaskStatus.ARCHIVED)