diff --git a/app/blueprints/api/tasks/routes.py b/app/blueprints/api/tasks/routes.py index 30880d8..95fdec6 100644 --- a/app/blueprints/api/tasks/routes.py +++ b/app/blueprints/api/tasks/routes.py @@ -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/", 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("/", 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("//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("//start", methods=["PUT"]) @@ -81,8 +93,12 @@ def start_task(task_id: int): case 'add': add_to_group_task.delay(task_id=task_id) + + case 'parse': + parse_users_task.apply(task_id=task_id) + delete_task(task_id) - return 'Started', 200 + return '', 204 @tasks.route("//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 \ No newline at end of file + return '', 204 \ No newline at end of file diff --git a/app/blueprints/api/tasks/tasks.py b/app/blueprints/api/tasks/tasks.py index 99526e0..255dee8 100644 --- a/app/blueprints/api/tasks/tasks.py +++ b/app/blueprints/api/tasks/tasks.py @@ -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 diff --git a/app/models/user.py b/app/models/user.py index ffaf9e6..68e9fdd 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -16,4 +16,7 @@ class User(db.Model): username: Mapped[str] = mapped_column(nullable=True, unique=True) collection_id = Column(Integer, ForeignKey("collection.id")) - collection: Mapped["Collection"] = relationship("Collection", back_populates="users") \ No newline at end of file + 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 \ No newline at end of file