Вроде доделал создание чата
This commit is contained in:
parent
82c57b3134
commit
2a4502815d
6 changed files with 41 additions and 12 deletions
|
@ -72,3 +72,8 @@ class UserCanNotReadThisChatException(BlackPhoenixException):
|
||||||
class WrongCodeException(BlackPhoenixException):
|
class WrongCodeException(BlackPhoenixException):
|
||||||
status_code = status.HTTP_409_CONFLICT
|
status_code = status.HTTP_409_CONFLICT
|
||||||
detail = 'Введён не верный код подтверждения'
|
detail = 'Введён не верный код подтверждения'
|
||||||
|
|
||||||
|
|
||||||
|
class UserNotFoundException(BlackPhoenixException):
|
||||||
|
status_code = status.HTTP_404_NOT_FOUND
|
||||||
|
detail = 'Юзер не найден'
|
||||||
|
|
|
@ -6,6 +6,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
||||||
from app.admin.auth import authentication_backend
|
from app.admin.auth import authentication_backend
|
||||||
from app.admin.views import UsersAdmin, ChatsAdmin, MessagesAdmin, UsersXChatsAdmin
|
from app.admin.views import UsersAdmin, ChatsAdmin, MessagesAdmin, UsersXChatsAdmin
|
||||||
from app.database import engine
|
from app.database import engine
|
||||||
|
from app.users.auth import check_verificated_user
|
||||||
from app.users.chat.router import router as chat_router
|
from app.users.chat.router import router as chat_router
|
||||||
from app.users.router import router as user_router
|
from app.users.router import router as user_router
|
||||||
from app.pages.router import router as pages_router
|
from app.pages.router import router as pages_router
|
||||||
|
@ -48,5 +49,5 @@ app.mount("/static", StaticFiles(directory="app/static"), name="static")
|
||||||
|
|
||||||
|
|
||||||
@app.get('/')
|
@app.get('/')
|
||||||
async def root():
|
async def root(user_id: int):
|
||||||
pass
|
return await check_verificated_user(user_id)
|
||||||
|
|
|
@ -5,7 +5,8 @@ from passlib.context import CryptContext
|
||||||
from pydantic import EmailStr
|
from pydantic import EmailStr
|
||||||
|
|
||||||
from app.config import settings
|
from app.config import settings
|
||||||
from app.exceptions import UserDontHavePermissionException, IncorrectAuthDataException, UserAlreadyExistsException
|
from app.exceptions import UserDontHavePermissionException, IncorrectAuthDataException, UserAlreadyExistsException, \
|
||||||
|
UserNotFoundException
|
||||||
from app.users.dao import UserDAO
|
from app.users.dao import UserDAO
|
||||||
from app.users.models import Users
|
from app.users.models import Users
|
||||||
from app.users.schemas import SUserRegister
|
from app.users.schemas import SUserRegister
|
||||||
|
@ -13,6 +14,7 @@ from app.users.schemas import SUserRegister
|
||||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||||
|
|
||||||
ADMIN_USER = 100
|
ADMIN_USER = 100
|
||||||
|
ADMIN_USER_ID = 21
|
||||||
REGISTRATED_USER = 0
|
REGISTRATED_USER = 0
|
||||||
VERIFICATED_USER = 1
|
VERIFICATED_USER = 1
|
||||||
|
|
||||||
|
@ -70,6 +72,13 @@ async def check_existing_user(user_data: SUserRegister) -> None:
|
||||||
raise UserAlreadyExistsException
|
raise UserAlreadyExistsException
|
||||||
|
|
||||||
|
|
||||||
|
async def check_verificated_user(user_id: int) -> bool:
|
||||||
|
user = await UserDAO.find_one_or_none(id=user_id)
|
||||||
|
if not user:
|
||||||
|
raise UserNotFoundException
|
||||||
|
return user.role >= VERIFICATED_USER
|
||||||
|
|
||||||
|
|
||||||
async def get_user_allowed_chats_id(user_id: int):
|
async def get_user_allowed_chats_id(user_id: int):
|
||||||
user_allowed_chats = await UserDAO.get_user_allowed_chats(user_id)
|
user_allowed_chats = await UserDAO.get_user_allowed_chats(user_id)
|
||||||
user_allowed_chats_id = (chat['chat_id'] for chat in user_allowed_chats)
|
user_allowed_chats_id = (chat['chat_id'] for chat in user_allowed_chats)
|
||||||
|
|
|
@ -3,15 +3,24 @@ from sqlalchemy import insert, select, update, and_
|
||||||
from app.dao.base import BaseDAO
|
from app.dao.base import BaseDAO
|
||||||
from app.database import async_session_maker
|
from app.database import async_session_maker
|
||||||
from app.users.models import Users
|
from app.users.models import Users
|
||||||
from app.users.chat.models import Chats, Messages
|
from app.users.chat.models import Chats, Messages, UsersXChats
|
||||||
|
|
||||||
|
|
||||||
class ChatDAO(BaseDAO):
|
class ChatDAO(BaseDAO):
|
||||||
model = Chats
|
model = Chats
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def create(cls, user_id: int, chat_name: str):
|
async def create(cls, user_id: int, chat_name: str) -> int:
|
||||||
query = insert(Chats).values(chat_for=user_id, chat_name=chat_name)
|
query = insert(Chats).values(chat_for=user_id, chat_name=chat_name).returning(Chats.id)
|
||||||
|
async with async_session_maker() as session:
|
||||||
|
result = await session.execute(query)
|
||||||
|
await session.commit()
|
||||||
|
result = result.scalar()
|
||||||
|
return result
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def add_user_to_chat(cls, user_id: int, chat_id: int):
|
||||||
|
query = insert(UsersXChats).values(user_id=user_id, chat_id=chat_id)
|
||||||
async with async_session_maker() as session:
|
async with async_session_maker() as session:
|
||||||
await session.execute(query)
|
await session.execute(query)
|
||||||
await session.commit()
|
await session.commit()
|
||||||
|
|
|
@ -6,7 +6,7 @@ from app.users.chat.shemas import SMessage, SLastMessages
|
||||||
|
|
||||||
from app.users.dao import UserDAO
|
from app.users.dao import UserDAO
|
||||||
from app.users.dependencies import get_current_user
|
from app.users.dependencies import get_current_user
|
||||||
from app.users.auth import validate_user_access_to_chat, validate_user_admin
|
from app.users.auth import validate_user_access_to_chat, validate_user_admin, get_user_allowed_chats_id, ADMIN_USER_ID
|
||||||
from app.users.models import Users
|
from app.users.models import Users
|
||||||
|
|
||||||
router = APIRouter(
|
router = APIRouter(
|
||||||
|
@ -26,6 +26,9 @@ async def add_message_to_chat(
|
||||||
message: str,
|
message: str,
|
||||||
user: Users = Depends(get_current_user)
|
user: Users = Depends(get_current_user)
|
||||||
):
|
):
|
||||||
|
chats = await get_user_allowed_chats_id(user.id)
|
||||||
|
if chat_id not in chats:
|
||||||
|
raise UserDontHavePermissionException
|
||||||
send_message_to_chat = await ChatDAO.send_message(
|
send_message_to_chat = await ChatDAO.send_message(
|
||||||
user_id=user.id,
|
user_id=user.id,
|
||||||
chat_id=chat_id,
|
chat_id=chat_id,
|
||||||
|
@ -48,7 +51,7 @@ async def delete_message_from_chat(
|
||||||
return deleted_message
|
return deleted_message
|
||||||
|
|
||||||
|
|
||||||
# Не доделан
|
|
||||||
@router.post("/create_chat", status_code=status.HTTP_201_CREATED)
|
@router.post("/create_chat", status_code=status.HTTP_201_CREATED)
|
||||||
async def create_chat(
|
async def create_chat(
|
||||||
user_to_exclude: int,
|
user_to_exclude: int,
|
||||||
|
@ -57,8 +60,10 @@ async def create_chat(
|
||||||
):
|
):
|
||||||
if user.id == user_to_exclude:
|
if user.id == user_to_exclude:
|
||||||
raise UserCanNotReadThisChatException
|
raise UserCanNotReadThisChatException
|
||||||
created_chat = await ChatDAO.create(user_id=user_to_exclude, chat_name=chat_name)
|
chat_id = await ChatDAO.create(user_id=user_to_exclude, chat_name=chat_name)
|
||||||
return created_chat
|
user_added_to_chat = await ChatDAO.add_user_to_chat(user.id, chat_id)
|
||||||
|
await ChatDAO.add_user_to_chat(ADMIN_USER_ID, chat_id)
|
||||||
|
return user_added_to_chat
|
||||||
|
|
||||||
|
|
||||||
@router.get("/get_last_message/{chat_id}", response_model=list[SMessage])
|
@router.get("/get_last_message/{chat_id}", response_model=list[SMessage])
|
||||||
|
|
|
@ -16,7 +16,7 @@ class Users(Base):
|
||||||
hashed_password: Mapped[str]
|
hashed_password: Mapped[str]
|
||||||
role: Mapped[int]
|
role: Mapped[int]
|
||||||
black_phoenix: Mapped[bool]
|
black_phoenix: Mapped[bool]
|
||||||
avatar_image: Mapped[Optional[str]] = mapped_column(server_default='app/static/images/ты уже пешка BP.png')
|
avatar_image: Mapped[Optional[str]] = mapped_column(server_default='static/images/ты уже пешка BP.png')
|
||||||
date_of_birth: Mapped[date]
|
date_of_birth: Mapped[date]
|
||||||
date_of_registration: Mapped[date] = mapped_column(server_default=func.now())
|
date_of_registration: Mapped[date] = mapped_column(server_default=func.now())
|
||||||
|
|
||||||
|
@ -41,4 +41,4 @@ class UsersVerificationCodes(Base):
|
||||||
user = relationship("Users", back_populates="verificationcode")
|
user = relationship("Users", back_populates="verificationcode")
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"Код {self.code} для юзера {self.user}. {self.description}"
|
return f"Код {self.code} для юзера {self.user_id}. {self.description}"
|
||||||
|
|
Loading…
Add table
Reference in a new issue