Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down
51 changes: 30 additions & 21 deletions repositories/user.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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]:
Expand Down
Loading