Cleaned up save_playlist(); improved DEBUG logging.
This commit is contained in:
parent
f61c6fd74f
commit
5d65bebd1e
11
app/model.py
11
app/model.py
@ -68,12 +68,21 @@ class Notes(Base):
|
|||||||
session.query(Notes).filter(Notes.id == id).delete()
|
session.query(Notes).filter(Notes.id == id).delete()
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_note(id):
|
||||||
|
return session.query(Notes).filter(Notes.id == id).one()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def update_note(cls, id, row, text):
|
def update_note(cls, id, row, text=None):
|
||||||
|
"""
|
||||||
|
Update note details. If text=None, don't change text.
|
||||||
|
"""
|
||||||
|
|
||||||
DEBUG(f"update_note(id={id}, row={row}, text={text})")
|
DEBUG(f"update_note(id={id}, row={row}, text={text})")
|
||||||
|
|
||||||
note = session.query(cls).filter(cls.id == id).one()
|
note = session.query(cls).filter(cls.id == id).one()
|
||||||
note.row = row
|
note.row = row
|
||||||
|
if text:
|
||||||
note.note = text
|
note.note = text
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
|||||||
130
app/playlists.py
130
app/playlists.py
@ -192,7 +192,10 @@ class Playlist(QTableWidget):
|
|||||||
note.note[-9:], " %H:%M:%S").time()
|
note.note[-9:], " %H:%M:%S").time()
|
||||||
DEBUG(f"Note contains valid time={start_time}")
|
DEBUG(f"Note contains valid time={start_time}")
|
||||||
except ValueError:
|
except ValueError:
|
||||||
DEBUG("Note does not contain valid time")
|
DEBUG(
|
||||||
|
f"Note on row {row} ('{note.note}') "
|
||||||
|
"does not contain valid time"
|
||||||
|
)
|
||||||
|
|
||||||
item = QTableWidgetItem(str(note.id))
|
item = QTableWidgetItem(str(note.id))
|
||||||
self.setItem(row, self.COL_INDEX, item)
|
self.setItem(row, self.COL_INDEX, item)
|
||||||
@ -588,7 +591,7 @@ class Playlist(QTableWidget):
|
|||||||
def repaint(self, clear_selection=True):
|
def repaint(self, clear_selection=True):
|
||||||
"Set row colours, fonts, etc, and save playlist"
|
"Set row colours, fonts, etc, and save playlist"
|
||||||
|
|
||||||
self.save()
|
self.save_playlist()
|
||||||
|
|
||||||
if clear_selection:
|
if clear_selection:
|
||||||
self.clearSelection()
|
self.clearSelection()
|
||||||
@ -641,7 +644,7 @@ class Playlist(QTableWidget):
|
|||||||
self.item(row, self.COL_ENDTIME).text(), "%H:%M:%S"
|
self.item(row, self.COL_ENDTIME).text(), "%H:%M:%S"
|
||||||
)
|
)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
DEBUG("Note does not contain valid time")
|
pass
|
||||||
# Set colour
|
# Set colour
|
||||||
self.set_row_colour(
|
self.set_row_colour(
|
||||||
row, QColor(Config.COLOUR_NOTES_PLAYLIST)
|
row, QColor(Config.COLOUR_NOTES_PLAYLIST)
|
||||||
@ -676,72 +679,101 @@ class Playlist(QTableWidget):
|
|||||||
# Headers might need updating
|
# Headers might need updating
|
||||||
self.parent().parent().update_headers()
|
self.parent().parent().update_headers()
|
||||||
|
|
||||||
def save(self):
|
def save_playlist(self):
|
||||||
"""
|
"""
|
||||||
Save playlist to database.
|
Save playlist to database. Add missing notes/tracks; remove any that
|
||||||
|
are in database but not playlist. Correct row number in database if
|
||||||
Notes are also saved.
|
necessary.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Create list of current tracks (row, track_id) for this playlst and
|
|
||||||
# compare with actual playlist. Fix as required.
|
|
||||||
# Repeat for notes (row, id, text)
|
|
||||||
|
|
||||||
tracks = {}
|
|
||||||
notes = {}
|
|
||||||
note_rows = self.meta_get_notes()
|
note_rows = self.meta_get_notes()
|
||||||
|
playlist = Playlists.get_playlist_by_id(self.playlist_id)
|
||||||
|
|
||||||
|
# Create dictionaries indexed by track/note id
|
||||||
|
playlist_notes = {}
|
||||||
|
playlist_tracks = {}
|
||||||
|
database_notes = {}
|
||||||
|
database_tracks = {}
|
||||||
|
|
||||||
|
# Playlist
|
||||||
for row in range(self.rowCount()):
|
for row in range(self.rowCount()):
|
||||||
|
# Get id of item
|
||||||
if self.item(row, self.COL_INDEX):
|
if self.item(row, self.COL_INDEX):
|
||||||
id = int(self.item(row, self.COL_INDEX).text())
|
id = int(self.item(row, self.COL_INDEX).text())
|
||||||
else:
|
else:
|
||||||
DEBUG(f"(playlist.save(): no COL_INDEX data in row {row}")
|
DEBUG(f"(save_playlist(): no COL_INDEX data in row {row}")
|
||||||
continue
|
continue
|
||||||
if row in note_rows:
|
if row in note_rows:
|
||||||
notes[id] = (row, self.item(row, self.COL_NOTE).text())
|
playlist_notes[id] = row
|
||||||
else:
|
else:
|
||||||
tracks[id] = row
|
playlist_tracks[id] = row
|
||||||
|
|
||||||
# Get tracks and notes from database
|
# Database
|
||||||
db_tracks = {}
|
for note in playlist.notes:
|
||||||
db_notes = {}
|
database_notes[note.id] = note.row
|
||||||
p = Playlists.get_playlist_by_id(self.playlist_id)
|
for track in playlist.tracks:
|
||||||
|
database_tracks[track.track_id] = track.row
|
||||||
|
|
||||||
for track in p.tracks:
|
# Notes to remove from playlist in database
|
||||||
db_tracks[track.track_id] = track.row
|
for note_id in set(database_notes.keys()) - set(playlist_notes.keys()):
|
||||||
|
DEBUG(
|
||||||
|
f"save_playlist(): Delete note.id={id} "
|
||||||
|
f"from playlist {playlist} in database"
|
||||||
|
)
|
||||||
|
Notes.delete_note(note_id)
|
||||||
|
|
||||||
for note in p.notes:
|
# Tracks to remove from playlist in database
|
||||||
db_notes[note.id] = (note.row, note.note)
|
for track_id in (
|
||||||
|
set(database_tracks.keys()) - set(playlist_tracks.keys())
|
||||||
|
):
|
||||||
|
DEBUG(
|
||||||
|
f"save_playlist(): Delete track.id={track_id} "
|
||||||
|
f"from playlist {playlist} in database"
|
||||||
|
)
|
||||||
|
PlaylistTracks.remove_track(playlist.id, track_id)
|
||||||
|
|
||||||
# Note ids to remove from db
|
# Notes to add to playlist database
|
||||||
for id in set(db_notes.keys()) - set(notes.keys()):
|
# This should never be needed as notes are added to a specific
|
||||||
DEBUG(f"playlist.save(): Delete note.id={id} from database")
|
# playlist upon creation
|
||||||
|
for note_id in set(playlist_notes.keys()) - set(database_notes.keys()):
|
||||||
|
ERROR(
|
||||||
|
f"save_playlist(): Note.id={note_id} "
|
||||||
|
f"missing from playlist {playlist} in database"
|
||||||
|
)
|
||||||
|
|
||||||
# Note ids to add to db
|
# Notes to remove from playlist database
|
||||||
for id in set(notes.keys()) - set(db_notes.keys()):
|
for note_id in set(database_notes.keys()) - set(playlist_notes.keys()):
|
||||||
DEBUG(f"playlist.save(): Add note.id={id} to database")
|
DEBUG(
|
||||||
|
f"save_playlist(): Remove note.id={note_id} "
|
||||||
|
f"from playlist {playlist} in database"
|
||||||
|
)
|
||||||
|
Notes.delete_note(note_id)
|
||||||
|
|
||||||
# Notes to update in db
|
# Note rows to update in playlist database
|
||||||
for id in set(notes.keys()) & set(db_notes.keys()):
|
for note_id in set(playlist_notes.keys()) & set(database_notes.keys()):
|
||||||
if notes[id] != db_notes[id]:
|
if playlist_notes[note_id] != database_notes[note_id]:
|
||||||
DEBUG(f"playlist.save(): Update db note.id={id} in database")
|
DEBUG(
|
||||||
Notes.update_note(id, row=notes[id][0], text=notes[id][1])
|
f"save_playlist(): Set database note.id {note_id} "
|
||||||
|
f"row={playlist_notes[note_id]} "
|
||||||
|
f"in playlist {playlist} in database"
|
||||||
|
)
|
||||||
|
Notes.update_note(note_id, playlist_notes[note_id])
|
||||||
|
|
||||||
# Track ids to remove from db
|
# Track rows to update in playlist database
|
||||||
for id in set(db_tracks.keys()) - set(tracks.keys()):
|
for track_id in (
|
||||||
DEBUG(f"playlist.save(): Delete track.id={id} from database")
|
set(playlist_tracks.keys()) & set(database_tracks.keys())
|
||||||
|
):
|
||||||
# Track ids to add to db
|
if playlist_tracks[track_id] != database_tracks[track_id]:
|
||||||
for id in set(tracks.keys()) - set(db_tracks.keys()):
|
DEBUG(
|
||||||
DEBUG(f"playlist.save(): Add track.id={id} to database")
|
f"save_playlist(): Set database track.id {track_id} "
|
||||||
|
f"row={playlist_tracks[track_id]} "
|
||||||
# Tracks to update in db
|
f"in playlist {playlist} in database"
|
||||||
for id in set(tracks.keys()) & set(db_tracks.keys()):
|
)
|
||||||
if tracks[id] != db_tracks[id]:
|
|
||||||
DEBUG(f"playlist.save(): Update db track.id={id} in database")
|
|
||||||
PlaylistTracks.update_track_row(
|
PlaylistTracks.update_track_row(
|
||||||
self.playlist_id, track_id=id,
|
playlist_id=self.playlist_id,
|
||||||
old_row=db_tracks[id], new_row=tracks[id]
|
track_id=track_id,
|
||||||
|
old_row=database_tracks[track_id],
|
||||||
|
new_row=playlist_tracks[track_id]
|
||||||
)
|
)
|
||||||
|
|
||||||
def set_column_widths(self):
|
def set_column_widths(self):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user