Обновил таблицу чатов
This commit is contained in:
parent
2a4502815d
commit
b4d26dae90
9 changed files with 145 additions and 8 deletions
|
@ -0,0 +1,30 @@
|
|||
"""Изменения таблицы чатов
|
||||
|
||||
Revision ID: 29121de940c3
|
||||
Revises: 90e16e2ea817
|
||||
Create Date: 2024-02-11 15:23:25.957366
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '29121de940c3'
|
||||
down_revision: Union[str, None] = '90e16e2ea817'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
pass
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
pass
|
||||
# ### end Alembic commands ###
|
|
@ -0,0 +1,30 @@
|
|||
"""Изменения таблицы чатов
|
||||
|
||||
Revision ID: 73241845c54f
|
||||
Revises: 7bb5f34b11d2
|
||||
Create Date: 2024-02-11 15:18:55.916912
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '73241845c54f'
|
||||
down_revision: Union[str, None] = '7bb5f34b11d2'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.add_column('chats', sa.Column('visibility', sa.Boolean(), server_default='true', nullable=False))
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_column('chats', 'visibility')
|
||||
# ### end Alembic commands ###
|
|
@ -0,0 +1,30 @@
|
|||
"""Изменения таблицы чатов
|
||||
|
||||
Revision ID: 90e16e2ea817
|
||||
Revises: 73241845c54f
|
||||
Create Date: 2024-02-11 15:22:39.496545
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '90e16e2ea817'
|
||||
down_revision: Union[str, None] = '73241845c54f'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
pass
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
pass
|
||||
# ### end Alembic commands ###
|
|
@ -0,0 +1,32 @@
|
|||
"""Изменения таблицы чатов
|
||||
|
||||
Revision ID: b978b3b7a449
|
||||
Revises: 29121de940c3
|
||||
Create Date: 2024-02-11 15:37:53.064931
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = 'b978b3b7a449'
|
||||
down_revision: Union[str, None] = '29121de940c3'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.add_column('chats', sa.Column('created_by', sa.Integer(), nullable=False))
|
||||
op.create_foreign_key(None, 'chats', 'users', ['created_by'], ['id'])
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_constraint(None, 'chats', type_='foreignkey')
|
||||
op.drop_column('chats', 'created_by')
|
||||
# ### end Alembic commands ###
|
|
@ -10,8 +10,8 @@ class ChatDAO(BaseDAO):
|
|||
model = Chats
|
||||
|
||||
@classmethod
|
||||
async def create(cls, user_id: int, chat_name: str) -> int:
|
||||
query = insert(Chats).values(chat_for=user_id, chat_name=chat_name).returning(Chats.id)
|
||||
async def create(cls, user_id: int, chat_name: str, created_by: int) -> int:
|
||||
query = insert(Chats).values(chat_for=user_id, chat_name=chat_name, created_by=created_by).returning(Chats.id)
|
||||
async with async_session_maker() as session:
|
||||
result = await session.execute(query)
|
||||
await session.commit()
|
||||
|
@ -57,6 +57,10 @@ class ChatDAO(BaseDAO):
|
|||
await session.commit()
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
async def delete_chat(cls, chat_id: int, user_id: int):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
async def get_some_messages(cls, chat_id: int, message_number_from: int, messages_to_get: int):
|
||||
"""
|
||||
|
|
|
@ -5,6 +5,7 @@ from sqlalchemy import func, ForeignKey, DateTime
|
|||
from sqlalchemy.orm import mapped_column, Mapped, relationship
|
||||
|
||||
from app.database import Base
|
||||
# from app.users.models import Users
|
||||
|
||||
|
||||
class Chats(Base):
|
||||
|
@ -13,13 +14,17 @@ class Chats(Base):
|
|||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
chat_for = mapped_column(ForeignKey("users.id"))
|
||||
chat_name: Mapped[str]
|
||||
visibility: Mapped[bool] = mapped_column(server_default='true')
|
||||
created_by: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
||||
|
||||
message = relationship("Messages", back_populates="chat")
|
||||
usersxchats = relationship("UsersXChats", back_populates="chat")
|
||||
user_to_exclude = relationship("Users", back_populates="chat")
|
||||
user_to_exclude = relationship("Users", primaryjoin='Chats.chat_for == Users.id', back_populates="chat")
|
||||
user_who_create = relationship("Users", primaryjoin='Chats.created_by == Users.id', back_populates="creator")
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return f"Чат #{self.id}."
|
||||
return f"Чат #{self.id}. Виден: {self.visibility}. Сделан {self.created_by}"
|
||||
|
||||
|
||||
class Messages(Base):
|
||||
|
|
|
@ -51,7 +51,6 @@ async def delete_message_from_chat(
|
|||
return deleted_message
|
||||
|
||||
|
||||
|
||||
@router.post("/create_chat", status_code=status.HTTP_201_CREATED)
|
||||
async def create_chat(
|
||||
user_to_exclude: int,
|
||||
|
@ -60,7 +59,7 @@ async def create_chat(
|
|||
):
|
||||
if user.id == user_to_exclude:
|
||||
raise UserCanNotReadThisChatException
|
||||
chat_id = 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, created_by=user.id)
|
||||
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
|
||||
|
|
|
@ -57,6 +57,7 @@ class UserDAO(BaseDAO):
|
|||
chats_with_descriptions.c.chat_id,
|
||||
chats_with_descriptions.c.chat_for,
|
||||
chats_with_descriptions.c.chat_name,
|
||||
chats_with_descriptions.c.visibility,
|
||||
Users.id,
|
||||
Users.avatar_image,
|
||||
)
|
||||
|
@ -70,7 +71,11 @@ class UserDAO(BaseDAO):
|
|||
chats_with_avatars.c.avatar_image
|
||||
)
|
||||
.select_from(chats_with_avatars)
|
||||
.where(chats_with_avatars.c.id == user_id))
|
||||
.where(
|
||||
and_(
|
||||
chats_with_avatars.c.id == user_id,
|
||||
chats_with_avatars.c.visibility == True
|
||||
)))
|
||||
async with async_session_maker() as session:
|
||||
result = await session.execute(query)
|
||||
result = result.mappings().all()
|
||||
|
|
|
@ -5,6 +5,7 @@ from sqlalchemy import func, ForeignKey, DateTime
|
|||
from sqlalchemy.orm import mapped_column, Mapped, relationship
|
||||
|
||||
from app.database import Base
|
||||
# from app.users.chat.models import Chats
|
||||
|
||||
|
||||
class Users(Base):
|
||||
|
@ -22,7 +23,8 @@ class Users(Base):
|
|||
|
||||
message = relationship("Messages", back_populates="user")
|
||||
usersxchats = relationship("UsersXChats", back_populates="user")
|
||||
chat = relationship("Chats", back_populates="user_to_exclude")
|
||||
chat = relationship("Chats", primaryjoin='Chats.chat_for == Users.id', back_populates="user_to_exclude")
|
||||
creator = relationship("Chats", primaryjoin='Chats.created_by == Users.id', back_populates="user_who_create")
|
||||
verificationcode = relationship("UsersVerificationCodes", back_populates="user")
|
||||
|
||||
def __str__(self):
|
||||
|
|
Loading…
Add table
Reference in a new issue