From 32e81fb07460e832986bf1c4fbeade9ed9897541 Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Sat, 6 Aug 2022 21:17:11 +0100 Subject: [PATCH] Save of new style playlist implemented but not tested --- app/models.py | 39 +++++++++++++++++++++++++++++++++++++-- app/playlists.py | 24 +++++++++++++++++++++++- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/app/models.py b/app/models.py index 4f6065c..8068ec6 100644 --- a/app/models.py +++ b/app/models.py @@ -43,7 +43,7 @@ from sqlalchemy.orm.exc import ( # leading_silence, # trailing_silence, # ) -# from log import log.debug, log.error +from log import log # Base = declarative_base() @@ -442,7 +442,6 @@ class PlaylistRows(Base): f"note={self.note} row_number={self.row_number}>" ) - # def __init__( # self, session: Session, playlist_id: int, track_id: int, # row: int) -> None: @@ -453,6 +452,42 @@ class PlaylistRows(Base): # self.row = row # session.add(self) # session.flush() +# + @staticmethod + def delete_higher_rows(session: Session, playlist_id: int, row: int) \ + -> None: + """ + Delete rows in given playlist that have a higher row number + than 'row' + """ + + # Log the rows to be deleted + rows_to_go = session.execute( + select(PlaylistRows) + .where(PlaylistRows.playlist_id == playlist_id, + PlaylistRows.row_number > row) + ).scalars().all() + if not rows_to_go: + return + + for row in rows_to_go: + log.debu(f"Should delete: {row}") + # If needed later: + # session.delete(row) + + +# @classmethod +# def get_playlist_rows(cls, playlist_id: int) -> \ +# Optional[List["PlaylistRows"]]: +# """ +# Return a list of PlaylistRows for passed playlist ordered by row +# """ +# +# return session.execute( +# select(cls) +# .where(cls.playlist_id == playlist_id) +# .order_by(cls.row_number) +# ).scalars().all() # # @staticmethod # def max_used_row(session: Session, playlist_id: int) -> Optional[int]: diff --git a/app/playlists.py b/app/playlists.py index 7b15c9c..ae33cf0 100644 --- a/app/playlists.py +++ b/app/playlists.py @@ -648,6 +648,28 @@ class PlaylistTab(QTableWidget): # KAE self.save_playlist(session) self.update_display(session) + def save_playlist(self, session: Session) -> None: + """ + Save playlist to database + """ + + # Iteratate through playlist and check that the row in each + # playlist_row object is correct + for row in range(self.rowCount()): + plr = session.get(PlaylistRows, self._get_playlistrow_id(row)) + # Set the row number (even if it's already correct) + if plr.row_number != row: + log.debug( + f"Updating PlaylistRow: {plr.row_number=}, {row=}" + ) + plr.row_number = row + + # 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, row) + # def save_playlist(self, session) -> None: # """ # Save playlist to database. @@ -1009,7 +1031,7 @@ class PlaylistTab(QTableWidget): self._set_row_colour( row, QColor(note_colour) ) - # Notes are always bold + # Section headers are always bold self._set_row_bold(row) continue