Remove link to database object from playlist. Fixes #16

This commit is contained in:
Keith Edmunds 2021-06-06 11:36:27 +01:00
parent e498457395
commit c5f5155332
2 changed files with 16 additions and 15 deletions

View File

@ -285,8 +285,7 @@ class Window(QMainWindow, Ui_MainWindow):
""" """
playlist_table = Playlist(self) playlist_table = Playlist(self)
playlist_table.db = playlist_db playlist_table.populate(session, playlist_db)
playlist_table.populate(session)
idx = self.tabPlaylist.addTab(playlist_table, playlist_db.name) idx = self.tabPlaylist.addTab(playlist_table, playlist_db.name)
self.tabPlaylist.setCurrentIndex(idx) self.tabPlaylist.setCurrentIndex(idx)

View File

@ -244,10 +244,10 @@ class Playlist(QTableWidget):
DEBUG(f"playlists.add_track(track={track}), row={row}") DEBUG(f"playlists.add_track(track={track}), row={row}")
# We need to add ourself to the session # We need to add ourself to the session
us_in_db = session.query(Playlists).filter( playlist_db = session.query(Playlists).filter(
Playlists.id == self.id).one() Playlists.id == self.id).one()
us_in_db.add_track(session, track, row) playlist_db.add_track(session, track, row)
self.insertRow(row) self.insertRow(row)
@ -360,21 +360,24 @@ class Playlist(QTableWidget):
self.current_track_start_time = None self.current_track_start_time = None
self._repaint() self._repaint()
def populate(self, session): def populate(self, session, playlist_db):
# add tracks and notes in row order. """
Populate ourself from the passed playlist_db object
"""
# We don't mandate that an item will be # 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 # First, save our id for the future
self.id = self.db.id self.id = playlist_db.id
self.name = self.db.name self.name = playlist_db.name
data = [] data = []
for t in self.db.tracks: for t in playlist_db.tracks:
data.append(([t.row], t.tracks)) data.append(([t.row], t.tracks))
for n in self.db.notes: for n in playlist_db.notes:
data.append(([n.row], n)) data.append(([n.row], n))
# Clear playlist # Clear playlist
@ -388,7 +391,6 @@ class Playlist(QTableWidget):
scroll_to = self.item(0, self.COL_INDEX) scroll_to = self.item(0, self.COL_INDEX)
self.scrollToItem(scroll_to, QAbstractItemView.PositionAtTop) self.scrollToItem(scroll_to, QAbstractItemView.PositionAtTop)
self._save_playlist(session)
self._repaint() self._repaint()
def repaint(self): def repaint(self):
@ -834,7 +836,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( playlist_db = session.query(Playlists).filter(
Playlists.id == self.id).one() Playlists.id == self.id).one()
# Notes first # Notes first
@ -852,7 +854,7 @@ class Playlist(QTableWidget):
playlist_notes[note_id] = row playlist_notes[note_id] = row
# Database # Database
for note in us_in_db.notes: for note in playlist_db.notes:
database_notes[note.id] = note.row database_notes[note.id] = note.row
# Notes to add to database # Notes to add to database
@ -861,14 +863,14 @@ class Playlist(QTableWidget):
for note_id in set(playlist_notes.keys()) - set(database_notes.keys()): for note_id in set(playlist_notes.keys()) - set(database_notes.keys()):
ERROR( ERROR(
f"_save_playlist(): Note.id={note_id} " f"_save_playlist(): Note.id={note_id} "
f"missing from playlist {us_in_db} in database" f"missing from playlist {playlist_db} in database"
) )
# Notes to remove from database # Notes to remove from database
for note_id in set(database_notes.keys()) - set(playlist_notes.keys()): for note_id in set(database_notes.keys()) - set(playlist_notes.keys()):
DEBUG( DEBUG(
f"_save_playlist(): Delete note note_id={note_id} " f"_save_playlist(): Delete note note_id={note_id} "
f"from playlist {us_in_db} in database" f"from playlist {playlist_db} in database"
) )
Notes.delete_note(session, note_id) Notes.delete_note(session, note_id)