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

118 lines
3.0 KiB
Python
Raw Normal View History

2024-01-31 19:37:01 +10:00
import uuid
from flask import Blueprint, render_template, request
2024-03-09 01:51:36 +10:00
from sqlalchemy import delete
2024-01-31 19:37:01 +10:00
from app.extensions import db
2024-03-09 01:51:36 +10:00
from app.models.collection import Collection
2024-01-31 19:37:01 +10:00
from app.models.task import Task
from app.models.session import Session
2024-03-09 01:51:36 +10:00
from .tasks import parse_users_task, send_messages_task, add_to_group_task
2024-01-31 19:37:01 +10:00
tasks = Blueprint("tasks", __name__, url_prefix="/tasks", template_folder="templates")
@tasks.route("/", methods=["GET"])
def get_tasks():
tasks = Task.query.all()
2024-02-11 20:16:11 +10:00
sessions = Session.query.filter_by(authorized=True).all()
2024-01-31 19:37:01 +10:00
2024-02-04 00:36:46 +10:00
return render_template("tasks_cards.j2", tasks=tasks, sessions=sessions)
2024-01-31 19:37:01 +10:00
@tasks.route("/session/<int:session_id>", methods=["POST"])
def create_task(session_id: int):
info = request.form
2024-03-09 01:51:36 +10:00
collection_id = int(info['collection'])
collection = Collection.query.get_or_404(collection_id)
2024-01-31 19:37:01 +10:00
session = Session.query.get_or_404(session_id)
2024-03-09 01:51:36 +10:00
types = list(filter(lambda item: item in ('parse', 'add', 'message'), info.keys()))
2024-03-10 00:29:44 +10:00
if len(types) <= 0:
return "", 204
type = types[0]
2024-03-09 01:51:36 +10:00
task = Task(
name = info.get('task_name') or str(uuid.uuid4()),
2024-01-31 19:37:01 +10:00
session = session,
2024-03-09 01:51:36 +10:00
collection = collection,
2024-01-31 19:37:01 +10:00
status = "CREATED",
status_message = "Задача создана",
2024-03-09 01:51:36 +10:00
type = type,
url = info.get('parse') or info.get('add'),
2024-01-31 19:37:01 +10:00
message = info.get('message'),
file = None
)
db.session.add(task)
db.session.commit()
start_task(task.id)
2024-03-09 01:51:36 +10:00
return '', 204
2024-01-31 19:37:01 +10:00
@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()
2024-03-12 18:54:34 +10:00
return '', 200
2024-01-31 19:37:01 +10:00
@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
2024-02-04 00:36:46 +10:00
if task.task_id and fn:
2024-01-31 19:37:01 +10:00
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()
2024-03-09 01:51:36 +10:00
return '', 204
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)
2024-03-09 01:51:36 +10:00
case 'parse':
2024-03-09 22:40:56 +10:00
parse_users_task.run(task_id=task_id)
2024-03-09 01:51:36 +10:00
delete_task(task_id)
2024-01-31 19:37:01 +10:00
2024-03-09 01:51:36 +10:00
return '', 204
2024-02-04 00:36:46 +10:00
@tasks.route("/<int:task_id>/change", methods=["PUT"])
def change_settings(task_id: int):
task: Task = Task.query.get_or_404(task_id)
data = request.form
if data.get("session"):
task.session = Session.query.get(data.get("session"))
if data.get("name"):
task.name = data.get("name")
db.session.commit()
2024-03-09 01:51:36 +10:00
return '', 204