Marking users to delete

This commit is contained in:
Анатолий Богомолов 2024-03-13 16:49:37 +10:00
parent da9f98ccb5
commit 1286ee4474
5 changed files with 44 additions and 12 deletions

View File

@ -7,21 +7,15 @@ def success_state(task_id):
task.status = "SUCCESS" task.status = "SUCCESS"
task.status_message = "задача выполнена успешно" task.status_message = "задача выполнена успешно"
db.session.commit()
def failure_state(task_id, exception): def failure_state(task_id, exception):
task = Task.query.get(task_id) task = Task.query.get(task_id)
task.status = "FAILURE" task.status = "FAILURE"
task.status_message = f"произошла ошибка {str(exception)}" task.status_message = f"произошла ошибка {str(exception)}"
db.session.commit()
def run_state(task_id, task_record_id): def run_state(task_id, task_record_id):
task = Task.query.get(task_record_id) task = Task.query.get(task_record_id)
task.task_id = task_id task.task_id = task_id
task.status = "RUNNING" task.status = "RUNNING"
task.status_message = "задача запущена" task.status_message = "задача запущена"
db.session.commit()

View File

@ -3,6 +3,7 @@ import time
from celery import shared_task from celery import shared_task
import asyncio import asyncio
from loguru import logger
from telethon.tl.types import UserStatusOnline, UserStatusOffline, UserStatusRecently from telethon.tl.types import UserStatusOnline, UserStatusOffline, UserStatusRecently
from app.models.user import User from app.models.user import User
@ -38,12 +39,15 @@ def add_to_group_task(self, task_id: int):
) )
except Exception as e: except Exception as e:
logger.exception(e)
failure_state(task_id, e) failure_state(task_id, e)
raise e
else: else:
success_state(task_id) success_state(task_id)
finally:
db.session.commit()
@shared_task(bind=True) @shared_task(bind=True)
def parse_users_task(self, task_id: int): def parse_users_task(self, task_id: int):
task: Task = Task.query.get_or_404(task_id) task: Task = Task.query.get_or_404(task_id)

View File

@ -1,4 +1,4 @@
from sqlalchemy import Integer from sqlalchemy import Integer, null
from sqlalchemy.schema import ( from sqlalchemy.schema import (
Column, Column,
ForeignKey, ForeignKey,
@ -15,6 +15,8 @@ class User(db.Model):
phone: Mapped[str] = mapped_column(nullable=True) phone: Mapped[str] = mapped_column(nullable=True)
username: Mapped[str] = mapped_column(nullable=True) username: Mapped[str] = mapped_column(nullable=True)
delete: Mapped[bool] = mapped_column(default="false")
collection_id = Column(Integer, ForeignKey("collection.id")) collection_id = Column(Integer, ForeignKey("collection.id"))
collection: Mapped["Collection"] = relationship("Collection", back_populates="users") collection: Mapped["Collection"] = relationship("Collection", back_populates="users")

View File

@ -0,0 +1,32 @@
"""Delete flag for users
Revision ID: 65ccd5b1039b
Revises: 11ed77b4dbe8
Create Date: 2024-03-13 16:48:13.991684
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '65ccd5b1039b'
down_revision = '11ed77b4dbe8'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('user', schema=None) as batch_op:
batch_op.add_column(sa.Column('delete', sa.Boolean(), nullable=False, server_default="false"))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('user', schema=None) as batch_op:
batch_op.drop_column('delete')
# ### end Alembic commands ###

View File

@ -22,7 +22,7 @@ class PaperParser:
if group_participants: if group_participants:
participants_usernames = [participant.username for participant in group_participants if participant.username] participants_usernames = [participant.username for participant in group_participants if participant.username]
users = filter(lambda user: user.username not in participants_usernames, users) users = filter(lambda user: user.username not in participants_usernames and not user.delete, users)
for user in users: for user in users:
try: try:
@ -32,8 +32,8 @@ class PaperParser:
await self.client.invite_user(user, group) await self.client.invite_user(user, group)
except (UserPrivacyException, IgnoreException): except (UserPrivacyException, IgnoreException):
self.users_to_delete.append(user) logger.warning(f"Exception occurred. Marking {user.username} for deletion...")
logger.warning("Exception occurred. Skipping user...") user.delete = True
finally: finally:
if not task.is_aborted(): if not task.is_aborted():