refactor: Remove unused playlist routes and related logic; clean up sound and stream models
This commit is contained in:
@@ -81,19 +81,6 @@ class Playlist(db.Model):
|
||||
),
|
||||
}
|
||||
|
||||
def to_detailed_dict(self) -> dict:
|
||||
"""Convert playlist to detailed dictionary with sounds."""
|
||||
playlist_dict = self.to_dict()
|
||||
playlist_dict["sounds"] = [
|
||||
{
|
||||
"sound": ps.sound.to_dict() if ps.sound else None,
|
||||
"order": ps.order,
|
||||
"added_at": ps.added_at.isoformat() if ps.added_at else None,
|
||||
}
|
||||
for ps in sorted(self.playlist_sounds, key=lambda x: x.order)
|
||||
]
|
||||
return playlist_dict
|
||||
|
||||
@classmethod
|
||||
def create_playlist(
|
||||
cls,
|
||||
@@ -123,26 +110,6 @@ class Playlist(db.Model):
|
||||
|
||||
return playlist
|
||||
|
||||
@classmethod
|
||||
def find_by_name(
|
||||
cls, name: str, user_id: Optional[int] = None
|
||||
) -> Optional["Playlist"]:
|
||||
"""Find playlist by name, optionally filtered by user."""
|
||||
query = cls.query.filter_by(name=name)
|
||||
if user_id is not None:
|
||||
query = query.filter_by(user_id=user_id)
|
||||
return query.first()
|
||||
|
||||
@classmethod
|
||||
def find_by_user(cls, user_id: int) -> list["Playlist"]:
|
||||
"""Find all playlists for a user."""
|
||||
return cls.query.filter_by(user_id=user_id).order_by(cls.name).all()
|
||||
|
||||
@classmethod
|
||||
def find_system_playlists(cls) -> list["Playlist"]:
|
||||
"""Find all system playlists (user_id is None)."""
|
||||
return cls.query.filter_by(user_id=None).order_by(cls.name).all()
|
||||
|
||||
@classmethod
|
||||
def find_current_playlist(
|
||||
cls, user_id: Optional[int] = None
|
||||
@@ -163,22 +130,6 @@ class Playlist(db.Model):
|
||||
query = query.filter_by(user_id=user_id)
|
||||
return query.first()
|
||||
|
||||
def set_as_current(self, commit: bool = True) -> None:
|
||||
"""Set this playlist as the current one and unset others."""
|
||||
# Unset other current playlists for the same user/system
|
||||
if self.user_id is not None:
|
||||
Playlist.query.filter_by(
|
||||
user_id=self.user_id, is_current=True
|
||||
).update({"is_current": False})
|
||||
else:
|
||||
Playlist.query.filter_by(user_id=None, is_current=True).update(
|
||||
{"is_current": False}
|
||||
)
|
||||
|
||||
self.is_current = True
|
||||
if commit:
|
||||
db.session.commit()
|
||||
|
||||
def add_sound(
|
||||
self, sound_id: int, order: Optional[int] = None, commit: bool = True
|
||||
) -> "PlaylistSound":
|
||||
@@ -203,87 +154,3 @@ class Playlist(db.Model):
|
||||
db.session.commit()
|
||||
|
||||
return playlist_sound
|
||||
|
||||
def remove_sound(self, sound_id: int, commit: bool = True) -> bool:
|
||||
"""Remove a sound from the playlist."""
|
||||
from app.models.playlist_sound import PlaylistSound
|
||||
|
||||
playlist_sound = PlaylistSound.query.filter_by(
|
||||
playlist_id=self.id, sound_id=sound_id
|
||||
).first()
|
||||
|
||||
if playlist_sound:
|
||||
db.session.delete(playlist_sound)
|
||||
if commit:
|
||||
db.session.commit()
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def reorder_sounds(
|
||||
self, sound_orders: list[dict], commit: bool = True
|
||||
) -> None:
|
||||
"""Reorder sounds in the playlist.
|
||||
|
||||
Args:
|
||||
sound_orders: List of dicts with 'sound_id' and 'order' keys
|
||||
"""
|
||||
from app.models.playlist_sound import PlaylistSound
|
||||
|
||||
for item in sound_orders:
|
||||
playlist_sound = PlaylistSound.query.filter_by(
|
||||
playlist_id=self.id, sound_id=item["sound_id"]
|
||||
).first()
|
||||
if playlist_sound:
|
||||
playlist_sound.order = item["order"]
|
||||
|
||||
if commit:
|
||||
db.session.commit()
|
||||
|
||||
def get_total_duration(self) -> int:
|
||||
"""Get total duration of all sounds in the playlist in milliseconds."""
|
||||
from app.models.sound import Sound
|
||||
|
||||
total = (
|
||||
db.session.query(db.func.sum(Sound.duration))
|
||||
.join(self.playlist_sounds)
|
||||
.filter(Sound.id.in_([ps.sound_id for ps in self.playlist_sounds]))
|
||||
.scalar()
|
||||
)
|
||||
|
||||
return total or 0
|
||||
|
||||
def duplicate(
|
||||
self, new_name: str, user_id: Optional[int] = None, commit: bool = True
|
||||
) -> "Playlist":
|
||||
"""Create a duplicate of this playlist."""
|
||||
new_playlist = Playlist.create_playlist(
|
||||
name=new_name,
|
||||
description=self.description,
|
||||
genre=self.genre,
|
||||
user_id=user_id,
|
||||
is_main=False,
|
||||
is_deletable=True,
|
||||
is_current=False,
|
||||
commit=commit,
|
||||
)
|
||||
|
||||
# Copy all sounds with their order
|
||||
for ps in self.playlist_sounds:
|
||||
new_playlist.add_sound(ps.sound_id, ps.order, commit=False)
|
||||
|
||||
if commit:
|
||||
db.session.commit()
|
||||
|
||||
return new_playlist
|
||||
|
||||
def save(self, commit: bool = True) -> None:
|
||||
"""Save changes to the playlist."""
|
||||
if commit:
|
||||
db.session.commit()
|
||||
|
||||
def delete(self, commit: bool = True) -> None:
|
||||
"""Delete the playlist."""
|
||||
db.session.delete(self)
|
||||
if commit:
|
||||
db.session.commit()
|
||||
|
||||
Reference in New Issue
Block a user