From bfdf17d07834f1f9882bd05ffd277c1eb1667c10 Mon Sep 17 00:00:00 2001 From: ilyarolf Date: Tue, 20 Jan 2026 17:54:16 +0300 Subject: [PATCH 1/2] fix search by telegram id issue --- repositories/user.py | 51 ++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/repositories/user.py b/repositories/user.py index a3711e74..bd89544a 100644 --- a/repositories/user.py +++ b/repositories/user.py @@ -1,10 +1,6 @@ -import datetime -import math - from sqlalchemy import select, update, func, or_ from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.orm import Session - import config from callbacks import StatisticsTimeDelta from db import session_execute, session_flush @@ -14,6 +10,9 @@ class UserRepository: + INT32_MAX = 2_147_483_647 + INT32_MIN = -2_147_483_648 + @staticmethod async def get_by_tgid(telegram_id: int, session: AsyncSession | Session) -> UserDTO | None: stmt = select(User).where(User.telegram_id == telegram_id) @@ -53,25 +52,35 @@ async def get_all_count(session: Session | AsyncSession) -> int: return users_count.scalar_one() @staticmethod - async def get_user_entity(user_entity: int | str, session: Session | AsyncSession) -> UserDTO | None: + async def get_user_entity( + user_entity: int | str, + session: Session | AsyncSession + ) -> UserDTO | None: + + entity_int: int | None = None try: - entity_like_int = int(user_entity) - except ValueError: - entity_like_int = None - - stmt = select(User).where( - or_( - User.telegram_id == entity_like_int if entity_like_int is not None else False, - User.telegram_username == user_entity if entity_like_int is None else False, - User.id == entity_like_int if entity_like_int is not None else False - ) - ) - user = await session_execute(stmt, session) - user = user.scalar() + entity_int = int(user_entity) + except (ValueError, TypeError): + pass + + conditions = [] + if entity_int is not None: + conditions.append(User.telegram_id == entity_int) + if UserRepository.INT32_MIN <= entity_int <= UserRepository.INT32_MAX: + conditions.append(User.id == entity_int) + if isinstance(user_entity, str): + conditions.append(User.telegram_username == user_entity) + + if not conditions: + return None + stmt = select(User).where(or_(*conditions)) + result = await session_execute(stmt, session) + user = result.scalar_one_or_none() + if user is None: - return user - else: - return UserDTO.model_validate(user, from_attributes=True) + return None + + return UserDTO.model_validate(user, from_attributes=True) @staticmethod async def get_by_timedelta(timedelta: StatisticsTimeDelta, session: Session | AsyncSession) -> list[UserDTO]: From 8c11992c7c6ee18335dd365528629808a0172569 Mon Sep 17 00:00:00 2001 From: ilyarolf Date: Tue, 20 Jan 2026 17:54:39 +0300 Subject: [PATCH 2/2] fix issue if bot doesn't have picture --- bot.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/bot.py b/bot.py index ca195387..467a5909 100644 --- a/bot.py +++ b/bot.py @@ -4,7 +4,7 @@ from pathlib import Path from aiogram.client.default import DefaultBotProperties from aiogram.fsm.storage.redis import RedisStorage -from aiogram.types import BufferedInputFile +from aiogram.types import BufferedInputFile, URLInputFile from redis.asyncio import Redis from sqladmin import Admin @@ -88,7 +88,17 @@ async def on_startup(): static.mkdir() me = await bot.get_me() photos = await bot.get_user_profile_photos(me.id) - bot_photo_id = photos.photos[0][-1].file_id + if photos.total_count == 0: + photo_id_list = [] + for admin_id in config.ADMIN_ID_LIST: + msg = await bot.send_photo(chat_id=admin_id, + photo=URLInputFile(url="https://i.imgur.com/CxWRPwY.png", + filename="no_image.png")) + bot_photo_id = msg.photo[-1].file_id + photo_id_list.append(bot_photo_id) + bot_photo_id = photo_id_list[0] + else: + bot_photo_id = photos.photos[0][-1].file_id with open("static/no_image.jpeg", "w") as f: f.write(bot_photo_id) validate_i18n()