diff --git a/app/models.py b/app/models.py index fd86339..50c54ef 100644 --- a/app/models.py +++ b/app/models.py @@ -431,23 +431,6 @@ class PlaylistRows(Base): track_id=plr.track_id, ) - @staticmethod - def delete_higher_rows( - session: scoped_session, playlist_id: int, maxrow: int - ) -> None: - """ - Delete rows in given playlist that have a higher row number - than 'maxrow' - """ - - session.execute( - delete(PlaylistRows).where( - PlaylistRows.playlist_id == playlist_id, - PlaylistRows.plr_rownum > maxrow, - ) - ) - session.flush() - @classmethod def deep_row( cls, session: scoped_session, playlist_id: int, row_number: int @@ -488,6 +471,36 @@ class PlaylistRows(Base): return session.scalars(stmt).unique().all() + @staticmethod + def delete_higher_rows( + session: scoped_session, playlist_id: int, maxrow: int + ) -> None: + """ + Delete rows in given playlist that have a higher row number + than 'maxrow' + """ + + session.execute( + delete(PlaylistRows).where( + PlaylistRows.playlist_id == playlist_id, + PlaylistRows.plr_rownum > maxrow, + ) + ) + session.flush() + + @staticmethod + def delete_rows(session: scoped_session, playlist_id: int, row_numbers: List[int]) -> None: + """ + Delete passed rows in given playlist. + """ + + session.execute( + delete(PlaylistRows).where( + PlaylistRows.playlist_id == playlist_id, + PlaylistRows.plr_rownum.in_(row_numbers) + ) + ) + @staticmethod def fixup_rownumbers(session: scoped_session, playlist_id: int) -> None: """ diff --git a/app/playlistmodel.py b/app/playlistmodel.py index f3705b9..142addf 100644 --- a/app/playlistmodel.py +++ b/app/playlistmodel.py @@ -347,8 +347,11 @@ class PlaylistModel(QAbstractTableModel): Delete passed rows from model """ - # TODO - print(f"Delete rows {row_numbers=}") + with Session() as session: + PlaylistRows.delete_rows(session, self.playlist_id, row_numbers) + PlaylistRows.fixup_rownumbers(session, self.playlist_id) + self.refresh_data(session) + self.update_track_times() def display_role(self, row: int, column: int, prd: PlaylistRowData) -> QVariant: """ diff --git a/app/playlists.py b/app/playlists.py index 3921887..b19e0cd 100644 --- a/app/playlists.py +++ b/app/playlists.py @@ -1085,9 +1085,7 @@ class PlaylistTab(QTableView): # Delete row if not current_row and not next_row: - self._add_context_menu( - "Delete row", lambda: model.delete_rows(self._get_selected_rows()) - ) + self._add_context_menu("Delete row", lambda: self._delete_rows()) # Remove track from row if track_row and not current_row and not next_row: @@ -1211,77 +1209,22 @@ class PlaylistTab(QTableView): Delete mutliple rows Actions required: - - Remove the rows from the display - - Save the playlist - - Update track start/end times + - Confirm deletion should go ahead + - Pass to model to do the deed """ - rows_to_delete: List[int] = [] - with Session() as session: - plrs = self.get_selected_playlistrows(session) - row_count = len(plrs) - if not row_count: - return + rows_to_delete = self._get_selected_rows() + row_count = len(rows_to_delete) + if row_count < 1: + return - # Get confirmation - plural = "s" if row_count > 1 else "" - if not ask_yes_no("Delete rows", f"Really delete {row_count} row{plural}?"): - return + # Get confirmation + plural = "s" if row_count > 1 else "" + if not ask_yes_no("Delete rows", f"Really delete {row_count} row{plural}?"): + return - rows_to_delete = [plr.plr_rownum for plr in plrs] - - # Delete rows from database. Would be more efficient to - # query then have a single delete. - for plr in plrs: - session.delete(plr) - - # Remove from display - self.remove_rows(rows_to_delete) - - # Need to save the playlist to ensure the PlaylistRows have - # the correct row_number - self.save_playlist(session) - - # Reset drag mode - # self.setDragEnabled(False) - - self._update_start_end_times(session) - - # def _find_next_track_row( - # self, session: scoped_session, starting_row: Optional[int] = None - # ) -> Optional[int]: - # """ - # Find next track to play. If a starting row is given, start there; - # otherwise, start from top. Skip rows already played. - - # If not found, return None. - - # If found, return row number. - # """ - - # if starting_row is None: - # starting_row = 0 - - # track_rows = [ - # p.plr_rownum - # for p in PlaylistRows.get_rows_with_tracks(session, self.playlist_id) - # ] - # played_rows = [ - # p.plr_rownum - # for p in PlaylistRows.get_played_rows(session, self.playlist_id) - # ] - # for row_number in range(starting_row, self.rowCount()): - # if row_number not in track_rows or row_number in played_rows: - # continue - # plr = selWIP V3: play track workingf._get_row_plr(session, row_number) - # if not plr: - # continue - # if file_is_unreadable(plr.track.path): - # continue - # else: - # return row_number - - # return None + model = cast(PlaylistModel, self.model()) + model.delete_rows(self._get_selected_rows()) def _get_current_track_row_number(self) -> Optional[int]: """Return current track row or None"""