commit
9c5fef1a4d
412 files changed
+94582
No files matched your search
@@ -0,0 +1,26 @@
|
||||
from sakura_cloud.models.admin import AdminAuditLog
|
||||
from sakura_cloud.models.artwork import Artwork
|
||||
from sakura_cloud.models.device import Device
|
||||
from sakura_cloud.models.file import FileBlob, UploadSession, UserFile
|
||||
from sakura_cloud.models.follow import CreatorFollow, UpdateAlert
|
||||
from sakura_cloud.models.feed_cache import FeedCache
|
||||
from sakura_cloud.models.novel import Novel
|
||||
from sakura_cloud.models.ocr import ArtworkOcrText
|
||||
from sakura_cloud.models.push import PushDeviceToken
|
||||
from sakura_cloud.models.user import User
|
||||
|
||||
__all__ = [
|
||||
"Artwork",
|
||||
"ArtworkOcrText",
|
||||
"AdminAuditLog",
|
||||
"CreatorFollow",
|
||||
"Device",
|
||||
"FileBlob",
|
||||
"FeedCache",
|
||||
"Novel",
|
||||
"PushDeviceToken",
|
||||
"UpdateAlert",
|
||||
"UploadSession",
|
||||
"User",
|
||||
"UserFile",
|
||||
]
|
||||
@@ -0,0 +1,29 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import DateTime, ForeignKey, Index, JSON, String, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from sakura_cloud.db.base import Base
|
||||
from sakura_cloud.models.mixins import UUIDPrimaryKeyMixin, utc_now
|
||||
|
||||
|
||||
class AdminAuditLog(UUIDPrimaryKeyMixin, Base):
|
||||
__tablename__ = "admin_audit_logs"
|
||||
__table_args__ = (
|
||||
Index("ix_admin_audit_logs_admin_created", "admin_user_id", "created_at"),
|
||||
Index("ix_admin_audit_logs_action_created", "action", "created_at"),
|
||||
Index("ix_admin_audit_logs_target", "target_type", "target_id"),
|
||||
)
|
||||
|
||||
admin_user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
|
||||
action: Mapped[str] = mapped_column(String(100), nullable=False, index=True)
|
||||
target_type: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
target_id: Mapped[str | None] = mapped_column(String(100), nullable=True)
|
||||
ip_address: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
||||
user_agent: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
detail_json: Mapped[dict] = mapped_column("detail_json", JSON, default=dict, nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now, nullable=False)
|
||||
|
||||
admin_user = relationship("User")
|
||||
@@ -0,0 +1,63 @@
|
||||
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 Artwork(UUIDPrimaryKeyMixin, TimestampMixin, Base):
|
||||
__tablename__ = "artworks"
|
||||
__table_args__ = (
|
||||
Index("ix_artworks_user_content_viewed", "user_id", "content_type", "last_viewed_at"),
|
||||
Index("ix_artworks_user_created", "user_id", "created_at"),
|
||||
Index("ix_artworks_user_created_desc", "user_id", "created_at"),
|
||||
Index("ix_artworks_user_updated", "user_id", "updated_at"),
|
||||
Index("ix_artworks_user_updated_desc", "user_id", "updated_at"),
|
||||
Index("ix_artworks_user_content_created_desc", "user_id", "content_type", "created_at"),
|
||||
Index("ix_artworks_user_content_updated_desc", "user_id", "content_type", "updated_at"),
|
||||
Index("ix_artworks_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)
|
||||
content_type: Mapped[str] = mapped_column(String(20), default="illust", 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)
|
||||
author_id: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
page_count: Mapped[int] = mapped_column(Integer, default=1, nullable=False)
|
||||
file_path: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
file_size: Mapped[int] = mapped_column(BigInteger, default=0, nullable=False)
|
||||
thumbnail_path: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
mime_type: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
width: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||
height: Mapped[int | None] = mapped_column(Integer, 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)
|
||||
artwork_metadata: Mapped[dict] = mapped_column("metadata", JSON, default=dict, nullable=False)
|
||||
last_viewed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True, index=True)
|
||||
|
||||
user = relationship("User", back_populates="artworks")
|
||||
file = relationship("UserFile")
|
||||
ocr_text = relationship("ArtworkOcrText", back_populates="artwork", cascade="all, delete-orphan", uselist=False)
|
||||
|
||||
@property
|
||||
def image_url(self) -> str | None:
|
||||
if self.file_id:
|
||||
return f"/api/v1/files/{self.file_id}/content"
|
||||
if self.file_path.startswith(("http://", "https://")):
|
||||
return self.file_path
|
||||
return None
|
||||
|
||||
@property
|
||||
def thumbnail_url(self) -> str | None:
|
||||
if self.file_id:
|
||||
return f"/api/v1/files/{self.file_id}/thumbnail"
|
||||
if self.thumbnail_path and self.thumbnail_path.startswith(("http://", "https://")):
|
||||
return self.thumbnail_path
|
||||
return None
|
||||
@@ -0,0 +1,27 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import DateTime, ForeignKey, Index, String
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from sakura_cloud.db.base import Base
|
||||
from sakura_cloud.models.mixins import TimestampMixin, UUIDPrimaryKeyMixin
|
||||
|
||||
|
||||
class Device(UUIDPrimaryKeyMixin, TimestampMixin, Base):
|
||||
__tablename__ = "devices"
|
||||
|
||||
user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
|
||||
device_name: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||
device_type: Mapped[str] = mapped_column(String(20), nullable=False)
|
||||
device_token: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
last_active_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
last_sync_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
|
||||
user = relationship("User", back_populates="devices")
|
||||
|
||||
__table_args__ = (
|
||||
Index("idx_devices_user_id", "user_id"),
|
||||
Index("idx_devices_last_active", "last_active_at"),
|
||||
)
|
||||
@@ -0,0 +1,22 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import DateTime, ForeignKey, Index, JSON, String, UniqueConstraint
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from sakura_cloud.db.base import Base
|
||||
from sakura_cloud.models.mixins import TimestampMixin, UUIDPrimaryKeyMixin
|
||||
|
||||
|
||||
class FeedCache(UUIDPrimaryKeyMixin, TimestampMixin, Base):
|
||||
__tablename__ = "feed_cache"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("user_id", "cache_key", name="uq_feed_cache_user_key"),
|
||||
Index("ix_feed_cache_user_expires", "user_id", "expires_at"),
|
||||
)
|
||||
|
||||
user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
|
||||
cache_key: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||
payload: Mapped[list[dict]] = mapped_column(JSON, default=list, nullable=False)
|
||||
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
@@ -0,0 +1,55 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import BigInteger, DateTime, ForeignKey, Index, 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, utc_now
|
||||
|
||||
|
||||
class FileBlob(Base):
|
||||
__tablename__ = "file_blobs"
|
||||
|
||||
sha256: Mapped[str] = mapped_column(String(64), primary_key=True)
|
||||
storage_path: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
thumbnail_path: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
file_size: Mapped[int] = mapped_column(BigInteger, nullable=False)
|
||||
mime_type: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now, nullable=False)
|
||||
|
||||
user_files = relationship("UserFile", back_populates="blob")
|
||||
|
||||
|
||||
class UserFile(UUIDPrimaryKeyMixin, Base):
|
||||
__tablename__ = "user_files"
|
||||
|
||||
user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
|
||||
file_sha256: Mapped[str] = mapped_column(String(64), ForeignKey("file_blobs.sha256", ondelete="CASCADE"), nullable=False)
|
||||
original_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
uploaded_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now, nullable=False)
|
||||
|
||||
user = relationship("User", back_populates="files")
|
||||
blob = relationship("FileBlob", back_populates="user_files")
|
||||
|
||||
__table_args__ = (Index("idx_user_files_user_id", "user_id"),)
|
||||
|
||||
|
||||
class UploadSession(UUIDPrimaryKeyMixin, TimestampMixin, Base):
|
||||
__tablename__ = "upload_sessions"
|
||||
|
||||
user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
|
||||
sha256: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
||||
file_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
file_size: Mapped[int] = mapped_column(BigInteger, nullable=False)
|
||||
mime_type: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
resource_type: Mapped[str] = mapped_column(String(20), default="image", nullable=False)
|
||||
chunk_size: Mapped[int] = mapped_column(BigInteger, nullable=False)
|
||||
received_size: Mapped[int] = mapped_column(BigInteger, default=0, nullable=False)
|
||||
temp_path: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
status: Mapped[str] = mapped_column(String(20), default="pending", nullable=False)
|
||||
|
||||
user = relationship("User")
|
||||
|
||||
__table_args__ = (Index("idx_upload_sessions_user_id", "user_id"),)
|
||||
@@ -0,0 +1,60 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import Boolean, DateTime, ForeignKey, Index, 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 CreatorFollow(UUIDPrimaryKeyMixin, TimestampMixin, Base):
|
||||
__tablename__ = "creator_follows"
|
||||
__table_args__ = (
|
||||
Index("ix_creator_follows_user_enabled_updated", "user_id", "enabled", "updated_at"),
|
||||
Index("ix_creator_follows_user_platform_creator", "user_id", "platform", "creator_id"),
|
||||
)
|
||||
|
||||
user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True)
|
||||
platform: Mapped[str] = mapped_column(String(20), nullable=False, index=True)
|
||||
creator_id: Mapped[str] = mapped_column(String(100), nullable=False, index=True)
|
||||
creator_name: Mapped[str | None] = mapped_column(String(200), nullable=True)
|
||||
creator_handle: Mapped[str | None] = mapped_column(String(200), nullable=True)
|
||||
profile_url: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
avatar_url: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
enabled: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False, index=True)
|
||||
last_checked_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
latest_published_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
metadata_json: Mapped[dict] = mapped_column("metadata", JSON, default=dict, nullable=False)
|
||||
|
||||
user = relationship("User", back_populates="creator_follows")
|
||||
|
||||
|
||||
class UpdateAlert(UUIDPrimaryKeyMixin, TimestampMixin, Base):
|
||||
__tablename__ = "update_alerts"
|
||||
__table_args__ = (
|
||||
Index("ix_update_alerts_user_read_created", "user_id", "is_read", "created_at"),
|
||||
Index("ix_update_alerts_user_created", "user_id", "created_at"),
|
||||
Index("ix_update_alerts_user_platform_creator", "user_id", "platform", "creator_id"),
|
||||
)
|
||||
|
||||
user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True)
|
||||
follow_id: Mapped[str | None] = mapped_column(
|
||||
String(36),
|
||||
ForeignKey("creator_follows.id", ondelete="SET NULL"),
|
||||
nullable=True,
|
||||
index=True,
|
||||
)
|
||||
platform: Mapped[str] = mapped_column(String(20), nullable=False, index=True)
|
||||
creator_id: Mapped[str] = mapped_column(String(100), nullable=False, index=True)
|
||||
title: Mapped[str] = mapped_column(String(500), nullable=False)
|
||||
body: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
content_type: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
||||
target_url: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
cover_url: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
is_read: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False, index=True)
|
||||
payload_json: Mapped[dict] = mapped_column("payload", JSON, default=dict, nullable=False)
|
||||
|
||||
user = relationship("User", back_populates="update_alerts")
|
||||
follow = relationship("CreatorFollow")
|
||||
@@ -0,0 +1,29 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy import DateTime, String
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
|
||||
def new_uuid() -> str:
|
||||
return str(uuid.uuid4())
|
||||
|
||||
|
||||
def utc_now() -> datetime:
|
||||
return datetime.now(timezone.utc)
|
||||
|
||||
|
||||
class UUIDPrimaryKeyMixin:
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=new_uuid)
|
||||
|
||||
|
||||
class TimestampMixin:
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utc_now, nullable=False)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
default=utc_now,
|
||||
onupdate=utc_now,
|
||||
nullable=False,
|
||||
)
|
||||
@@ -0,0 +1,50 @@
|
||||
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
|
||||
@@ -0,0 +1,33 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import Float, ForeignKey, Index, Integer, String, Text, UniqueConstraint
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from sakura_cloud.db.base import Base
|
||||
from sakura_cloud.models.mixins import TimestampMixin, UUIDPrimaryKeyMixin
|
||||
|
||||
|
||||
class ArtworkOcrText(UUIDPrimaryKeyMixin, TimestampMixin, Base):
|
||||
__tablename__ = "artwork_ocr_texts"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("user_id", "artwork_id", name="uq_artwork_ocr_texts_user_artwork"),
|
||||
Index("ix_artwork_ocr_texts_user_artwork", "user_id", "artwork_id"),
|
||||
Index("ix_artwork_ocr_texts_user_status", "user_id", "status"),
|
||||
Index("ix_artwork_ocr_texts_user_source", "user_id", "source_platform", "source_id"),
|
||||
)
|
||||
|
||||
user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
|
||||
artwork_id: Mapped[str] = mapped_column(String(36), ForeignKey("artworks.id", ondelete="CASCADE"), nullable=False)
|
||||
file_id: Mapped[str | None] = mapped_column(String(36), ForeignKey("user_files.id", ondelete="SET NULL"), nullable=True)
|
||||
source_platform: Mapped[str | None] = mapped_column(String(20), nullable=True)
|
||||
source_id: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
language: Mapped[str] = mapped_column(String(20), default="mixed", nullable=False)
|
||||
ocr_text: Mapped[str] = mapped_column(Text, default="", nullable=False)
|
||||
confidence: Mapped[float | None] = mapped_column(Float, nullable=True)
|
||||
engine: Mapped[str] = mapped_column(String(50), default="paddleocr", nullable=False)
|
||||
status: Mapped[str] = mapped_column(String(20), default="pending", nullable=False, index=True)
|
||||
attempts: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
|
||||
error: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
|
||||
artwork = relationship("Artwork", back_populates="ocr_text")
|
||||
user = relationship("User", back_populates="artwork_ocr_texts")
|
||||
@@ -0,0 +1,26 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import Boolean, DateTime, ForeignKey, Index, String, Text, UniqueConstraint
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from sakura_cloud.db.base import Base
|
||||
from sakura_cloud.models.mixins import TimestampMixin, UUIDPrimaryKeyMixin
|
||||
|
||||
|
||||
class PushDeviceToken(UUIDPrimaryKeyMixin, TimestampMixin, Base):
|
||||
__tablename__ = "push_device_tokens"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("user_id", "device_id", name="uq_push_device_tokens_user_device"),
|
||||
Index("ix_push_device_tokens_user_active", "user_id", "is_active"),
|
||||
)
|
||||
|
||||
user_id: Mapped[str] = mapped_column(String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
|
||||
device_id: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||
platform: Mapped[str] = mapped_column(String(20), nullable=False)
|
||||
fcm_token: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False, index=True)
|
||||
last_seen_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
|
||||
user = relationship("User", back_populates="push_device_tokens")
|
||||
@@ -0,0 +1,56 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
|
||||
from sqlalchemy import BigInteger, DateTime, Enum as SqlEnum, Index, String, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from sakura_cloud.core.config import settings
|
||||
from sakura_cloud.db.base import Base
|
||||
from sakura_cloud.models.mixins import TimestampMixin, UUIDPrimaryKeyMixin
|
||||
|
||||
|
||||
class UserRole(str, Enum):
|
||||
ADMIN = "admin"
|
||||
SUPER_ADMIN = "super_admin"
|
||||
USER = "user"
|
||||
|
||||
|
||||
class User(UUIDPrimaryKeyMixin, TimestampMixin, Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
username: Mapped[str] = mapped_column(String(50), nullable=False, unique=True)
|
||||
email: Mapped[str] = mapped_column(String(100), nullable=False, unique=True)
|
||||
password_hash: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
avatar: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
storage_used: Mapped[int] = mapped_column(BigInteger, default=0, nullable=False)
|
||||
storage_limit: Mapped[int] = mapped_column(
|
||||
BigInteger,
|
||||
default=lambda: settings.default_storage_limit_bytes,
|
||||
nullable=False,
|
||||
)
|
||||
role: Mapped[UserRole] = mapped_column(SqlEnum(UserRole), default=UserRole.USER, nullable=False)
|
||||
is_active: Mapped[bool] = mapped_column(default=True, nullable=False, index=True)
|
||||
disabled_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
disabled_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
token_version: Mapped[int] = mapped_column(default=0, nullable=False)
|
||||
reminder_enabled: Mapped[bool] = mapped_column(default=True, nullable=False)
|
||||
reminder_hour: Mapped[int] = mapped_column(default=9, nullable=False)
|
||||
reminder_minute: Mapped[int] = mapped_column(default=0, nullable=False)
|
||||
reminder_timezone: Mapped[str] = mapped_column(String(64), default="Asia/Shanghai", nullable=False)
|
||||
last_reminder_sent_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
|
||||
artworks = relationship("Artwork", back_populates="user", cascade="all, delete-orphan")
|
||||
novels = relationship("Novel", back_populates="user", cascade="all, delete-orphan")
|
||||
devices = relationship("Device", back_populates="user", cascade="all, delete-orphan")
|
||||
files = relationship("UserFile", back_populates="user", cascade="all, delete-orphan")
|
||||
creator_follows = relationship("CreatorFollow", back_populates="user", cascade="all, delete-orphan")
|
||||
update_alerts = relationship("UpdateAlert", back_populates="user", cascade="all, delete-orphan")
|
||||
push_device_tokens = relationship("PushDeviceToken", back_populates="user", cascade="all, delete-orphan")
|
||||
artwork_ocr_texts = relationship("ArtworkOcrText", back_populates="user", cascade="all, delete-orphan")
|
||||
|
||||
__table_args__ = (
|
||||
Index("idx_users_username", "username"),
|
||||
Index("idx_users_email", "email"),
|
||||
)
|
||||
Reference in new issue
Block a user