PaperParser/app/blueprints/api/tasks/routes.py

88 lines
2.2 KiB
Python
Raw Normal View History

2024-01-31 19:37:01 +10:00
import uuid
from flask import Blueprint, render_template, request
from app.extensions import db
from app.models.task import Task
from app.models.session import Session
from .tasks import send_messages_task, add_to_group_task
tasks = Blueprint("tasks", __name__, url_prefix="/tasks", template_folder="templates")
@tasks.route("/", methods=["GET"])
def get_tasks():
tasks = Task.query.all()
return render_template("tasks_cards.j2", tasks=tasks)
@tasks.route("/<int:task_id>", methods=["GET"])
def get_task(task_id: int):
...
@tasks.route("/session/<int:session_id>", methods=["POST"])
def create_task(session_id: int):
info = request.form
session = Session.query.get_or_404(session_id)
task = Task(
name = str(uuid.uuid4()),
session = session,
status = "CREATED",
status_message = "Задача создана",
type = info.get('task'),
url = info.get('url'),
message = info.get('message'),
file = None
)
db.session.add(task)
db.session.commit()
start_task(task.id)
return 'Created', 200
@tasks.route("/<int:task_id>", methods=["DELETE"])
def delete_task(task_id: int):
task: Task = Task.query.get_or_404(task_id)
stop_task(task.id)
db.session.delete(task)
db.session.commit()
return 'Deleted', 200
@tasks.route("/<int:task_id>/stop", methods=["PUT"])
def stop_task(task_id: int):
task = Task.query.get_or_404(task_id)
fn = None
match task.type:
case 'message':
fn = send_messages_task
case 'add':
fn = add_to_group_task
if task.task_id:
task_fn = fn.AsyncResult(task.task_id)
task_fn.abort()
2024-02-03 22:29:56 +10:00
task.status = "STOPING"
task.status_message = "Задача останавливается"
2024-01-31 19:37:01 +10:00
2024-02-03 22:29:56 +10:00
db.session.commit()
return 'Stoping', 200
2024-01-31 19:37:01 +10:00
@tasks.route("/<int:task_id>/start", methods=["PUT"])
def start_task(task_id: int):
task: Task = Task.query.get_or_404(task_id)
match task.type:
case 'message':
send_messages_task.delay(task_id=task_id)
case 'add':
add_to_group_task.delay(task_id=task_id)
return 'Started', 200