Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 41 additions & 29 deletions app/webhooks/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
from bot.charity_bot import dispatcher
from bot.formatter import display_task_notification
from bot.messages import TelegramNotification, SendUserMessageContext, SendUserNotificationsContext
from core.repositories.task_repository import TaskRepository

task_repository = TaskRepository(db_session)


class CreateTasks(MethodResource, Resource):
Expand All @@ -39,32 +42,40 @@ class CreateTasks(MethodResource, Resource):
def post(self):
tasks = request_to_context(TaskCreateRequest, request)

tasks_active_db = task_repository.get_active_tasks()
id_tasks_active_db = [task.id for task in tasks_active_db]
tasks_dict = {task.id: task for task in tasks}
tasks_db = Task.query.options(load_only('archive')).all()
task_id_json = [task.id for task in tasks]
task_id_db = [task.id for task in tasks_db]

task_for_adding_db = []
task_update = []
task_to_send = []

task_id_db_not_archive = [task.id for task in tasks_db if task.archive is False]
task_for_archive = list(set(task_id_db_not_archive) - set(task_id_json))
archive_records = [task for task in tasks_db if task.id in task_for_archive]
archived_tasks = self.__archive_tasks(archive_records)
def initialization_tasks(task_id_json):
for id_task in task_id_json:
task = task_repository.get_or_none(id_task)
if task:
task_update.append(task)
else:
task_for_adding_db.append(id_task)

task_id_db_archive = list(set(task_id_db) - set(task_id_db_not_archive))
task_for_unarchive = list(set(task_id_db_archive) & set(task_id_json))
unarchive_records = [task for task in tasks_db if task.id in task_for_unarchive]
unarchived_tasks = self.__unarchive_tasks(unarchive_records, task_to_send, tasks_dict)
initialization_tasks(task_id_json)

task_for_adding_db = list(set(task_id_json) - set(task_id_db))
tasks_to_add = [task for task in tasks if task.id in task_for_adding_db]
added_tasks = self.__add_tasks(tasks_to_add, task_to_send)
added_tasks = []
if task_for_adding_db:
tasks_to_add = [task for task in tasks if task.id in task_for_adding_db]
added_tasks = self.__add_tasks(tasks_to_add, task_to_send)

task_id_db_active = list(
set(task_id_json) - set(task_for_archive) - set(task_for_unarchive) - set(task_for_adding_db)
)
active_tasks = [task for task in tasks_db if task.id in task_id_db_active]
updated_tasks = self.__update_active_tasks(active_tasks, task_to_send, tasks_dict)
updated_tasks = []
unarchived_tasks = []
if task_update:
updated_tasks = self.__update_tasks(task_update, task_to_send, tasks_dict)
unarchived_tasks = self.__unarchive_tasks(task_update, updated_tasks, task_to_send, id_tasks_active_db)

archived_tasks = []

if updated_tasks or added_tasks:
archive_records = [task for task in tasks_active_db if task not in task_update]
archived_tasks = self.__archive_tasks(archive_records)

try:
db_session.commit()
Expand Down Expand Up @@ -148,15 +159,16 @@ def __archive_tasks(self, archive_records):
logger.info(f'Tasks: Archived task ids: {task_ids}')
return task_ids

def __unarchive_tasks(self, unarchive_records, task_to_send, tasks_dict):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не очень хорошая идея удалить этот метод.
Может быть не часто, но он используется. Лог с тестового сервера:
image

task_ids = [task.id for task in unarchive_records]
for task in unarchive_records:
task_from_dict = tasks_dict.get(task.id)
self.__update_task_fields(task, task_from_dict)
task_to_send.append(task)
logger.info(f'Tasks: Unarchived {len(unarchive_records)} tasks.')
logger.info(f'Tasks: Unarchived task IDs: {task_ids}')
return task_ids
def __unarchive_tasks(self, task_update, updated_tasks, task_to_send, id_tasks_active_db):
unarchived_tasks_ids = []
for updated_task in updated_tasks:
if updated_task not in id_tasks_active_db:
unarchived_tasks_ids.append(updated_task)
tasks_send = [task for task in task_update if task.id in unarchived_tasks_ids]
task_to_send.extend(tasks_send)
logger.info(f"Tasks: Unarchived {len(unarchived_tasks_ids)} tasks.")
logger.info(f"Tasks: Unarchived task IDs: {unarchived_tasks_ids}")
return unarchived_tasks_ids

def __hash__(self, task):
if type(task) == dict:
Expand All @@ -166,7 +178,7 @@ def __hash__(self, task):
return hash(f'{title}{description}{deadline}')
return hash(f'{task.title}{task.description}{task.deadline}')

def __update_active_tasks(self, active_tasks, task_to_send, tasks_dict):
def __update_tasks(self, active_tasks, task_to_send, tasks_dict):
updated_task_ids = []
for task in active_tasks:
task_from_dict = tasks_dict.get(task.id)
Expand Down
35 changes: 35 additions & 0 deletions core/repositories/task_repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from typing import Optional

from app.models import Task
from core.repositories.abstract_repository import AbstractRepository
from sqlalchemy.orm import Session


class TaskRepository(AbstractRepository):

def __init__(self, session: Session) -> None:
self.session = session

def get_or_none(self, task_id: int) -> Optional[Task]:
return self.session.get(Task, task_id)

def get(self, task_id: int) -> Task:
task = self.get_or_none(task_id)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Метод может вернуть None, но ты его никак не отлавливаешь.

if not task:
raise LookupError(f'Task ID={task_id} not found')
return task

def get_active_tasks(self) -> Task:
tasks = self.session.query(Task).filter_by(archive=False).all()
return tasks

def create(self, task_list: list[Task]) -> list[Task]:
self.session.add_all(task_list)
self.session.commit()
return task_list

def update(self, task: Task) -> Task:
self.session.add(task)
self.session.commit()
self.session.refresh(task)
return task
12 changes: 7 additions & 5 deletions core/repositories/user_repository.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from sqlalchemy.orm import Session

from core.repositories.abstract_repository import AbstractRepository
from typing import Optional

from app.models import User
from core.repositories.abstract_repository import AbstractRepository
from sqlalchemy.orm import Session


class UserRepository(AbstractRepository):
Expand All @@ -26,5 +25,8 @@ def create(self, user: User) -> User:
self.session.refresh(user)
return user

def update(self, user: User) -> None:
pass
def update(self, user: User) -> User:
self.session.add(user)
self.session.commit()
self.session.refresh(user)
return user