From 0106f22db6065bdb5ec0af2e2a32c4cdc62498ea Mon Sep 17 00:00:00 2001 From: ikhit Date: Fri, 18 Oct 2024 18:38:31 +0300 Subject: [PATCH] add redis set timer command --- .../admin_handlers/superuser_handlers.py | 6 +- ...st_commit => 17c3c5e67c14_first_commit.py} | 0 .../versions/4f7c029c89f4_add_timer.py | 74 ------------------- app/bot/handlers.py | 11 ++- app/core/init_db.py | 12 +-- app/main.py | 3 +- app/redis_db/connect.py | 2 +- 7 files changed, 17 insertions(+), 91 deletions(-) rename app/alembic/versions/{17c3c5e67c14_first_commit => 17c3c5e67c14_first_commit.py} (100%) delete mode 100644 app/alembic/versions/4f7c029c89f4_add_timer.py diff --git a/app/admin/handlers/admin_handlers/superuser_handlers.py b/app/admin/handlers/admin_handlers/superuser_handlers.py index 1818121..0076fc5 100644 --- a/app/admin/handlers/admin_handlers/superuser_handlers.py +++ b/app/admin/handlers/admin_handlers/superuser_handlers.py @@ -20,6 +20,7 @@ SUPERUSER_SPECIAL_BUTTONS, SUPERUSER_SPECIAL_OPTIONS, ) +from redis_db.connect import get_redis_connection from bot.exceptions import message_exception_handler from models.models import User, RoleEnum from crud.request_to_manager import get_manager_stats @@ -336,7 +337,10 @@ async def check_and_set_new_timer( ): """Проверить и выставить новый таймер.""" try: - # await change_timer(message.text, session) + redis_client = await get_redis_connection() + + await redis_client.set("timeout", int(message.text)) + await redis_client.close() await message.answer( f"Новый таймер на {message.text} секунд установлен!", reply_markup=await get_inline_keyboard( diff --git a/app/alembic/versions/17c3c5e67c14_first_commit b/app/alembic/versions/17c3c5e67c14_first_commit.py similarity index 100% rename from app/alembic/versions/17c3c5e67c14_first_commit rename to app/alembic/versions/17c3c5e67c14_first_commit.py diff --git a/app/alembic/versions/4f7c029c89f4_add_timer.py b/app/alembic/versions/4f7c029c89f4_add_timer.py deleted file mode 100644 index 0a6246f..0000000 --- a/app/alembic/versions/4f7c029c89f4_add_timer.py +++ /dev/null @@ -1,74 +0,0 @@ -"""add timer - -Revision ID: 4f7c029c89f4 -Revises: 17c3c5e67c14 -Create Date: 2024-10-18 00:20:36.194500 - -""" -from typing import Sequence, Union - -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision: str = '4f7c029c89f4' -down_revision: Union[str, None] = '17c3c5e67c14' -branch_labels: Union[str, Sequence[str], None] = None -depends_on: Union[str, Sequence[str], None] = None - - -def upgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.create_table('timer', - sa.Column('timer', sa.INTEGER(), nullable=False), - sa.Column('id', sa.Integer(), nullable=False), - sa.PrimaryKeyConstraint('id') - ) - op.alter_column('categorytype', 'media', - existing_type=sa.VARCHAR(length=128), - type_=sa.VARCHAR(length=256), - existing_nullable=True) - op.alter_column('checkcompanyportfolio', 'name', - existing_type=sa.VARCHAR(length=48), - type_=sa.VARCHAR(length=150), - existing_nullable=False) - op.alter_column('contactmanager', 'phone_number', - existing_type=sa.VARCHAR(length=25), - type_=sa.VARCHAR(length=32), - existing_nullable=False) - op.alter_column('informationaboutcompany', 'name', - existing_type=sa.VARCHAR(length=48), - type_=sa.VARCHAR(length=150), - existing_nullable=False) - op.alter_column('user', 'name', - existing_type=sa.VARCHAR(length=32), - type_=sa.VARCHAR(length=150), - existing_nullable=False) - # ### end Alembic commands ### - - -def downgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.alter_column('user', 'name', - existing_type=sa.VARCHAR(length=150), - type_=sa.VARCHAR(length=32), - existing_nullable=False) - op.alter_column('informationaboutcompany', 'name', - existing_type=sa.VARCHAR(length=150), - type_=sa.VARCHAR(length=48), - existing_nullable=False) - op.alter_column('contactmanager', 'phone_number', - existing_type=sa.VARCHAR(length=32), - type_=sa.VARCHAR(length=25), - existing_nullable=False) - op.alter_column('checkcompanyportfolio', 'name', - existing_type=sa.VARCHAR(length=150), - type_=sa.VARCHAR(length=48), - existing_nullable=False) - op.alter_column('categorytype', 'media', - existing_type=sa.VARCHAR(length=256), - type_=sa.VARCHAR(length=128), - existing_nullable=True) - op.drop_table('timer') - # ### end Alembic commands ### diff --git a/app/bot/handlers.py b/app/bot/handlers.py index bd98225..b84902a 100644 --- a/app/bot/handlers.py +++ b/app/bot/handlers.py @@ -2,13 +2,16 @@ from aiogram import Router, F from aiogram.filters import CommandStart, Command +from aiogram.fsm.context import FSMContext from aiogram.types import Message from sqlalchemy.ext.asyncio import AsyncSession from admin.keyboards.keyboards import get_inline_keyboard from admin.admin_settings import MAIN_MENU_BUTTONS from bot.bot_const import ( - ADMIN_NEGATIVE_ANSWER, ADMIN_POSITIVE_ANSWER, START_MESSAGE + ADMIN_NEGATIVE_ANSWER, + ADMIN_POSITIVE_ANSWER, + START_MESSAGE, ) from models.models import RoleEnum from crud.users import create_user_id, get_role_by_tg_id, is_user_in_db @@ -53,9 +56,13 @@ async def cmd_admin(message: Message, session: AsyncSession) -> None: log_error_text="Ошибка при обработке команды /start." ) @router.message(CommandStart()) -async def cmd_start(message: Message, session: AsyncSession) -> None: +async def cmd_start( + message: Message, state: FSMContext, session: AsyncSession +) -> None: """Выводит приветствие пользователя.""" + await state.clear() + user_id = get_user_id(message) redis_client = await get_redis_connection() diff --git a/app/core/init_db.py b/app/core/init_db.py index 57d72eb..6686846 100644 --- a/app/core/init_db.py +++ b/app/core/init_db.py @@ -1,11 +1,10 @@ from admin.admin_settings import PORTFOLIO_DEFAULT_DATA from admin.admin_settings import admin_list -from crud.timer import get_timer from crud.users import create_user_id from crud.user_crud import user_crud from core.db import AsyncSessionLocal from crud.portfolio_projects_crud import portfolio_crud -from models.models import Timer, RoleEnum +from models.models import RoleEnum async def add_portfolio(): @@ -27,12 +26,3 @@ async def set_admin(): await user_crud.update(user, RoleEnum.ADMIN , session) -async def set_timer(): - """Установить таймер активности.""" - async with AsyncSessionLocal() as session: - if not await get_timer(session): - timer = Timer() - session.add(timer) - await session.commit() - await session.refresh(timer) - return timer diff --git a/app/main.py b/app/main.py index c902571..a256f75 100644 --- a/app/main.py +++ b/app/main.py @@ -9,7 +9,7 @@ from bot.callbacks import router as callback_router from bot.fsm_contexts.manager_context import router as fsm_context_router from bot.fsm_contexts.feedback_context import router as feedback_context -from core.init_db import add_portfolio, set_admin, set_timer +from core.init_db import add_portfolio, set_admin from admin.handlers.admin_handlers import admin_router from loggers.log import setup_logging @@ -38,7 +38,6 @@ async def main() -> None: dispatcher.update.middleware( DataBaseSession(session_pool=AsyncSessionLocal) ) - await set_timer() await add_portfolio() await set_admin() await dispatcher.start_polling(bot) diff --git a/app/redis_db/connect.py b/app/redis_db/connect.py index ccf7d3b..95428eb 100644 --- a/app/redis_db/connect.py +++ b/app/redis_db/connect.py @@ -5,5 +5,5 @@ async def get_redis_connection(): """Подключение к Redis.""" return await aioredis.from_url( - "redis://158.160.77.163:6379", decode_responses=True # localhost: + "redis://localhost", decode_responses=True # localhost:158.160.77.163:6379 )