Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Ensure streams object is recreated when loading from database an… #513

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions backend/program/media/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import relationship
import sqlalchemy
from sqlalchemy import orm

from program.db.db import db

Expand All @@ -30,7 +31,6 @@ class MediaItem(db.Model):
scraped_at: Mapped[Optional[datetime]] = mapped_column(sqlalchemy.DateTime, nullable=True)
scraped_times: Mapped[Optional[int]] = mapped_column(sqlalchemy.Integer, default=0)
active_stream: Mapped[Optional[dict[str, str]]] = mapped_column(sqlalchemy.JSON, nullable=True)
streams: Optional[dict[str, Torrent]] = {}
symlinked: Mapped[Optional[bool]] = mapped_column(sqlalchemy.Boolean, default=False)
symlinked_at: Mapped[Optional[datetime]] = mapped_column(sqlalchemy.DateTime, nullable=True)
symlinked_times: Mapped[Optional[int]] = mapped_column(sqlalchemy.Integer, default=0)
Expand Down Expand Up @@ -59,7 +59,9 @@ class MediaItem(db.Model):
"polymorphic_on":"type",
"with_polymorphic":"*",
}

@orm.reconstructor
def init_on_load(self):
self.streams: Optional[dict[str, Torrent]] = {}
def __init__(self, item: dict) -> None:
# id: Mapped[int] = mapped_column(primary_key=True)
# name: Mapped[str] = mapped_column(String(30))
Expand Down Expand Up @@ -292,7 +294,10 @@ class Movie(MediaItem):
"polymorphic_identity": "movie",
"polymorphic_load": "inline",
}

@orm.reconstructor
def init_on_load(self):
self.streams: Optional[dict[str, Torrent]] = {}

def __init__(self, item):
self.type = "movie"
self.file = item.get("file", None)
Expand All @@ -312,7 +317,10 @@ class Season(MediaItem):
parent_id: Mapped[int] = mapped_column(sqlalchemy.ForeignKey("Show._id"), use_existing_column=True)
parent: Mapped["Show"] = relationship(lazy=False, back_populates="seasons", foreign_keys="Season.parent_id")
episodes: Mapped[List["Episode"]] = relationship(lazy=False, back_populates="parent", single_parent=True, cascade="all, delete-orphan", foreign_keys="Episode.parent_id")

@orm.reconstructor
def init_on_load(self):
self.streams: Optional[dict[str, Torrent]] = {}

__mapper_args__ = {
"polymorphic_identity": "season",
"polymorphic_load": "inline",
Expand Down Expand Up @@ -405,7 +413,10 @@ class Episode(MediaItem):
_id: Mapped[int] = mapped_column(sqlalchemy.ForeignKey("MediaItem._id"), primary_key=True)
parent_id: Mapped[int] = mapped_column(sqlalchemy.ForeignKey("Season._id"), use_existing_column=True)
parent: Mapped["Season"] = relationship(lazy=False, back_populates='episodes', foreign_keys="Episode.parent_id")

@orm.reconstructor
def init_on_load(self):
self.streams: Optional[dict[str, Torrent]] = {}

__mapper_args__ = {
"polymorphic_identity": "episode",
"polymorphic_load": "inline",
Expand Down Expand Up @@ -457,7 +468,10 @@ class Show(MediaItem):
__tablename__ = "Show"
_id: Mapped[int] = mapped_column(sqlalchemy.ForeignKey("MediaItem._id"), primary_key=True)
seasons: Mapped[List["Season"]] = relationship(lazy=False, back_populates="parent", single_parent=True, cascade="all, delete-orphan", foreign_keys="Season.parent_id")

@orm.reconstructor
def init_on_load(self):
self.streams: Optional[dict[str, Torrent]] = {}

__mapper_args__ = {
"polymorphic_identity": "show",
"polymorphic_load": "inline",
Expand Down