Coverage for yaptide/routes/task_routes.py: 32%
25 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-22 07:31 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-22 07:31 +0000
1from flask import request
2from flask_restful import Resource
4from yaptide.persistence.db_methods import (fetch_simulation_by_sim_id, fetch_task_by_sim_id_and_task_id,
5 update_task_state)
6from yaptide.routes.utils.response_templates import yaptide_response
7from yaptide.routes.utils.tokens import decode_auth_token
10class TasksResource(Resource):
11 """Class responsible for updating tasks"""
13 @staticmethod
14 def post():
15 """
16 Method updating task state
17 Structure required by this method to work properly:
18 {
19 "simulation_id": <int>,
20 "task_id": <string>,
21 "update_key": <string>,
22 "update_dict": <dict>
23 }
24 simulation_id and task_id self explanatory
25 """
26 payload_dict: dict = request.get_json(force=True)
27 required_keys = {"simulation_id", "task_id", "update_key", "update_dict"}
28 if required_keys != set(payload_dict.keys()):
29 diff = required_keys.difference(set(payload_dict.keys()))
30 return yaptide_response(message=f"Missing keys in JSON payload: {diff}", code=400)
32 sim_id: int = payload_dict["simulation_id"]
33 simulation = fetch_simulation_by_sim_id(sim_id=sim_id)
35 if not simulation:
36 return yaptide_response(message=f"Simulation {sim_id} does not exist", code=400)
38 decoded_token = decode_auth_token(payload_dict["update_key"], payload_key_to_return="simulation_id")
39 if decoded_token != sim_id:
40 return yaptide_response(message="Invalid update key", code=400)
42 task = fetch_task_by_sim_id_and_task_id(sim_id=simulation.id, task_id=payload_dict["task_id"])
44 if not task:
45 return yaptide_response(message=f"Task {payload_dict['task_id']} does not exist", code=400)
47 update_task_state(task=task, update_dict=payload_dict["update_dict"])
49 return yaptide_response(message="Task updated", code=202)