From a4bdbfccd0a169367058c16ad0be2cb8f80d0ed1 Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Fri, 4 Jun 2021 15:44:29 +0100 Subject: [PATCH] Explicitly save playlist id in playlist --- app/model.py | 18 ++++++++++++++++++ app/playlists.py | 25 ++++++++++++++++--------- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/app/model.py b/app/model.py index 36836ee..9aece7e 100644 --- a/app/model.py +++ b/app/model.py @@ -286,6 +286,24 @@ class PlaylistTracks(Base): record.row = new_row session.commit() + @staticmethod + def new_row(session, playlist_id): + "Return row number > largest existing row number" + + last_row = session.query(func.max(PlaylistTracks.row)).one()[0] + return last_row + 1 + + @staticmethod + def remove_all_tracks(session, playlist_id): + """ + Remove all tracks from passed playlist_id + """ + + session.query(PlaylistTracks).filter( + PlaylistTracks.playlist_id == playlist_id, + ).delete() + session.commit() + @staticmethod def remove_track(session, playlist_id, row): DEBUG( diff --git a/app/playlists.py b/app/playlists.py index 630033c..7168e7c 100644 --- a/app/playlists.py +++ b/app/playlists.py @@ -245,7 +245,7 @@ class Playlist(QTableWidget): # We need to add ourself to the session us_in_db = session.query(Playlists).filter( - Playlists.id == self.db.id).one() + Playlists.id == self.id).one() us_in_db.add_track(session, track, row) @@ -361,9 +361,14 @@ class Playlist(QTableWidget): self._repaint(save_playlist=False) def populate(self, session): - # add them in row order. We don't mandate that an item will be + # add tracks and notes in row order. + # We don't mandate that an item will be # on its specified row, only that it will be above # larger-numbered row items, and below lower-numbered ones. + + # First, save our id for the future + self.id = self.db.id + data = [] for t in self.db.tracks: @@ -816,18 +821,20 @@ class Playlist(QTableWidget): def _save_playlist(self, session): """ - Save playlist to database. We do this by correcting differences - between the on-screen (definitive) playlist and that in the - database. + Save playlist to database. - We treat the notes rows and the tracks rows differently. Notes must - appear only once in only one playlist. Tracks can appear multiple - times in one playlist and in multiple playlists. + For notes: check the database entry is correct and update it if + necessary. Playlists:Note is one:many, so there is only one notes + appearance in all playlists. + + For tracks: erase the playlist tracks and recreate. This is much + simpler than trying to correct any Playlists:Tracks many:many + errors. """ # We need to add ourself to the session us_in_db = session.query(Playlists).filter( - Playlists.id == self.db.id).one() + Playlists.id == self.id).one() # Notes first # Create dictionaries indexed by note_id