collection support of tasks
This commit is contained in:
parent
2f67e1cea6
commit
2b7ad0c724
|
@ -1,11 +1,13 @@
|
|||
import uuid
|
||||
from flask import Blueprint, render_template, request
|
||||
from sqlalchemy import delete
|
||||
|
||||
from app.extensions import db
|
||||
from app.models.collection import Collection
|
||||
from app.models.task import Task
|
||||
from app.models.session import Session
|
||||
|
||||
from .tasks import send_messages_task, add_to_group_task
|
||||
from .tasks import parse_users_task, send_messages_task, add_to_group_task
|
||||
|
||||
tasks = Blueprint("tasks", __name__, url_prefix="/tasks", template_folder="templates")
|
||||
|
||||
|
@ -19,14 +21,24 @@ def get_tasks():
|
|||
@tasks.route("/session/<int:session_id>", methods=["POST"])
|
||||
def create_task(session_id: int):
|
||||
info = request.form
|
||||
collection_id = int(info['collection'])
|
||||
|
||||
collection = Collection.query.get_or_404(collection_id)
|
||||
session = Session.query.get_or_404(session_id)
|
||||
task = Task(
|
||||
name = str(uuid.uuid4()),
|
||||
|
||||
types = list(filter(lambda item: item in ('parse', 'add', 'message'), info.keys()))
|
||||
|
||||
if len(types) > 0:
|
||||
type = types[0]
|
||||
|
||||
task = Task(
|
||||
name = info.get('task_name') or str(uuid.uuid4()),
|
||||
session = session,
|
||||
collection = collection,
|
||||
status = "CREATED",
|
||||
status_message = "Задача создана",
|
||||
type = info.get('task'),
|
||||
url = info.get('url'),
|
||||
type = type,
|
||||
url = info.get('parse') or info.get('add'),
|
||||
message = info.get('message'),
|
||||
file = None
|
||||
)
|
||||
|
@ -36,7 +48,7 @@ def create_task(session_id: int):
|
|||
|
||||
start_task(task.id)
|
||||
|
||||
return 'Created', 200
|
||||
return '', 204
|
||||
|
||||
@tasks.route("/<int:task_id>", methods=["DELETE"])
|
||||
def delete_task(task_id: int):
|
||||
|
@ -45,7 +57,7 @@ def delete_task(task_id: int):
|
|||
db.session.delete(task)
|
||||
db.session.commit()
|
||||
|
||||
return 'Deleted', 200
|
||||
return '', 204
|
||||
|
||||
@tasks.route("/<int:task_id>/stop", methods=["PUT"])
|
||||
def stop_task(task_id: int):
|
||||
|
@ -68,7 +80,7 @@ def stop_task(task_id: int):
|
|||
|
||||
db.session.commit()
|
||||
|
||||
return 'Stoping', 200
|
||||
return '', 204
|
||||
|
||||
|
||||
@tasks.route("/<int:task_id>/start", methods=["PUT"])
|
||||
|
@ -82,7 +94,11 @@ def start_task(task_id: int):
|
|||
case 'add':
|
||||
add_to_group_task.delay(task_id=task_id)
|
||||
|
||||
return 'Started', 200
|
||||
case 'parse':
|
||||
parse_users_task.apply(task_id=task_id)
|
||||
delete_task(task_id)
|
||||
|
||||
return '', 204
|
||||
|
||||
@tasks.route("/<int:task_id>/change", methods=["PUT"])
|
||||
def change_settings(task_id: int):
|
||||
|
@ -97,4 +113,4 @@ def change_settings(task_id: int):
|
|||
|
||||
db.session.commit()
|
||||
|
||||
return 'Changed', 200
|
||||
return '', 204
|
|
@ -2,6 +2,8 @@ import time
|
|||
|
||||
from celery import shared_task
|
||||
import asyncio
|
||||
from app.models.session import Session
|
||||
from app.models.user import User
|
||||
|
||||
from paper.parser import PaperParser
|
||||
from app.models.task import Task
|
||||
|
@ -10,14 +12,18 @@ from .handlers import run_state, failure_state, success_state
|
|||
|
||||
async def add_to_group(session, task, task_self):
|
||||
async with PaperParser(session.name) as parser:
|
||||
await parser.invite_users(session.users, task.url, task_self)
|
||||
await parser.invite_users(task.collection.users, task.url, task_self)
|
||||
|
||||
async def sending_message(session, task, task_self):
|
||||
async with PaperParser(session.name) as parser:
|
||||
await parser.send_messages(session.users, task.message, task.file, task_self)
|
||||
await parser.send_messages(task.collection.users, task.message, task.file, task_self)
|
||||
|
||||
async def parse_users(session, task):
|
||||
async with PaperParser(session.name) as parser:
|
||||
return await parser.get_participants(task.url)
|
||||
|
||||
@shared_task(bind=True)
|
||||
def add_to_group_task(self, task_id):
|
||||
def add_to_group_task(self, task_id: int):
|
||||
try:
|
||||
task: Task = Task.query.get(task_id)
|
||||
run_state(self.request.id, task_id)
|
||||
|
@ -37,10 +43,35 @@ def add_to_group_task(self, task_id):
|
|||
else:
|
||||
success_state(task_id)
|
||||
|
||||
@shared_task(bind=True)
|
||||
def parse_users_task(self, task_id: int):
|
||||
task: Task = Task.query.get_or_404(task_id)
|
||||
session = task.session
|
||||
collection = task.collection
|
||||
|
||||
users = asyncio.run(
|
||||
parse_users(session, task)
|
||||
)
|
||||
|
||||
for user in users:
|
||||
if not user.username:
|
||||
continue
|
||||
|
||||
if not User.exist(user.username, collection):
|
||||
db.session.add(
|
||||
User(
|
||||
first_name=user.first_name,
|
||||
last_name=user.last_name,
|
||||
username=user.username,
|
||||
phone=user.phone,
|
||||
collection=collection,
|
||||
)
|
||||
)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
@shared_task(bind=True)
|
||||
def send_messages_task(self, task_id):
|
||||
def send_messages_task(self, task_id: int):
|
||||
task: Task = Task.query.get(task_id)
|
||||
|
||||
session = task.session
|
||||
|
|
|
@ -17,3 +17,6 @@ class User(db.Model):
|
|||
|
||||
collection_id = Column(Integer, ForeignKey("collection.id"))
|
||||
collection: Mapped["Collection"] = relationship("Collection", back_populates="users")
|
||||
|
||||
def exist(username: str, collection):
|
||||
return not User.query.filter_by(username=username, collection=collection).first() is None
|
Loading…
Reference in New Issue