def lock_task_for_mapping(lock_task_dto: LockTaskDTO) -> TaskDTO: """ Sets the task_locked status to locked so no other user can work on it :param lock_task_dto: DTO with data needed to lock the task :raises TaskServiceError :return: Updated task, or None if not found """ task = MappingService.get_task(lock_task_dto.task_id, lock_task_dto.project_id) if not task.is_mappable(): raise MappingServiceError('Task in invalid state for mapping') user_can_map, error_reason = ProjectService.is_user_permitted_to_map( lock_task_dto.project_id, lock_task_dto.user_id) if not user_can_map: if error_reason == MappingNotAllowed.USER_NOT_ACCEPTED_LICENSE: raise UserLicenseError( 'User must accept license to map this task') else: raise MappingServiceError( f'Mapping not allowed because: {error_reason.name}') task.lock_task_for_mapping(lock_task_dto.user_id) return task.as_dto_with_instructions(lock_task_dto.preferred_locale)
def lock_tasks_for_validation( validation_dto: LockForValidationDTO) -> TaskDTOs: """ Lock supplied tasks for validation :raises ValidatatorServiceError """ # Loop supplied tasks to check they can all be locked for validation tasks_to_lock = [] for task_id in validation_dto.task_ids: task = Task.get(task_id, validation_dto.project_id) if task is None: raise NotFound(f'Task {task_id} not found') if TaskStatus(task.task_status) not in [ TaskStatus.MAPPED, TaskStatus.VALIDATED, TaskStatus.BADIMAGERY ]: raise ValidatatorServiceError( f'Task {task_id} is not MAPPED, BADIMAGERY or VALIDATED') if not ValidatorService._user_can_validate_task( validation_dto.user_id, task.mapped_by): raise ValidatatorServiceError( f'Tasks cannot be validated by the same user who marked task as mapped or badimagery' ) tasks_to_lock.append(task) user_can_validate, error_reason = ProjectService.is_user_permitted_to_validate( validation_dto.project_id, validation_dto.user_id) if not user_can_validate: if error_reason == ValidatingNotAllowed.USER_NOT_ACCEPTED_LICENSE: raise UserLicenseError( 'User must accept license to map this task') else: raise ValidatatorServiceError( f'Mapping not allowed because: {error_reason.name}') # Lock all tasks for validation dtos = [] for task in tasks_to_lock: task.lock_task_for_validating(validation_dto.user_id) dtos.append( task.as_dto_with_instructions(validation_dto.preferred_locale)) task_dtos = TaskDTOs() task_dtos.tasks = dtos return task_dtos