Arag / app /models /document.py
AuthorBot
Restructure project for HF Spaces deployment
772f852
Raw
History Blame Contribute Delete
2.03 kB
"""Author RAG Chatbot SaaS — Document Model."""
from sqlalchemy import BigInteger, ForeignKey, Integer, String, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.models.base import Base, TimestampMixin, generate_uuid
class Document(Base, TimestampMixin):
"""An uploaded training document associated with a book."""
__tablename__ = "documents"
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=generate_uuid)
author_id: Mapped[str] = mapped_column(
String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
)
book_id: Mapped[str] = mapped_column(
String(36), ForeignKey("books.id", ondelete="CASCADE"), nullable=False, index=True
)
# File metadata
filename: Mapped[str] = mapped_column(String(255), nullable=False)
original_filename: Mapped[str] = mapped_column(String(255), nullable=False)
file_extension: Mapped[str] = mapped_column(String(10), nullable=False)
file_size_bytes: Mapped[int] = mapped_column(BigInteger, nullable=False)
file_hash: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
storage_path: Mapped[str] = mapped_column(String(1000), nullable=False)
# Upload session
upload_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
# Processing
status: Mapped[str] = mapped_column(String(50), default="uploaded", nullable=False, index=True)
error_message: Mapped[str | None] = mapped_column(Text, nullable=True)
extracted_page_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
extracted_char_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
# Relationships
author: Mapped["User"] = relationship("User", back_populates="documents")
book: Mapped["Book"] = relationship("Book", back_populates="documents")
def __repr__(self) -> str:
return f"<Document id={self.id} file={self.original_filename!r} status={self.status!r}>"