nex_animation/backend/app/models/platform_assets.py

127 lines
6.2 KiB
Python

"""平台级数字资产:人物、场景、声线与可版本化的媒体文件。
与项目资产的边界:
- 平台资产可跨项目复用,是「素材母版」;
- 项目引入时默认复制一份快照,并记录来源与版本,后续制作不会受母版改动影响;
- 项目内仍可新建独有资产,也可把项目资产反向上架到平台。
"""
from __future__ import annotations
from datetime import datetime
from sqlalchemy import JSON, Boolean, DateTime, ForeignKey, Index, Integer, String, Text, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.db import Base
from app.models.base import TimestampMixin, now_utc
class PlatformVoice(TimestampMixin, Base):
__tablename__ = "platform_voices"
id: Mapped[int] = mapped_column(primary_key=True)
code: Mapped[str] = mapped_column(String(80), default="", index=True)
name: Mapped[str] = mapped_column(String(160))
provider: Mapped[str] = mapped_column(String(60), default="")
provider_voice_id: Mapped[str] = mapped_column(String(240), default="")
language: Mapped[str] = mapped_column(String(30), default="zh-CN")
gender: Mapped[str] = mapped_column(String(20), default="")
description: Mapped[str] = mapped_column(Text, default="")
tuning: Mapped[dict] = mapped_column(JSON, default=dict)
version: Mapped[str] = mapped_column(String(40), default="v1")
is_default: Mapped[bool] = mapped_column(Boolean, default=False)
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
tags: Mapped[list] = mapped_column(JSON, default=list)
class PlatformCharacter(TimestampMixin, Base):
__tablename__ = "platform_characters"
id: Mapped[int] = mapped_column(primary_key=True)
# 平台母版默认全局;绑定项目后只在对应项目中优先展示/管理。
project_id: Mapped[int | None] = mapped_column(
ForeignKey("projects.id", ondelete="SET NULL"), nullable=True, index=True
)
code: Mapped[str] = mapped_column(String(80), default="", index=True)
name: Mapped[str] = mapped_column(String(120))
role: Mapped[str] = mapped_column(String(60), default="配角")
personality: Mapped[str] = mapped_column(Text, default="")
appearance: Mapped[str] = mapped_column(Text, default="")
persona_prompt: Mapped[str] = mapped_column(Text, default="")
voice_profile: Mapped[str] = mapped_column(Text, default="")
default_voice_id: Mapped[int | None] = mapped_column(
ForeignKey("platform_voices.id", ondelete="SET NULL"), nullable=True
)
style_version: Mapped[str] = mapped_column(String(40), default="v1")
tags: Mapped[list] = mapped_column(JSON, default=list)
notes: Mapped[str] = mapped_column(Text, default="")
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
default_voice: Mapped["PlatformVoice | None"] = relationship(
foreign_keys=[default_voice_id], lazy="selectin"
)
class PlatformScene(TimestampMixin, Base):
__tablename__ = "platform_scenes"
id: Mapped[int] = mapped_column(primary_key=True)
code: Mapped[str] = mapped_column(String(80), default="", index=True)
name: Mapped[str] = mapped_column(String(160))
description: Mapped[str] = mapped_column(Text, default="")
prompt: Mapped[str] = mapped_column(Text, default="")
lighting: Mapped[str] = mapped_column(String(120), default="")
time_of_day: Mapped[str] = mapped_column(String(60), default="")
palette: Mapped[str] = mapped_column(String(160), default="")
tags: Mapped[list] = mapped_column(JSON, default=list)
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
class PlatformMedia(TimestampMixin, Base):
__tablename__ = "platform_media"
__table_args__ = (Index("ix_platform_media_owner", "owner_type", "owner_id", "kind"),)
id: Mapped[int] = mapped_column(primary_key=True)
owner_type: Mapped[str] = mapped_column(String(20), index=True) # human / scene / voice
owner_id: Mapped[int] = mapped_column(Integer, index=True)
kind: Mapped[str] = mapped_column(String(40), index=True)
name: Mapped[str] = mapped_column(String(200), default="")
note: Mapped[str] = mapped_column(Text, default="")
path: Mapped[str] = mapped_column(String(600))
checksum: Mapped[str] = mapped_column(String(80), default="")
mime: Mapped[str] = mapped_column(String(120), default="")
size_bytes: Mapped[int] = mapped_column(Integer, default=0)
width: Mapped[int | None] = mapped_column(Integer, nullable=True)
height: Mapped[int | None] = mapped_column(Integer, nullable=True)
duration_ms: Mapped[int | None] = mapped_column(Integer, nullable=True)
current_version: Mapped[int] = mapped_column(Integer, default=1)
metadata_json: Mapped[dict] = mapped_column(JSON, default=dict)
versions: Mapped[list["PlatformMediaVersion"]] = relationship(
back_populates="media", cascade="all, delete-orphan",
order_by="PlatformMediaVersion.version_no.desc()", lazy="selectin",
)
class PlatformMediaVersion(Base):
__tablename__ = "platform_media_versions"
__table_args__ = (UniqueConstraint("media_id", "version_no", name="uq_platform_media_version"),)
id: Mapped[int] = mapped_column(primary_key=True)
media_id: Mapped[int] = mapped_column(ForeignKey("platform_media.id", ondelete="CASCADE"), index=True)
version_no: Mapped[int] = mapped_column(Integer, default=1)
path: Mapped[str] = mapped_column(String(600))
checksum: Mapped[str] = mapped_column(String(80), default="")
source: Mapped[str] = mapped_column(String(30), default="upload")
note: Mapped[str] = mapped_column(Text, default="")
size_bytes: Mapped[int] = mapped_column(Integer, default=0)
# 与 AssetVersion 同构:见 backend/app/models/assets.py 的注释
mime: Mapped[str] = mapped_column(String(120), default="", nullable=True)
width: Mapped[int | None] = mapped_column(Integer, nullable=True)
height: Mapped[int | None] = mapped_column(Integer, nullable=True)
duration_ms: Mapped[int | None] = mapped_column(Integer, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=now_utc)
media: Mapped["PlatformMedia"] = relationship(back_populates="versions")