96 lines
3.9 KiB
Python
96 lines
3.9 KiB
Python
from datetime import datetime
|
|
|
|
from sqlalchemy import func, ForeignKey, DateTime
|
|
from sqlalchemy.orm import mapped_column, Mapped, relationship
|
|
from sqlalchemy.ext.associationproxy import association_proxy
|
|
|
|
from app.database import Base
|
|
|
|
|
|
class Chats(Base):
|
|
__tablename__ = "chats"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
created_by: Mapped[int] = mapped_column(ForeignKey("users.id"))
|
|
chat_for = mapped_column(ForeignKey("users.id"))
|
|
chat_name: Mapped[str]
|
|
visibility: Mapped[bool] = mapped_column(server_default='true')
|
|
|
|
message = relationship("Messages", back_populates="chat")
|
|
usersxchats = relationship("UsersXChats", 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")
|
|
pinned_messages = relationship("PinnedMessages", back_populates="chat")
|
|
pinned_chats = relationship("PinnedChats", back_populates="chat")
|
|
|
|
def __str__(self):
|
|
return f"Чат #{self.id}. Виден: {self.visibility}. Сделан {self.created_by} для {self.chat_for}"
|
|
|
|
|
|
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 | None]
|
|
image_url: Mapped[str | None]
|
|
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")
|
|
pinned_messages = relationship("PinnedMessages", back_populates="message")
|
|
self_id = relationship("Answers", primaryjoin='Messages.id == Answers.self_id', back_populates="self_message")
|
|
answer_id = relationship("Answers", primaryjoin='Messages.id == Answers.answer_id', back_populates="answer_message")
|
|
|
|
def __str__(self):
|
|
return f"#{self.id} {self.message} от {self.user_id}. Написано {self.created_at}"
|
|
|
|
|
|
class Answers(Base):
|
|
__tablename__ = "answers"
|
|
|
|
self_id: Mapped[int] = mapped_column(ForeignKey('messages.id'), primary_key=True)
|
|
answer_id: Mapped[int] = mapped_column(ForeignKey('messages.id'))
|
|
|
|
self_message = relationship("Messages", primaryjoin="Answers.self_id == Messages.id", back_populates="self_id")
|
|
answer_message = relationship("Messages", primaryjoin="Answers.answer_id == Messages.id", back_populates="answer_id")
|
|
|
|
|
|
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}"
|
|
|
|
|
|
class PinnedMessages(Base):
|
|
__tablename__ = "pinnedmessages"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
chat_id = mapped_column(ForeignKey("chats.id"))
|
|
message_id = mapped_column(ForeignKey("messages.id"))
|
|
user_id = mapped_column(ForeignKey("users.id"))
|
|
|
|
chat = relationship("Chats", back_populates="pinned_messages")
|
|
message = relationship("Messages", back_populates="pinned_messages")
|
|
user = relationship("Users", back_populates="pinned_messages")
|
|
|
|
|
|
class PinnedChats(Base):
|
|
__tablename__ = "pinnedchats"
|
|
|
|
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="pinned_chats")
|
|
user = relationship("Users", back_populates="pinned_chats")
|