From a28e4393d2f807978eefc38bce34999ccd06e477 Mon Sep 17 00:00:00 2001 From: Alexander Date: Mon, 19 Dec 2022 23:02:17 +0300 Subject: [PATCH 1/3] Refactoring module tasks --- core/repositories/task_repository.py | 39 ++++++++++++++++++++++++++++ core/repositories/user_repository.py | 12 +++++---- 2 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 core/repositories/task_repository.py diff --git a/core/repositories/task_repository.py b/core/repositories/task_repository.py new file mode 100644 index 00000000..fff7a176 --- /dev/null +++ b/core/repositories/task_repository.py @@ -0,0 +1,39 @@ +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) + if not task: + raise LookupError(f'Task ID={task_id} not found') + return task + + def checking_object(self, task_id: int) -> Task: + task = self.get_or_none(task_id) + return task + + def get_active_tasks(self): + 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 diff --git a/core/repositories/user_repository.py b/core/repositories/user_repository.py index b0a3e113..d8713d79 100644 --- a/core/repositories/user_repository.py +++ b/core/repositories/user_repository.py @@ -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): @@ -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 From d51c22cf0e1535c12bc3f148a6fab0c0e1ad295f Mon Sep 17 00:00:00 2001 From: Alexander Date: Sat, 14 Jan 2023 16:59:55 +0300 Subject: [PATCH 2/3] 'resolved incompatibility in tasks.py' --- app/webhooks/tasks.py | 70 +++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/app/webhooks/tasks.py b/app/webhooks/tasks.py index e913347e..e83f853c 100644 --- a/app/webhooks/tasks.py +++ b/app/webhooks/tasks.py @@ -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): @@ -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.checking_object(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() @@ -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): - 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: @@ -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) From 84db2dd50ccb8e13a54a77da7cb40196a39fdfeb Mon Sep 17 00:00:00 2001 From: Alexander Date: Sat, 14 Jan 2023 22:31:42 +0300 Subject: [PATCH 3/3] 'dell method checking_object' --- app/webhooks/tasks.py | 2 +- core/repositories/task_repository.py | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/app/webhooks/tasks.py b/app/webhooks/tasks.py index e83f853c..ba57226e 100644 --- a/app/webhooks/tasks.py +++ b/app/webhooks/tasks.py @@ -52,7 +52,7 @@ def post(self): def initialization_tasks(task_id_json): for id_task in task_id_json: - task = task_repository.checking_object(id_task) + task = task_repository.get_or_none(id_task) if task: task_update.append(task) else: diff --git a/core/repositories/task_repository.py b/core/repositories/task_repository.py index fff7a176..aefd240f 100644 --- a/core/repositories/task_repository.py +++ b/core/repositories/task_repository.py @@ -19,11 +19,7 @@ def get(self, task_id: int) -> Task: raise LookupError(f'Task ID={task_id} not found') return task - def checking_object(self, task_id: int) -> Task: - task = self.get_or_none(task_id) - return task - - def get_active_tasks(self): + def get_active_tasks(self) -> Task: tasks = self.session.query(Task).filter_by(archive=False).all() return tasks