Fix moving tracks between playlists

This commit is contained in:
Keith Edmunds 2022-04-04 21:30:31 +01:00
parent 0a3700e208
commit c5f33c437f
2 changed files with 78 additions and 49 deletions

View File

@ -17,9 +17,10 @@ from sqlalchemy import (
DateTime, DateTime,
Float, Float,
ForeignKey, ForeignKey,
func,
Integer, Integer,
String, String,
func UniqueConstraint,
) )
from sqlalchemy.exc import IntegrityError from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import ( from sqlalchemy.orm import (
@ -39,7 +40,6 @@ from helpers import (
) )
from log import DEBUG, ERROR from log import DEBUG, ERROR
Base: DeclarativeMeta = declarative_base() Base: DeclarativeMeta = declarative_base()
@ -97,9 +97,9 @@ class NoteColours(Base):
for rec in ( for rec in (
session.query(NoteColours) session.query(NoteColours)
.filter(NoteColours.enabled.is_(True)) .filter(NoteColours.enabled.is_(True))
.order_by(NoteColours.order) .order_by(NoteColours.order)
.all() .all()
): ):
if rec.is_regex: if rec.is_regex:
flags = re.UNICODE flags = re.UNICODE
@ -206,8 +206,8 @@ class Playdates(Base):
last_played: Optional[Playdates] = session.query( last_played: Optional[Playdates] = session.query(
Playdates.lastplayed).filter( Playdates.lastplayed).filter(
(Playdates.track_id == track_id) (Playdates.track_id == track_id)
).order_by(Playdates.lastplayed.desc()).first() ).order_by(Playdates.lastplayed.desc()).first()
if last_played: if last_played:
return last_played[0] return last_played[0]
else: else:
@ -265,7 +265,7 @@ class Playlists(Base):
""" """
if not row: if not row:
row = PlaylistTracks.next_free_row(session, self) row = PlaylistTracks.next_free_row(session, self.id)
PlaylistTracks(session, self.id, track_id, row) PlaylistTracks(session, self.id, track_id, row)
@ -294,8 +294,8 @@ class Playlists(Base):
return ( return (
session.query(cls) session.query(cls)
.filter(cls.loaded.is_(False)) .filter(cls.loaded.is_(False))
.order_by(cls.last_used.desc()) .order_by(cls.last_used.desc())
).all() ).all()
@classmethod @classmethod
@ -306,8 +306,8 @@ class Playlists(Base):
return ( return (
session.query(cls) session.query(cls)
.filter(cls.loaded.is_(True)) .filter(cls.loaded.is_(True))
.order_by(cls.last_used.desc()) .order_by(cls.last_used.desc())
).all() ).all()
def mark_open(self, session: Session) -> None: def mark_open(self, session: Session) -> None:
@ -317,8 +317,9 @@ class Playlists(Base):
self.last_used = datetime.now() self.last_used = datetime.now()
session.flush() session.flush()
def move_track(self, session: Session, rows: List[int], def move_track(
to_playlist: "Playlists") -> None: self, session: Session, rows: List[int],
to_playlist: "Playlists") -> None:
"""Move tracks to another playlist""" """Move tracks to another playlist"""
for row in rows: for row in rows:
@ -354,7 +355,7 @@ class PlaylistTracks(Base):
id: int = Column(Integer, primary_key=True, autoincrement=True) id: int = Column(Integer, primary_key=True, autoincrement=True)
playlist_id: int = Column(Integer, ForeignKey('playlists.id'), playlist_id: int = Column(Integer, ForeignKey('playlists.id'),
primary_key=True) primary_key=True)
track_id: int = Column(Integer, ForeignKey('tracks.id'), primary_key=True) track_id: int = Column(Integer, ForeignKey('tracks.id'), primary_key=True)
row: int = Column(Integer, nullable=False) row: int = Column(Integer, nullable=False)
tracks: RelationshipProperty = relationship("Tracks") tracks: RelationshipProperty = relationship("Tracks")
@ -367,6 +368,10 @@ class PlaylistTracks(Base):
cascade="all, delete-orphan" cascade="all, delete-orphan"
) )
) )
# Ensure row numbers are unique within each playlist
__table_args__ = (UniqueConstraint
('row', 'playlist_id', name="uniquerow"),
)
def __init__( def __init__(
self, session: Session, playlist_id: int, track_id: int, self, session: Session, playlist_id: int, track_id: int,
@ -380,14 +385,14 @@ class PlaylistTracks(Base):
session.flush() session.flush()
@staticmethod @staticmethod
def next_free_row(session: Session, playlist: Playlists) -> int: def next_free_row(session: Session, playlist_id: int) -> int:
"""Return next free row number""" """Return next free row number"""
row: int row: int
last_row = session.query( last_row = session.query(
func.max(PlaylistTracks.row) func.max(PlaylistTracks.row)
).filter_by(playlist_id=playlist.id).first() ).filter_by(playlist_id=playlist_id).first()
# if there are no rows, the above returns (None, ) which is True # if there are no rows, the above returns (None, ) which is True
if last_row and last_row[0] is not None: if last_row and last_row[0] is not None:
row = last_row[0] + 1 row = last_row[0] + 1
@ -396,6 +401,33 @@ class PlaylistTracks(Base):
return row return row
@staticmethod
def move_rows(
session: Session, rows: List[int], from_playlist_id: int,
to_playlist_id: int) -> None:
"""Move rows between playlists"""
# A constraint deliberately blocks duplicate (playlist_id, row)
# entries in database; however, unallocated rows in the database
# are fine (ie, we can have rows 1, 4, 6 and no 2, 3, 5).
# Unallocated rows will be automatically removed when the
# playlist is saved.
lowest_source_row: int = min(rows)
first_destination_free_row = PlaylistTracks.next_free_row(
session, to_playlist_id)
# Calculate offset that will put the lowest row number being
# moved at the first free row in destination playlist
offset = first_destination_free_row - lowest_source_row
session.query(PlaylistTracks).filter(
PlaylistTracks.playlist_id == from_playlist_id,
PlaylistTracks.row.in_(rows)
).update({'playlist_id': to_playlist_id,
'row': PlaylistTracks.row + offset},
False
)
class Settings(Base): class Settings(Base):
__tablename__ = 'settings' __tablename__ = 'settings'
@ -444,24 +476,25 @@ class Tracks(Base):
mtime: float = Column(Float, index=True) mtime: float = Column(Float, index=True)
lastplayed: datetime = Column(DateTime, index=True, default=None) lastplayed: datetime = Column(DateTime, index=True, default=None)
playlists: RelationshipProperty = relationship("PlaylistTracks", playlists: RelationshipProperty = relationship("PlaylistTracks",
back_populates="tracks", back_populates="tracks",
lazy="joined") lazy="joined")
playdates: RelationshipProperty = relationship("Playdates", playdates: RelationshipProperty = relationship("Playdates",
back_populates="tracks", back_populates="tracks",
lazy="joined") lazy="joined")
def __init__(self, def __init__(
session: Session, self,
path: str, session: Session,
title: Optional[str] = None, path: str,
artist: Optional[str] = None, title: Optional[str] = None,
duration: Optional[int] = None, artist: Optional[str] = None,
start_gap: Optional[int] = None, duration: Optional[int] = None,
fade_at: Optional[int] = None, start_gap: Optional[int] = None,
silence_at: Optional[int] = None, fade_at: Optional[int] = None,
mtime: Optional[float] = None, silence_at: Optional[int] = None,
lastplayed: Optional[datetime] = None, mtime: Optional[float] = None,
) -> None: lastplayed: Optional[datetime] = None,
) -> None:
self.path = path self.path = path
self.title = title self.title = title
self.artist = artist self.artist = artist
@ -556,10 +589,10 @@ class Tracks(Base):
audio: AudioSegment = get_audio_segment(self.path) audio: AudioSegment = get_audio_segment(self.path)
self.duration = len(audio) self.duration = len(audio)
self.fade_at = round(fade_point(audio) / 1000, self.fade_at = round(fade_point(audio) / 1000,
Config.MILLISECOND_SIGFIGS) * 1000 Config.MILLISECOND_SIGFIGS) * 1000
self.mtime = os.path.getmtime(self.path) self.mtime = os.path.getmtime(self.path)
self.silence_at = round(trailing_silence(audio) / 1000, self.silence_at = round(trailing_silence(audio) / 1000,
Config.MILLISECOND_SIGFIGS) * 1000 Config.MILLISECOND_SIGFIGS) * 1000
self.start_gap = leading_silence(audio) self.start_gap = leading_silence(audio)
session.add(self) session.add(self)
session.flush() session.flush()
@ -581,16 +614,16 @@ class Tracks(Base):
return ( return (
session.query(cls) session.query(cls)
.filter(cls.artist.ilike(f"%{text}%")) .filter(cls.artist.ilike(f"%{text}%"))
.order_by(cls.title) .order_by(cls.title)
).all() ).all()
@classmethod @classmethod
def search_titles(cls, session: Session, text: str) -> List["Tracks"]: def search_titles(cls, session: Session, text: str) -> List["Tracks"]:
return ( return (
session.query(cls) session.query(cls)
.filter(cls.title.ilike(f"%{text}%")) .filter(cls.title.ilike(f"%{text}%"))
.order_by(cls.title) .order_by(cls.title)
).all() ).all()
@staticmethod @staticmethod

View File

@ -424,7 +424,8 @@ class Window(QMainWindow, Ui_MainWindow):
# Update database for both source and destination playlists # Update database for both source and destination playlists
rows = visible_tab.get_selected_rows() rows = visible_tab.get_selected_rows()
source_playlist.move_track(session, rows, destination_playlist) PlaylistTracks.move_rows(session, rows, source_playlist.id,
destination_playlist.id)
# Update destination playlist_tab if visible (if not visible, it # Update destination playlist_tab if visible (if not visible, it
# will be re-populated when it is opened) # will be re-populated when it is opened)
@ -594,10 +595,10 @@ class Window(QMainWindow, Ui_MainWindow):
dlg = SelectPlaylistDialog(self, playlists=playlists, dlg = SelectPlaylistDialog(self, playlists=playlists,
session=session) session=session)
dlg.exec() dlg.exec()
if dlg.plid: playlist = dlg.playlist
p = Playlists.get_by_id(session=session, playlist_id=dlg.plid) if playlist:
p.mark_open(session) playlist.mark_open(session)
self.create_playlist_tab(session, p) self.create_playlist_tab(session, playlist)
def select_next_row(self) -> None: def select_next_row(self) -> None:
"""Select next or first row in playlist""" """Select next or first row in playlist"""
@ -971,11 +972,6 @@ class SelectPlaylistDialog(QDialog):
height = record.f_int or 600 height = record.f_int or 600
self.resize(width, height) self.resize(width, height)
# for (plid, plname) in [(a.id, a.name) for a in playlists]:
# p = QListWidgetItem()
# p.setText(plname)
# p.setData(Qt.UserRole, plid)
# self.ui.lstPlaylists.addItem(p)
for playlist in playlists: for playlist in playlists:
p = QListWidgetItem() p = QListWidgetItem()
p.setText(playlist.name) p.setText(playlist.name)