Skip to content

Commit

Permalink
Merge pull request #21 from ikhit/ikhit-dev
Browse files Browse the repository at this point in the history
Ikhit dev
  • Loading branch information
GreenVibesOnly authored Oct 16, 2024
2 parents 119448d + 2dc232b commit bfc10e5
Show file tree
Hide file tree
Showing 44 changed files with 2,867 additions and 1,732 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<<<<<<< HEAD
BOT_TOKEN=<'BOT TOKEN'>
TELEGRAM_CHAT_IDS=<'TELEGRAM CHAT IDS'>

=======
TELEGRAM_TOKEN=<'TELEGRAM TOKEN'>
TELEGRAM_CHAT_IDS=<'TELEGRAM CHAT IDS'>

DATABASE_URL=<'DATABASE URL'>
>>>>>>> upstream/dev
POSTGRES_USER=<'DB OWNER'>
POSTGRES_PASSWORD=<'PASSWORD'>
POSTGRES_DB=<'DB NAME'>
Expand Down
19 changes: 19 additions & 0 deletions app/admin/admin_managers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from .delete_manager import DeleteManager, DeleteState # noqa
from .create_manager import CreateManager, CreateState # noqa
from .update_manager import UpdateManager, UpdatePortfolio, UpdateState # noqa
from .question_manager import ( # noqa
QuestionCreateManager,
QuestionUpdateManager,
CreateQuestionStates,
UpdateQuestionStates,
DeleteQuestionStates,
QuestionDeleteManager,
)
from .category_manager import ( # noqa
CreateCategoryManager,
UpdateCategoryManager,
DeleteCategoryManager,
CategoryCreateState,
CategoryUpdateState,
CategoryDeleteState,
)
27 changes: 27 additions & 0 deletions app/admin/admin_managers/base_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from abc import ABC

from aiogram.fsm.state import StatesGroup

from crud.base_crud import CRUDBase


class BaseAdminManager(ABC):
"""
Базовый абстрактный класс для менеджеров администратора.
Этот класс определяет общие атрибуты и методы для менеджеров,
которые будут использоваться для взаимодействия с CRUD-операциями
и клавиатурами администратора.
Attributes:
model_crud (CRUDBase): Объект для выполнения операций CRUD с моделью.
back_option (str): Данные для возврата в меню.
staes_group (StaesGroup): Набор машинных состояний
"""

def __init__(
self, model_crud: CRUDBase, back_option: str, states_group: StatesGroup
) -> None:
self.model_crud = model_crud
self.back_option = back_option
self.states_group = states_group
160 changes: 160 additions & 0 deletions app/admin/admin_managers/category_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
from aiogram.fsm.context import FSMContext
from aiogram.fsm.state import State, StatesGroup
from aiogram.types import CallbackQuery
from sqlalchemy.ext.asyncio import AsyncSession

from admin.keyboards.keyboards import (
get_inline_confirmation,
get_inline_keyboard,
)
from admin.admin_settings import ADMIN_UPDATE_BUTTONS
from crud.base_crud import CRUDBase
from crud.category_product import category_product_crud
from .create_manager import CreateManager
from .update_manager import UpdateManager
from .delete_manager import DeleteManager


class CategoryCreateState(StatesGroup):
select = State()
name = State()
url = State()
description = State()
media = State()


class CategoryUpdateState(StatesGroup):
select = State()
name = State()
url = State()
description = State()
media = State()


class CategoryDeleteState(StatesGroup):
select = State()
confirm = State()


class CreateCategoryManager(CreateManager):
def __init__(
self,
back_option: str,
model_crud: CRUDBase = category_product_crud,
states_group: StatesGroup = CategoryCreateState(),
) -> None:
super().__init__(model_crud, back_option, states_group)

async def add_obj_name(
self,
product_id: int,
callback: CallbackQuery,
state: FSMContext,
):
"""
Добавить название объекта и перейти в
следующее машинное состояние.
"""
await callback.message.delete()
await state.update_data(product_id=product_id)
await callback.message.answer(
"Введите название:",
reply_markup=await get_inline_keyboard(
previous_menu=self.back_option
),
)
await state.set_state(self.states_group.name)


class UpdateCategoryManager(UpdateManager):
def __init__(
self,
back_option: str,
model_crud: CRUDBase = category_product_crud,
states_group: StatesGroup = CategoryUpdateState(),
) -> None:
super().__init__(model_crud, back_option, states_group)

async def select_obj_to_update(
self,
product_id: int,
callback: CallbackQuery,
state: FSMContext,
session: AsyncSession,
) -> None:
obj_list = await self.model_crud.get_category_by_product_id(
product_id, session
)
obj_names = [obj.name for obj in obj_list]
obj_ids = [obj.id for obj in obj_list]
await callback.message.edit_text(
"Какой объект отредактировать?",
reply_markup=await get_inline_keyboard(
options=obj_names,
callback=obj_ids,
previous_menu=self.back_option,
),
)
await state.set_state(self.states_group.select)

async def select_data_to_update(
self,
callback: CallbackQuery,
session: AsyncSession,
):
"""Выбрать поле редактирования для модели в БД."""
self.obj_to_update = await self.model_crud.get(callback.data, session)
await callback.message.edit_text(
"Выбирите данные для обновления:",
reply_markup=await get_inline_keyboard(
ADMIN_UPDATE_BUTTONS, previous_menu=self.back_option
),
)


class DeleteCategoryManager(DeleteManager):
def __init__(
self,
back_option: str,
model_crud: CRUDBase = category_product_crud,
states_group: StatesGroup = CategoryDeleteState(),
) -> None:
super().__init__(model_crud, back_option, states_group)

async def select_obj_to_delete(
self,
product_id: int,
callback: CallbackQuery,
state: FSMContext,
session: AsyncSession,
) -> None:
obj_list = await self.model_crud.get_category_by_product_id(
product_id, session
)
obj_names = [obj.name for obj in obj_list]
obj_ids = [obj.id for obj in obj_list]
await callback.message.edit_text(
"Какие данные удалить?",
reply_markup=await get_inline_keyboard(
options=obj_names,
callback=obj_ids,
previous_menu=self.back_option,
),
)
await state.set_state(self.states_group.select)

async def confirm_delete(
self,
callback: CallbackQuery,
state: FSMContext,
session: AsyncSession,
) -> None:
"""Подтвердить выбор объекта для удаления."""
self.obj_to_delete = await self.model_crud.get(callback.data, session)
await callback.message.edit_text(
f"Вы уверены, что хотите удалить эти данные?\n\n {self.obj_to_delete.name}",
reply_markup=await get_inline_confirmation(
cancel_option=self.back_option
),
),
await state.set_state(self.states_group.confirm)
Loading

0 comments on commit bfc10e5

Please sign in to comment.