Добавил типов
This commit is contained in:
parent
77d1fd9668
commit
fa82cb828f
4 changed files with 21 additions and 20 deletions
|
@ -1,4 +1,5 @@
|
|||
from datetime import datetime, timedelta
|
||||
from typing import Iterator
|
||||
|
||||
from jose import jwt
|
||||
from passlib.context import CryptContext
|
||||
|
@ -79,25 +80,25 @@ async def check_verificated_user(user_id: int) -> bool:
|
|||
return user.role >= VERIFICATED_USER
|
||||
|
||||
|
||||
async def check_verificated_user_with_exc(user_id: int):
|
||||
async def check_verificated_user_with_exc(user_id: int) -> None:
|
||||
if not await check_verificated_user(user_id=user_id):
|
||||
raise UserMustConfirmEmailException
|
||||
|
||||
|
||||
async def get_user_allowed_chats_id(user_id: int):
|
||||
async def get_user_allowed_chats_id(user_id: int) -> Iterator[int]:
|
||||
user_allowed_chats = await UserDAO.get_user_allowed_chats(user_id)
|
||||
user_allowed_chats_id = (chat['chat_id'] for chat in user_allowed_chats)
|
||||
return user_allowed_chats_id
|
||||
|
||||
|
||||
async def validate_user_access_to_chat(user_id: int, chat_id: int):
|
||||
async def validate_user_access_to_chat(user_id: int, chat_id: int) -> bool:
|
||||
user_allowed_chats = await get_user_allowed_chats_id(user_id=user_id)
|
||||
if not chat_id in user_allowed_chats:
|
||||
raise UserDontHavePermissionException
|
||||
return True
|
||||
|
||||
|
||||
async def validate_user_admin(user_id: int):
|
||||
async def validate_user_admin(user_id: int) -> bool:
|
||||
user_role = await UserDAO.get_user_role(user_id=user_id)
|
||||
if user_role == ADMIN_USER:
|
||||
return True
|
||||
|
|
|
@ -20,7 +20,7 @@ class ChatDAO(BaseDAO):
|
|||
return result
|
||||
|
||||
@staticmethod
|
||||
async def add_user_to_chat(user_id: int, chat_id: int):
|
||||
async def add_user_to_chat(user_id: int, chat_id: int) -> bool:
|
||||
query = select(UsersXChats.user_id).where(UsersXChats.chat_id == chat_id)
|
||||
async with async_session_maker() as session:
|
||||
result = await session.execute(query)
|
||||
|
@ -33,7 +33,7 @@ class ChatDAO(BaseDAO):
|
|||
return True
|
||||
|
||||
@staticmethod
|
||||
async def send_message(user_id, chat_id: int, message: str, image_url: str | None = None):
|
||||
async def send_message(user_id: int, chat_id: int, message: str, image_url: str | None = None) -> int:
|
||||
query = (insert(Messages).values(chat_id=chat_id, user_id=user_id, message=message, image_url=image_url)
|
||||
.returning(Messages.id))
|
||||
async with async_session_maker() as session:
|
||||
|
@ -59,7 +59,7 @@ class ChatDAO(BaseDAO):
|
|||
return result[0]
|
||||
|
||||
@staticmethod
|
||||
async def delete_message(message_id: int):
|
||||
async def delete_message(message_id: int) -> bool:
|
||||
query = update(Messages).where(Messages.id == message_id).values(visibility=False)
|
||||
async with async_session_maker() as session:
|
||||
await session.execute(query)
|
||||
|
@ -67,7 +67,7 @@ class ChatDAO(BaseDAO):
|
|||
return True
|
||||
|
||||
@staticmethod
|
||||
async def get_some_messages(chat_id: int, message_number_from: int, messages_to_get: int):
|
||||
async def get_some_messages(chat_id: int, message_number_from: int, messages_to_get: int) -> list[dict]:
|
||||
"""
|
||||
WITH messages_with_users AS (
|
||||
SELECT *
|
||||
|
@ -105,7 +105,7 @@ class ChatDAO(BaseDAO):
|
|||
return result
|
||||
|
||||
@staticmethod
|
||||
async def delete_chat(chat_id: int):
|
||||
async def delete_chat(chat_id: int) -> bool:
|
||||
query = update(Chats).where(Chats.id == chat_id).values(visibility=False)
|
||||
async with async_session_maker() as session:
|
||||
await session.execute(query)
|
||||
|
@ -113,7 +113,7 @@ class ChatDAO(BaseDAO):
|
|||
return True
|
||||
|
||||
@staticmethod
|
||||
async def delete_user(chat_id: int, user_id: int):
|
||||
async def delete_user(chat_id: int, user_id: int) -> bool:
|
||||
query = delete(UsersXChats).where(and_(
|
||||
UsersXChats.chat_id == chat_id,
|
||||
UsersXChats.user_id == user_id
|
||||
|
@ -124,7 +124,7 @@ class ChatDAO(BaseDAO):
|
|||
return True
|
||||
|
||||
@staticmethod
|
||||
async def pinn_chat(chat_id: int, user_id: int):
|
||||
async def pinn_chat(chat_id: int, user_id: int) -> bool:
|
||||
query = select(PinnedChats.chat_id).where(PinnedChats.user_id == user_id)
|
||||
async with async_session_maker() as session:
|
||||
result = await session.execute(query)
|
||||
|
@ -137,7 +137,7 @@ class ChatDAO(BaseDAO):
|
|||
return True
|
||||
|
||||
@staticmethod
|
||||
async def unpinn_chat(chat_id: int, user_id: int):
|
||||
async def unpinn_chat(chat_id: int, user_id: int) -> bool:
|
||||
query = delete(PinnedChats).where(PinnedChats.chat_id == chat_id, PinnedChats.user_id == user_id)
|
||||
async with async_session_maker() as session:
|
||||
await session.execute(query)
|
||||
|
@ -184,7 +184,7 @@ class ChatDAO(BaseDAO):
|
|||
return result
|
||||
|
||||
@staticmethod
|
||||
async def pinn_message(chat_id: int, message_id: int, user_id: int):
|
||||
async def pinn_message(chat_id: int, message_id: int, user_id: int) -> bool:
|
||||
query = insert(PinnedMessages).values(chat_id=chat_id, message_id=message_id, user_id=user_id)
|
||||
async with async_session_maker() as session:
|
||||
await session.execute(query)
|
||||
|
@ -192,7 +192,7 @@ class ChatDAO(BaseDAO):
|
|||
return True
|
||||
|
||||
@staticmethod
|
||||
async def get_message_pinner(chat_id: int, message_id: int):
|
||||
async def get_message_pinner(chat_id: int, message_id: int) -> bool:
|
||||
query = select(PinnedMessages.user_id).where(
|
||||
PinnedMessages.chat_id == chat_id, PinnedMessages.message_id == message_id
|
||||
)
|
||||
|
@ -202,7 +202,7 @@ class ChatDAO(BaseDAO):
|
|||
return result
|
||||
|
||||
@staticmethod
|
||||
async def unpinn_message(chat_id: int, message_id: int):
|
||||
async def unpinn_message(chat_id: int, message_id: int) -> bool:
|
||||
query = delete(PinnedMessages).where(
|
||||
PinnedMessages.chat_id == chat_id, PinnedMessages.message_id == message_id
|
||||
)
|
||||
|
@ -212,7 +212,7 @@ class ChatDAO(BaseDAO):
|
|||
return True
|
||||
|
||||
@staticmethod
|
||||
async def get_pinned_messages(chat_id: int):
|
||||
async def get_pinned_messages(chat_id: int) -> list[dict]:
|
||||
query = (select(Messages.message, Messages.image_url,
|
||||
Messages.chat_id, Messages.user_id,
|
||||
Messages.created_at, Users.avatar_image,
|
||||
|
|
|
@ -18,7 +18,7 @@ class UserDAO(BaseDAO):
|
|||
|
||||
|
||||
@staticmethod
|
||||
async def change_data(user_id: int, **data_to_change):
|
||||
async def change_data(user_id: int, **data_to_change) -> str:
|
||||
query = update(Users).where(Users.id == user_id).values(**data_to_change)
|
||||
async with async_session_maker() as session:
|
||||
await session.execute(query)
|
||||
|
@ -28,7 +28,7 @@ class UserDAO(BaseDAO):
|
|||
return result.scalar()
|
||||
|
||||
@staticmethod
|
||||
async def get_user_role(user_id: int):
|
||||
async def get_user_role(user_id: int) -> int:
|
||||
query = select(Users.role).where(Users.id == user_id)
|
||||
async with async_session_maker() as session:
|
||||
result = await session.execute(query)
|
||||
|
@ -89,7 +89,7 @@ class UserDAO(BaseDAO):
|
|||
return result
|
||||
|
||||
@staticmethod
|
||||
async def get_user_avatar(user_id: int):
|
||||
async def get_user_avatar(user_id: int) -> str:
|
||||
query = select(Users.avatar_image).where(Users.id == user_id)
|
||||
async with async_session_maker() as session:
|
||||
result = await session.execute(query)
|
||||
|
|
|
@ -12,7 +12,7 @@ from app.users.dao import UserDAO
|
|||
from app.users.models import Users
|
||||
|
||||
|
||||
def get_token(request: Request):
|
||||
def get_token(request: Request) -> str:
|
||||
token = request.cookies.get("black_phoenix_access_token")
|
||||
if not token:
|
||||
raise TokenAbsentException
|
||||
|
|
Loading…
Add table
Reference in a new issue