"""Controller for getting a task."""fromtypingimportAnyfrompoiesis.api.controllers.interfaceimportInterfaceControllerfrompoiesis.api.exceptionsimportNotFoundExceptionfrompoiesis.api.modelsimportTesViewfrompoiesis.api.tes.modelsimportTesTaskfrompoiesis.api.utilsimporttask_to_basic_taskfrompoiesis.repository.mongoimportMongoDBClient
[docs]classGetTaskController(InterfaceController):"""Controller for getting a task. This controller handles retrieving a specific task from the database. Args: db: The database client. id: The ID of the task to retrieve. user_id: The ID of the user to retrieve the task for. view: The view to retrieve the task in. """def__init__(self,db:MongoDBClient,id:str,user_id:str,view:str|None=TesView.MINIMAL.value,)->None:"""Initialize the controller. Args: db: The database client. id: The ID of the task to retrieve. user_id: The ID of the user to retrieve the task for. view: The view to retrieve the task in. """self.db=dbself.id=idself.user_id=user_idself.view=TesView(view)ifviewelseTesView.MINIMAL
[docs]asyncdefexecute(self,*args:Any,**kwargs:Any)->TesTask:"""Execute the controller to get a task. Returns: The requested task. Raises: NotFoundException: If the task is not found. """task=awaitself.db.get_task(self.id)ifnottask:raiseNotFoundException(f"Task with ID {self.id} not found")iftask.user_id!=self.user_id:raiseNotFoundException(f"Task with ID {self.id} not found")ifself.view==TesView.MINIMAL:returnTesTask(id=str(task.data.id),state=task.data.state,executors=task.data.executors,)elifself.view==TesView.BASIC:returntask_to_basic_task(task.data)else:returntask.data