46 lines
1.6 KiB
Python
46 lines
1.6 KiB
Python
from __future__ import annotations
|
||
|
||
import uuid
|
||
from datetime import datetime, timezone
|
||
|
||
from sqlalchemy import Boolean, DateTime, ForeignKey, String, Text
|
||
from sqlalchemy.dialects.postgresql import UUID
|
||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
||
|
||
|
||
class Base(DeclarativeBase):
|
||
pass
|
||
|
||
|
||
def _now():
|
||
return datetime.now(timezone.utc)
|
||
|
||
|
||
class User(Base):
|
||
__tablename__ = "users"
|
||
|
||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||
email: Mapped[str] = mapped_column(String(320), unique=True, index=True, nullable=False)
|
||
full_name: Mapped[str | None] = mapped_column(String(200), nullable=True)
|
||
password_hash: Mapped[str] = mapped_column(String(255), nullable=False)
|
||
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
|
||
is_admin: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
|
||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now, nullable=False)
|
||
|
||
|
||
class UserChatMessage(Base):
|
||
"""История чата с ИИ (Ollama) по пользователю."""
|
||
|
||
__tablename__ = "user_chat_messages"
|
||
|
||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||
user_id: Mapped[uuid.UUID] = mapped_column(
|
||
ForeignKey("users.id", ondelete="CASCADE"),
|
||
index=True,
|
||
nullable=False,
|
||
)
|
||
role: Mapped[str] = mapped_column(String(32), nullable=False)
|
||
content: Mapped[str] = mapped_column(Text, nullable=False)
|
||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=_now, nullable=False)
|
||
|