Fix deleting rows from playlist

This commit is contained in:
Keith Edmunds 2022-12-23 21:27:06 +00:00
parent 46f2b662f3
commit f2a27366d3
3 changed files with 21 additions and 23 deletions

View File

@ -453,24 +453,22 @@ class PlaylistRows(Base):
plr.note) plr.note)
@staticmethod @staticmethod
def delete_higher_rows(session: Session, playlist_id: int, maxrow: int) \ def delete_plrids_not_in_list(session: Session, playlist_id: int,
-> None: plrids: List["PlaylistRows"]) -> None:
""" """
Delete rows in given playlist that have a higher row number Delete rows in given playlist that have a higher row number
than 'maxrow' than 'maxrow'
""" """
# Log the rows to be deleted session.execute(
rows_to_go = session.execute( delete(PlaylistRows)
select(PlaylistRows) .where(
.where(PlaylistRows.playlist_id == playlist_id, PlaylistRows.playlist_id == playlist_id,
PlaylistRows.row_number > maxrow) PlaylistRows.id.not_in(plrids)
).scalars().all() )
if not rows_to_go: )
return # Delete won't take effect until commit()
session.commit()
for row in rows_to_go:
session.delete(row)
@staticmethod @staticmethod
def delete_rows(session: Session, ids: List[int]) -> None: def delete_rows(session: Session, ids: List[int]) -> None:

View File

@ -2,7 +2,7 @@
from log import log from log import log
import argparse import argparse
import stackprinter import stackprinter # type: ignore
import subprocess import subprocess
import sys import sys
import threading import threading

View File

@ -327,8 +327,8 @@ class PlaylistTab(QTableWidget):
act_setnext.triggered.connect( act_setnext.triggered.connect(
lambda: self._set_next(session, row_number)) lambda: self._set_next(session, row_number))
# Open in Audacity
if not current: if not current:
# Open in Audacity
act_audacity = self.menu.addAction( act_audacity = self.menu.addAction(
"Open in Audacity") "Open in Audacity")
act_audacity.triggered.connect( act_audacity.triggered.connect(
@ -778,7 +778,7 @@ class PlaylistTab(QTableWidget):
self.current_track_start_time = None self.current_track_start_time = None
def populate_display(self, session: Session, playlist_id: int, 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 Populate display from the associated playlist ID
""" """
@ -854,13 +854,13 @@ class PlaylistTab(QTableWidget):
row_plr[row].row_number = row row_plr[row].row_number = row
row_plr[row].playlist_id = self.playlist_id row_plr[row].playlist_id = self.playlist_id
# Any rows in the database with a row_number higher that the # Any rows in the database for this playlist that have a plr id
# current value of 'row' should not be there. Commit session # that's not in the displayed playlist need to be deleted.
# first to ensure any changes made above are committed.
session.commit() # Ensure changes flushed
PlaylistRows.delete_higher_rows(session, self.playlist_id,
self.rowCount() - 1)
session.commit() session.commit()
PlaylistRows.delete_plrids_not_in_list(session, self.playlist_id,
display_plr_ids.values())
def scroll_current_to_top(self) -> None: def scroll_current_to_top(self) -> None:
"""Scroll currently-playing row to top""" """Scroll currently-playing row to top"""
@ -1325,7 +1325,7 @@ class PlaylistTab(QTableWidget):
self.remove_selected_rows() self.remove_selected_rows()
with Session() as session: with Session() as session:
self.save_playlist(session) QTimer.singleShot(0, lambda: self.save_playlist(session))
def _drop_on(self, event): def _drop_on(self, event):
""" """