Skip to content

Commit 47e5aa9

Browse files
authored
Merge pull request #149 from ilyarolf/develop
Develop
2 parents 80ec639 + 8c11992 commit 47e5aa9

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

bot.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pathlib import Path
55
from aiogram.client.default import DefaultBotProperties
66
from aiogram.fsm.storage.redis import RedisStorage
7-
from aiogram.types import BufferedInputFile
7+
from aiogram.types import BufferedInputFile, URLInputFile
88
from redis.asyncio import Redis
99
from sqladmin import Admin
1010

@@ -88,7 +88,17 @@ async def on_startup():
8888
static.mkdir()
8989
me = await bot.get_me()
9090
photos = await bot.get_user_profile_photos(me.id)
91-
bot_photo_id = photos.photos[0][-1].file_id
91+
if photos.total_count == 0:
92+
photo_id_list = []
93+
for admin_id in config.ADMIN_ID_LIST:
94+
msg = await bot.send_photo(chat_id=admin_id,
95+
photo=URLInputFile(url="https://i.imgur.com/CxWRPwY.png",
96+
filename="no_image.png"))
97+
bot_photo_id = msg.photo[-1].file_id
98+
photo_id_list.append(bot_photo_id)
99+
bot_photo_id = photo_id_list[0]
100+
else:
101+
bot_photo_id = photos.photos[0][-1].file_id
92102
with open("static/no_image.jpeg", "w") as f:
93103
f.write(bot_photo_id)
94104
validate_i18n()

repositories/user.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
import datetime
2-
import math
3-
41
from sqlalchemy import select, update, func, or_
52
from sqlalchemy.ext.asyncio import AsyncSession
63
from sqlalchemy.orm import Session
7-
84
import config
95
from callbacks import StatisticsTimeDelta
106
from db import session_execute, session_flush
@@ -14,6 +10,9 @@
1410

1511

1612
class UserRepository:
13+
INT32_MAX = 2_147_483_647
14+
INT32_MIN = -2_147_483_648
15+
1716
@staticmethod
1817
async def get_by_tgid(telegram_id: int, session: AsyncSession | Session) -> UserDTO | None:
1918
stmt = select(User).where(User.telegram_id == telegram_id)
@@ -53,25 +52,35 @@ async def get_all_count(session: Session | AsyncSession) -> int:
5352
return users_count.scalar_one()
5453

5554
@staticmethod
56-
async def get_user_entity(user_entity: int | str, session: Session | AsyncSession) -> UserDTO | None:
55+
async def get_user_entity(
56+
user_entity: int | str,
57+
session: Session | AsyncSession
58+
) -> UserDTO | None:
59+
60+
entity_int: int | None = None
5761
try:
58-
entity_like_int = int(user_entity)
59-
except ValueError:
60-
entity_like_int = None
61-
62-
stmt = select(User).where(
63-
or_(
64-
User.telegram_id == entity_like_int if entity_like_int is not None else False,
65-
User.telegram_username == user_entity if entity_like_int is None else False,
66-
User.id == entity_like_int if entity_like_int is not None else False
67-
)
68-
)
69-
user = await session_execute(stmt, session)
70-
user = user.scalar()
62+
entity_int = int(user_entity)
63+
except (ValueError, TypeError):
64+
pass
65+
66+
conditions = []
67+
if entity_int is not None:
68+
conditions.append(User.telegram_id == entity_int)
69+
if UserRepository.INT32_MIN <= entity_int <= UserRepository.INT32_MAX:
70+
conditions.append(User.id == entity_int)
71+
if isinstance(user_entity, str):
72+
conditions.append(User.telegram_username == user_entity)
73+
74+
if not conditions:
75+
return None
76+
stmt = select(User).where(or_(*conditions))
77+
result = await session_execute(stmt, session)
78+
user = result.scalar_one_or_none()
79+
7180
if user is None:
72-
return user
73-
else:
74-
return UserDTO.model_validate(user, from_attributes=True)
81+
return None
82+
83+
return UserDTO.model_validate(user, from_attributes=True)
7584

7685
@staticmethod
7786
async def get_by_timedelta(timedelta: StatisticsTimeDelta, session: Session | AsyncSession) -> list[UserDTO]:

0 commit comments

Comments
 (0)