From 35b101a538f6091f96833383b999729cd8ba677c Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Fri, 23 Dec 2022 17:23:43 +0000 Subject: [PATCH] Tidy up saving database --- app/models.py | 57 ++++++++++++++++++++++++++++++++-------------- app/musicmuster.py | 14 +++++++----- app/playlists.py | 22 ++++++++---------- 3 files changed, 57 insertions(+), 36 deletions(-) diff --git a/app/models.py b/app/models.py index 9a961bb..909cf7d 100644 --- a/app/models.py +++ b/app/models.py @@ -2,6 +2,7 @@ # import os.path import re +import stackprinter # from dbconfig import Session # @@ -417,7 +418,7 @@ class PlaylistRows(Base): return ( f"" + f"note={self.note}, row_number={self.row_number}>" ) def __init__(self, @@ -452,7 +453,7 @@ class PlaylistRows(Base): plr.note) @staticmethod - def delete_higher_rows(session: Session, playlist_id: int, row: int) \ + def delete_higher_rows(session: Session, playlist_id: int, maxrow: int) \ -> None: """ Delete rows in given playlist that have a higher row number @@ -463,7 +464,7 @@ class PlaylistRows(Base): rows_to_go = session.execute( select(PlaylistRows) .where(PlaylistRows.playlist_id == playlist_id, - PlaylistRows.row_number > row) + PlaylistRows.row_number > maxrow) ).scalars().all() if not rows_to_go: return @@ -472,11 +473,6 @@ class PlaylistRows(Base): log.debug(f"Should delete: {row}") # If needed later: # session.delete(row) - rows_to_go = session.execute( - select(PlaylistRows) - .where(PlaylistRows.playlist_id == playlist_id, - PlaylistRows.row_number > row) - ).scalars().all() @staticmethod def delete_rows(session: Session, ids: List[int]) -> None: @@ -509,6 +505,42 @@ class PlaylistRows(Base): # Ensure new row numbers are available to the caller session.commit() + @classmethod + def get_plr(session: Session, row_number: int, + playlist_id: int) -> "PlaylistRows": + """Return playlistrows object matching passed parameters""" + + return ( + select(PlaylistRows) + .where( + PlaylistRows.row_number == row_number, + PlaylistRows.playlist_id == playlist_id) + .limit(1) + ).first() + + @staticmethod + def get_track_plr(session: Session, track_id: int, + playlist_id: int) -> Optional["PlaylistRows"]: + """Return first matching PlaylistRows object or None""" + + return session.scalars( + select(PlaylistRows) + .where( + PlaylistRows.track_id == track_id, + PlaylistRows.playlist_id == playlist_id + ) + .limit(1) + ).first() + + @staticmethod + def get_last_used_row(session: Session, playlist_id: int) -> Optional[int]: + """Return the last used row for playlist, or None if no rows""" + + return session.execute( + select(func.max(PlaylistRows.row_number)) + .where(PlaylistRows.playlist_id == playlist_id) + ).scalar_one() + @classmethod def get_played_rows(cls, session: Session, playlist_id: int) -> List[int]: @@ -547,15 +579,6 @@ class PlaylistRows(Base): return plrs - @staticmethod - def get_last_used_row(session: Session, playlist_id: int) -> Optional[int]: - """Return the last used row for playlist, or None if no rows""" - - return session.execute( - select(func.max(PlaylistRows.row_number)) - .where(PlaylistRows.playlist_id == playlist_id) - ).scalar_one() - @classmethod def get_unplayed_rows(cls, session: Session, playlist_id: int) -> List[int]: diff --git a/app/musicmuster.py b/app/musicmuster.py index d130d57..8bef8e9 100755 --- a/app/musicmuster.py +++ b/app/musicmuster.py @@ -739,6 +739,7 @@ class Window(QMainWindow, Ui_MainWindow): helpers.set_track_metadata(session, track) helpers.normalise_track(track.path) self.visible_playlist_tab().insert_track(session, track) + self.visible_playlist_tab().save_playlist(session) def insert_header(self) -> None: """Show dialog box to enter header text and add to playlist""" @@ -758,6 +759,7 @@ class Window(QMainWindow, Ui_MainWindow): if ok: with Session() as session: playlist_tab.insert_header(session, dlg.textValue()) + self.save_playlist(session) def insert_track(self) -> None: """Show dialog box to select and add track from database""" @@ -828,13 +830,13 @@ class Window(QMainWindow, Ui_MainWindow): # Update destination playlist_tab if visible (if not visible, it # will be re-populated when it is opened) - destionation_playlist_tab = None + destination_playlist_tab = None for tab in range(self.tabPlaylist.count()): if self.tabPlaylist.widget(tab).playlist_id == dlg.playlist.id: - destionation_playlist_tab = self.tabPlaylist.widget(tab) + destination_playlist_tab = self.tabPlaylist.widget(tab) break - if destionation_playlist_tab: - destionation_playlist_tab.populate(session, dlg.playlist.id) + if destination_playlist_tab: + destination_playlist_tab.populate(session, dlg.playlist.id) def move_selected(self) -> None: """ @@ -1500,8 +1502,8 @@ class DbDialog(QDialog): self.parent().visible_playlist_tab().insert_track( self.session, track, note=self.ui.txtNote.text()) - # Commit session to get correct row numbers if more tracks added - self.session.commit() + # Save to database (which will also commit changes) + self.parent().visible_playlist_tab().save_playlist(self.session) # Clear note field and select search text to make it easier for # next search self.ui.txtNote.clear() diff --git a/app/playlists.py b/app/playlists.py index dca8dd1..d112e78 100644 --- a/app/playlists.py +++ b/app/playlists.py @@ -1,5 +1,5 @@ import re -import stackprinter +import stackprinter # type: ignore import subprocess import threading @@ -199,7 +199,7 @@ class PlaylistTab(QTableWidget): # self.setSortingEnabled(True) # Now load our tracks and notes - self.populate(session, self.playlist_id) + self.populate_display(session, self.playlist_id) def __repr__(self) -> str: return f"" @@ -623,7 +623,7 @@ class PlaylistTab(QTableWidget): # Add track details to items try: start_gap = row_data.track.start_gap - except: + except AttributeError: return start_gap_item = QTableWidgetItem(str(start_gap)) if start_gap and start_gap >= 500: @@ -771,10 +771,10 @@ class PlaylistTab(QTableWidget): self._clear_current_track_row() self.current_track_start_time = None - def populate(self, session: Session, playlist_id: int, + def populate_display(self, session: Session, playlist_id: int, scroll_to_top: bool = True) -> None: """ - Populate from the associated playlist ID + Populate display from the associated playlist ID """ # Sanity check row numbering before we load @@ -1344,9 +1344,8 @@ class PlaylistTab(QTableWidget): Delete mutliple rows Actions required: - - Delete the rows from the PlaylistRows table - - Correct the row numbers in the PlaylistRows table - Remove the rows from the display + - Save the playlist """ # Delete rows from database @@ -1359,13 +1358,10 @@ class PlaylistTab(QTableWidget): f"Really delete {row_count} row{plural}?"): return - with Session() as session: - PlaylistRows.delete_rows(session, plr_ids) + self.remove_selected_rows() - # Fix up row numbers left in this playlist - PlaylistRows.fixup_rownumbers(session, self.playlist_id) - # Remove selected rows from display - self.remove_selected_rows() + with Session() as session: + self.save_playlist(session) def _drop_on(self, event): """