Explicitly save playlist id in playlist

This commit is contained in:
Keith Edmunds 2021-06-04 15:44:29 +01:00
parent 52d48406ea
commit a4bdbfccd0
2 changed files with 34 additions and 9 deletions

View File

@ -286,6 +286,24 @@ class PlaylistTracks(Base):
record.row = new_row record.row = new_row
session.commit() 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 @staticmethod
def remove_track(session, playlist_id, row): def remove_track(session, playlist_id, row):
DEBUG( DEBUG(

View File

@ -245,7 +245,7 @@ class Playlist(QTableWidget):
# We need to add ourself to the session # We need to add ourself to the session
us_in_db = session.query(Playlists).filter( 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) us_in_db.add_track(session, track, row)
@ -361,9 +361,14 @@ class Playlist(QTableWidget):
self._repaint(save_playlist=False) self._repaint(save_playlist=False)
def populate(self, session): 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 # on its specified row, only that it will be above
# larger-numbered row items, and below lower-numbered ones. # larger-numbered row items, and below lower-numbered ones.
# First, save our id for the future
self.id = self.db.id
data = [] data = []
for t in self.db.tracks: for t in self.db.tracks:
@ -816,18 +821,20 @@ class Playlist(QTableWidget):
def _save_playlist(self, session): def _save_playlist(self, session):
""" """
Save playlist to database. We do this by correcting differences Save playlist to database.
between the on-screen (definitive) playlist and that in the
database.
We treat the notes rows and the tracks rows differently. Notes must For notes: check the database entry is correct and update it if
appear only once in only one playlist. Tracks can appear multiple necessary. Playlists:Note is one:many, so there is only one notes
times in one playlist and in multiple playlists. 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 # We need to add ourself to the session
us_in_db = session.query(Playlists).filter( us_in_db = session.query(Playlists).filter(
Playlists.id == self.db.id).one() Playlists.id == self.id).one()
# Notes first # Notes first
# Create dictionaries indexed by note_id # Create dictionaries indexed by note_id