diff --git a/app/models.py b/app/models.py index 777301f..c4fbc2b 100644 --- a/app/models.py +++ b/app/models.py @@ -453,24 +453,22 @@ class PlaylistRows(Base): plr.note) @staticmethod - def delete_higher_rows(session: Session, playlist_id: int, maxrow: int) \ - -> None: + def delete_plrids_not_in_list(session: Session, playlist_id: int, + plrids: List["PlaylistRows"]) -> None: """ Delete rows in given playlist that have a higher row number than 'maxrow' """ - # Log the rows to be deleted - rows_to_go = session.execute( - select(PlaylistRows) - .where(PlaylistRows.playlist_id == playlist_id, - PlaylistRows.row_number > maxrow) - ).scalars().all() - if not rows_to_go: - return - - for row in rows_to_go: - session.delete(row) + session.execute( + delete(PlaylistRows) + .where( + PlaylistRows.playlist_id == playlist_id, + PlaylistRows.id.not_in(plrids) + ) + ) + # Delete won't take effect until commit() + session.commit() @staticmethod def delete_rows(session: Session, ids: List[int]) -> None: diff --git a/app/musicmuster.py b/app/musicmuster.py index a46fca0..f062a92 100755 --- a/app/musicmuster.py +++ b/app/musicmuster.py @@ -2,7 +2,7 @@ from log import log import argparse -import stackprinter +import stackprinter # type: ignore import subprocess import sys import threading diff --git a/app/playlists.py b/app/playlists.py index 58ae757..e48efba 100644 --- a/app/playlists.py +++ b/app/playlists.py @@ -327,8 +327,8 @@ class PlaylistTab(QTableWidget): act_setnext.triggered.connect( lambda: self._set_next(session, row_number)) - # Open in Audacity if not current: + # Open in Audacity act_audacity = self.menu.addAction( "Open in Audacity") act_audacity.triggered.connect( @@ -778,7 +778,7 @@ class PlaylistTab(QTableWidget): self.current_track_start_time = None def populate_display(self, session: Session, playlist_id: int, - scroll_to_top: bool = True) -> None: + scroll_to_top: bool = True) -> None: """ Populate display from the associated playlist ID """ @@ -854,13 +854,13 @@ class PlaylistTab(QTableWidget): row_plr[row].row_number = row row_plr[row].playlist_id = self.playlist_id - # Any rows in the database with a row_number higher that the - # current value of 'row' should not be there. Commit session - # first to ensure any changes made above are committed. - session.commit() - PlaylistRows.delete_higher_rows(session, self.playlist_id, - self.rowCount() - 1) + # Any rows in the database for this playlist that have a plr id + # that's not in the displayed playlist need to be deleted. + + # Ensure changes flushed session.commit() + PlaylistRows.delete_plrids_not_in_list(session, self.playlist_id, + display_plr_ids.values()) def scroll_current_to_top(self) -> None: """Scroll currently-playing row to top""" @@ -1325,7 +1325,7 @@ class PlaylistTab(QTableWidget): self.remove_selected_rows() with Session() as session: - self.save_playlist(session) + QTimer.singleShot(0, lambda: self.save_playlist(session)) def _drop_on(self, event): """