Files
SakuraHub/sakura_cloud/models/novel.py
T

50 lines
2.9 KiB
Python
Raw Normal View History

2026-11-16 20:45:22 +08:00
from __future__ import annotations
from datetime import datetime
from sqlalchemy import BigInteger, Boolean, DateTime, ForeignKey, Index, Integer, JSON, String, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sakura_cloud.db.base import Base
from sakura_cloud.models.mixins import TimestampMixin, UUIDPrimaryKeyMixin
class Novel(UUIDPrimaryKeyMixin, TimestampMixin, Base):
__tablename__ = "novels"
__table_args__ = (
Index("ix_novels_user_viewed", "user_id", "last_viewed_at"),
Index("ix_novels_user_read", "user_id", "last_read_at"),
Index("ix_novels_user_series_chapter", "user_id", "series_id", "chapter_order"),
Index("ix_novels_user_updated_desc", "user_id", "updated_at"),
Index("ix_novels_series_chapter_order", "series_id", "chapter_order"),
Index("ix_novels_user_favorite_viewed", "user_id", "is_favorite", "last_viewed_at"),
)
user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
file_id: Mapped[str | None] = mapped_column(String(36), ForeignKey("user_files.id", ondelete="SET NULL"), nullable=True)
source_id: Mapped[str | None] = mapped_column(String(50), nullable=True)
source_platform: Mapped[str] = mapped_column(String(20), nullable=False, index=True)
title: Mapped[str] = mapped_column(String(500), nullable=False)
author: Mapped[str | None] = mapped_column(String(200), nullable=True, index=True)
series_id: Mapped[str | None] = mapped_column(String(50), nullable=True, index=True)
series_title: Mapped[str | None] = mapped_column(String(500), nullable=True)
chapter_title: Mapped[str | None] = mapped_column(String(500), nullable=True)
chapter_order: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
description: Mapped[str | None] = mapped_column(Text, nullable=True)
file_path: Mapped[str] = mapped_column(Text, nullable=False)
file_size: Mapped[int] = mapped_column(BigInteger, default=0, nullable=False)
cover_path: Mapped[str | None] = mapped_column(Text, nullable=True)
word_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
current_page: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
bookmark: Mapped[str | None] = mapped_column(Text, nullable=True)
tags: Mapped[list[str]] = mapped_column(JSON, default=list, nullable=False)
is_favorite: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False, index=True)
last_read_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
last_viewed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True, index=True)
user = relationship("User", back_populates="novels")
file = relationship("UserFile")
@property
def content_url(self) -> str | None:
return f"/api/v1/files/{self.file_id}/content" if self.file_id else None