forked from mtusi2026/mtusi2026lab3
83 lines
2.5 KiB
Python
83 lines
2.5 KiB
Python
"""ORM-модели таблиц; схема и ограничения задаются миграциями."""
|
|
|
|
import uuid
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import DateTime, func
|
|
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
|
|
|
NOW = func.clock_timestamp()
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
"""Базовый класс моделей."""
|
|
|
|
|
|
class User(Base):
|
|
"""Пользователь; пароль хранится только как хеш."""
|
|
|
|
__tablename__ = "users"
|
|
id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
|
|
username: Mapped[str]
|
|
display_name: Mapped[str]
|
|
password_hash: Mapped[str]
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=NOW
|
|
)
|
|
|
|
|
|
class Chat(Base):
|
|
"""Чат."""
|
|
|
|
__tablename__ = "chats"
|
|
id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
|
|
title: Mapped[str]
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=NOW
|
|
)
|
|
|
|
|
|
class ChatMember(Base):
|
|
"""Участие пользователя в чате."""
|
|
|
|
__tablename__ = "chat_members"
|
|
chat_id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
|
|
user_id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
|
|
|
|
|
|
class Message(Base):
|
|
"""Сообщение чата."""
|
|
|
|
__tablename__ = "messages"
|
|
id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
|
|
chat_id: Mapped[uuid.UUID]
|
|
sender_id: Mapped[uuid.UUID]
|
|
text: Mapped[str]
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=NOW
|
|
)
|
|
|
|
|
|
class SessionRow(Base):
|
|
"""Сессия входа; хранится SHA-256 токена."""
|
|
|
|
__tablename__ = "sessions"
|
|
token_hash: Mapped[str] = mapped_column(primary_key=True)
|
|
user_id: Mapped[uuid.UUID]
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=NOW
|
|
)
|
|
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True))
|
|
|
|
|
|
class MessageRequest(Base):
|
|
"""Общий результат POST сообщения; область ключа — пользователь и чат."""
|
|
|
|
__tablename__ = "message_requests"
|
|
user_id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
|
|
chat_id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
|
|
key: Mapped[str] = mapped_column(primary_key=True)
|
|
request_text: Mapped[str]
|
|
message_id: Mapped[uuid.UUID]
|
|
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True))
|