Помогите понять как искать пользователей в базе данных, которые заходили сегодня и неделю назад библиотеки: aiogram, sqlalchemy, asyncpg, datetime
Помогите понять как искать пользователей в базе данных, которые заходили сегодня и неделю назад библиотеки: aiogram, sqlalchemy, asyncpg, datetime
#requests
async def day(today):
async with async_session() as session:
users = await session.query(User).filter(User.date.days == today.days).all()
users = users.count()
return users
async def week(week):
async with async_session() as session:
users = await session.query(User).filter(abs(User.date.days - week.days) <= 7).all()
return users
#admin_handlers
@admin.callback_query(F.data == 'day')
async def day(callback_query: CallbackQuery):
today = datetime.date.today()
a = day(today)
await callback_query.message.answer(f'Количество пользователей: {a}')
@admin.callback_query(F.data == 'week')
async def week(callback_query: CallbackQuery):
now_week = datetime.date.today()
a = week(now_week)
await callback_query.message.answer(f'Количество пользователей: {a}')
#models
from sqlalchemy import ForeignKey, String, BigInteger
from sqlalchemy.orm import Mapped, mapped_column, DeclarativeBase, relationship
from sqlalchemy.ext.asyncio import AsyncAttrs, async_sessionmaker, create_async_engine
from config import DB_URL
engine = create_async_engine(url=DB_URL, echo=True, future=True)
async_session = async_sessionmaker(engine)
class Base(AsyncAttrs, DeclarativeBase):
pass
class User(Base):
__tablename__ = 'users'
id: Mapped[int] = mapped_column(primary_key=True)
tg_id = mapped_column(BigInteger)
name: Mapped[str] = mapped_column()
# age: Mapped[int] = mapped_column(none=True)
# gender: Mapped[str] = mapped_column(none=True)
date: Mapped[str] = mapped_column()
async def async_main():
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)