Added SQLAlchemy and Alembic to work with Postgres

This commit is contained in:
2023-10-29 01:28:46 +03:00
parent f95e013093
commit 279b48895b
8 changed files with 323 additions and 1 deletions

View File

@@ -2,6 +2,9 @@ from typing import Any, Awaitable, Callable, Dict
from aiogram import BaseMiddleware
from aiogram.types import Message
from sqlalchemy.orm import Session
from db.data import User, engine
ADMIN_LIST = [248770879, 395543883]
@@ -10,7 +13,12 @@ class AdminMiddleware(BaseMiddleware):
pass
async def __call__(self, handler: Callable[[Message, Dict[str, Any]], Awaitable[Any]], event: Message, data: Dict[str, Any]) -> Any:
if event.from_user.id in ADMIN_LIST:
with Session(engine) as session:
if not session.get(User, event.from_user.id):
user = User(id=event.from_user.id, user_name=event.from_user.username)
session.add(user)
session.commit()
if event.from_user.id not in ADMIN_LIST:
await event.answer('Команда только для админов!')
return None
return await handler(event, data)