This repository has been archived on 2026-05-31. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
GallerySubscriber/backend/app/models/subscription.py
T
2026-01-25 18:47:06 -05:00

73 lines
2.6 KiB
Python

"""Subscription model - represents an artist/creator to track."""
from datetime import datetime
from typing import Optional, TYPE_CHECKING
from sqlalchemy import String, Boolean, Integer, Text
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.models.base import Base, TimestampMixin, format_datetime
if TYPE_CHECKING:
from app.models.source import Source
class Subscription(Base, TimestampMixin):
"""A subscription represents an artist/creator being tracked.
Each subscription can have multiple sources (platform URLs).
The subscription name is used for the directory structure:
/{download_path}/{subscription.name}/{source.platform}/{post}
"""
__tablename__ = "subscriptions"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String(255), nullable=False, unique=True, index=True)
enabled: Mapped[bool] = mapped_column(Boolean, default=True)
priority: Mapped[int] = mapped_column(Integer, default=0)
# Optional description/notes
description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
# Flexible metadata storage (tags, custom settings, etc.)
metadata_: Mapped[dict] = mapped_column("metadata", JSONB, default=dict)
# Relationships
sources: Mapped[list["Source"]] = relationship(
back_populates="subscription",
cascade="all, delete-orphan",
lazy="selectin"
)
def __repr__(self) -> str:
return f"<Subscription {self.name}>"
@property
def platform_count(self) -> int:
"""Number of platforms/sources for this subscription."""
return len(self.sources) if self.sources else 0
@property
def enabled_source_count(self) -> int:
"""Number of enabled sources."""
return sum(1 for s in self.sources if s.enabled) if self.sources else 0
def to_dict(self, include_sources: bool = True) -> dict:
"""Convert to dictionary for API responses."""
result = {
"id": self.id,
"name": self.name,
"enabled": self.enabled,
"priority": self.priority,
"description": self.description,
"metadata": self.metadata_,
"platform_count": self.platform_count,
"enabled_source_count": self.enabled_source_count,
"created_at": format_datetime(self.created_at),
"updated_at": format_datetime(self.updated_at),
}
if include_sources and self.sources:
result["sources"] = [s.to_dict(include_subscription=False) for s in self.sources]
return result