53 lines
1.7 KiB
Python
53 lines
1.7 KiB
Python
from datetime import datetime
|
|
from typing import Optional
|
|
|
|
from sqlalchemy import func, ForeignKey, DateTime
|
|
from sqlalchemy.orm import mapped_column, Mapped, relationship
|
|
|
|
from app.database import Base
|
|
|
|
|
|
class Chats(Base):
|
|
__tablename__ = "chats"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
chat_for = 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")
|
|
|
|
def __str__(self):
|
|
return f"Чат #{self.id}."
|
|
|
|
|
|
class Messages(Base):
|
|
__tablename__ = "messages"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
chat_id = mapped_column(ForeignKey("chats.id"))
|
|
user_id = mapped_column(ForeignKey("users.id"))
|
|
message: Mapped[str]
|
|
image_url: Mapped[Optional[str]]
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
|
visibility: Mapped[bool] = mapped_column(server_default='true')
|
|
|
|
chat = relationship("Chats", back_populates="message")
|
|
user = relationship("Users", back_populates="message")
|
|
|
|
def __str__(self):
|
|
return f"#{self.id} {self.text} от {self.user_id}. Написано {self.created_at}"
|
|
|
|
|
|
class UsersXChats(Base):
|
|
__tablename__ = "usersxchats"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
user_id = mapped_column(ForeignKey("users.id"))
|
|
chat_id = mapped_column(ForeignKey("chats.id"))
|
|
|
|
chat = relationship("Chats", back_populates="usersxchats")
|
|
user = relationship("Users", back_populates="usersxchats")
|
|
|
|
def __str__(self):
|
|
return f"Юзер #{self.user_id} допущен к чату {self.chat_id}"
|