92 lines
4.1 KiB
Python
92 lines
4.1 KiB
Python
|
|
from datetime import datetime
|
||
|
|
from typing import TYPE_CHECKING
|
||
|
|
|
||
|
|
if TYPE_CHECKING:
|
||
|
|
from app.models.folders import AssetFolder
|
||
|
|
|
||
|
|
|
||
|
|
from sqlalchemy import BigInteger, Boolean, ForeignKey, Integer, JSON, String, UniqueConstraint
|
||
|
|
|
||
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||
|
|
|
||
|
|
|
||
|
|
from app.models.base import Base, Timestamp, new_id, utcnow
|
||
|
|
|
||
|
|
|
||
|
|
class Asset(Base):
|
||
|
|
__tablename__ = "visual_assets"
|
||
|
|
__table_args__ = (
|
||
|
|
UniqueConstraint("source_job_id", "source_output_index"),
|
||
|
|
UniqueConstraint("source_upstream_id", "source_output_index"),
|
||
|
|
)
|
||
|
|
folder_links: Mapped[list["AssetFolder"]] = relationship(
|
||
|
|
"AssetFolder", lazy="selectin", cascade="all, delete-orphan", passive_deletes=True
|
||
|
|
)
|
||
|
|
|
||
|
|
def __init__(self, **kwargs):
|
||
|
|
kwargs.setdefault("folder_links", [])
|
||
|
|
super().__init__(**kwargs)
|
||
|
|
|
||
|
|
@property
|
||
|
|
def folder_ids(self):
|
||
|
|
return [link.folder_id for link in self.folder_links]
|
||
|
|
|
||
|
|
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=new_id)
|
||
|
|
owner_id: Mapped[str] = mapped_column(ForeignKey("visual_users.id"), index=True)
|
||
|
|
name: Mapped[str] = mapped_column(String(255))
|
||
|
|
storage_key: Mapped[str] = mapped_column(String(512), unique=True)
|
||
|
|
thumbnail_key: Mapped[str | None] = mapped_column(String(512), nullable=True)
|
||
|
|
kind: Mapped[str] = mapped_column(String(16))
|
||
|
|
mime_type: Mapped[str] = mapped_column(String(128))
|
||
|
|
size_bytes: Mapped[int] = mapped_column(BigInteger)
|
||
|
|
sha256: Mapped[str] = mapped_column(String(64))
|
||
|
|
width: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||
|
|
height: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||
|
|
duration_seconds: Mapped[float | None] = mapped_column(nullable=True)
|
||
|
|
tags: Mapped[list] = mapped_column(JSON, default=list)
|
||
|
|
favorite: Mapped[bool] = mapped_column(Boolean, default=False)
|
||
|
|
origin: Mapped[str] = mapped_column(String(16), default="upload")
|
||
|
|
library_scope: Mapped[str] = mapped_column(
|
||
|
|
String(16), default="private", server_default="private", index=True
|
||
|
|
)
|
||
|
|
ready: Mapped[bool] = mapped_column(Boolean, default=True)
|
||
|
|
source_job_id: Mapped[str | None] = mapped_column(
|
||
|
|
ForeignKey("visual_jobs.id"), nullable=True, index=True
|
||
|
|
)
|
||
|
|
source_upstream_id: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
||
|
|
source_output_index: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||
|
|
created_at: Mapped[datetime] = mapped_column(Timestamp, default=utcnow, index=True)
|
||
|
|
deleted_at: Mapped[datetime | None] = mapped_column(Timestamp, nullable=True)
|
||
|
|
|
||
|
|
|
||
|
|
class AssetReference(Base):
|
||
|
|
__tablename__ = "visual_asset_refs"
|
||
|
|
__table_args__ = (UniqueConstraint("asset_id", "ref_kind", "ref_id"),)
|
||
|
|
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=new_id)
|
||
|
|
owner_id: Mapped[str] = mapped_column(String(32), ForeignKey("visual_users.id"), index=True)
|
||
|
|
asset_id: Mapped[str] = mapped_column(String(32))
|
||
|
|
ref_kind: Mapped[str] = mapped_column(String(32))
|
||
|
|
ref_id: Mapped[str] = mapped_column(String(32))
|
||
|
|
|
||
|
|
|
||
|
|
class AssetTag(Base):
|
||
|
|
__tablename__ = "visual_asset_tags"
|
||
|
|
__table_args__ = (UniqueConstraint("asset_id", "tag"),)
|
||
|
|
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=new_id)
|
||
|
|
owner_id: Mapped[str] = mapped_column(String(32), ForeignKey("visual_users.id"), index=True)
|
||
|
|
asset_id: Mapped[str] = mapped_column(String(32))
|
||
|
|
tag: Mapped[str] = mapped_column(String(128))
|
||
|
|
|
||
|
|
|
||
|
|
class SharedAsset(Base):
|
||
|
|
__tablename__ = "visual_shared_assets"
|
||
|
|
id: Mapped[str] = mapped_column(String(32), primary_key=True, default=new_id)
|
||
|
|
publisher_id: Mapped[str] = mapped_column(String(32), ForeignKey("visual_users.id"), index=True)
|
||
|
|
asset_id: Mapped[str] = mapped_column(String(32), ForeignKey("visual_assets.id"), unique=True)
|
||
|
|
source_asset_id: Mapped[str | None] = mapped_column(
|
||
|
|
String(32), ForeignKey("visual_assets.id"), nullable=True, index=True
|
||
|
|
)
|
||
|
|
version: Mapped[int] = mapped_column(Integer, default=1, server_default="1")
|
||
|
|
created_at: Mapped[datetime] = mapped_column(Timestamp, default=utcnow, index=True)
|
||
|
|
withdrawn_at: Mapped[datetime | None] = mapped_column(Timestamp, nullable=True)
|