More session fixups
This commit is contained in:
parent
1abe377b4c
commit
6efc103ba5
@ -261,7 +261,7 @@ class Window(QMainWindow, Ui_MainWindow):
|
|||||||
if ok:
|
if ok:
|
||||||
with Session() as session:
|
with Session() as session:
|
||||||
note = self.create_note(session, dlg.textValue())
|
note = self.create_note(session, dlg.textValue())
|
||||||
self.visible_playlist().add_note(note)
|
self.visible_playlist().add_note(session, note)
|
||||||
|
|
||||||
def load_last_playlists(self):
|
def load_last_playlists(self):
|
||||||
"Load the playlists that we loaded at end of last session"
|
"Load the playlists that we loaded at end of last session"
|
||||||
@ -655,7 +655,8 @@ class DbDialog(QDialog):
|
|||||||
|
|
||||||
item = self.ui.matchList.currentItem()
|
item = self.ui.matchList.currentItem()
|
||||||
track_id = item.data(Qt.UserRole)
|
track_id = item.data(Qt.UserRole)
|
||||||
self.add_track(track_id)
|
with Session() as session:
|
||||||
|
self.add_track(session, track_id)
|
||||||
|
|
||||||
def add_selected_and_close(self):
|
def add_selected_and_close(self):
|
||||||
self.add_selected()
|
self.add_selected()
|
||||||
@ -678,12 +679,12 @@ class DbDialog(QDialog):
|
|||||||
|
|
||||||
def double_click(self, entry):
|
def double_click(self, entry):
|
||||||
track_id = entry.data(Qt.UserRole)
|
track_id = entry.data(Qt.UserRole)
|
||||||
self.add_track(track_id)
|
with Session() as session:
|
||||||
|
self.add_track(session, track_id)
|
||||||
# Select search text to make it easier for next search
|
# Select search text to make it easier for next search
|
||||||
self.select_searchtext()
|
self.select_searchtext()
|
||||||
|
|
||||||
def add_track(self, track_id):
|
def add_track(self, session, track_id):
|
||||||
with Session() as session:
|
|
||||||
track = Tracks.track_from_id(session, track_id)
|
track = Tracks.track_from_id(session, track_id)
|
||||||
self.parent().visible_playlist().add_to_playlist(session, track)
|
self.parent().visible_playlist().add_to_playlist(session, track)
|
||||||
# Select search text to make it easier for next search
|
# Select search text to make it easier for next search
|
||||||
|
|||||||
@ -17,7 +17,7 @@ import os
|
|||||||
from config import Config
|
from config import Config
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from log import DEBUG, ERROR
|
from log import DEBUG, ERROR
|
||||||
from model import Notes, PlaylistTracks, Session, Settings, Tracks
|
from model import Notes, Playlists, PlaylistTracks, Session, Settings, Tracks
|
||||||
|
|
||||||
|
|
||||||
class Playlist(QTableWidget):
|
class Playlist(QTableWidget):
|
||||||
@ -222,7 +222,7 @@ class Playlist(QTableWidget):
|
|||||||
Notes object.
|
Notes object.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
DEBUG(f"add_to_playlist(session={session}, data={data})")
|
DEBUG(f"playlists.add_to_playlist(session={session}, data={data})")
|
||||||
|
|
||||||
if isinstance(data, Tracks):
|
if isinstance(data, Tracks):
|
||||||
self.add_track(session, data, repaint=repaint)
|
self.add_track(session, data, repaint=repaint)
|
||||||
@ -241,7 +241,13 @@ class Playlist(QTableWidget):
|
|||||||
row = self.currentRow()
|
row = self.currentRow()
|
||||||
else:
|
else:
|
||||||
row = self.rowCount()
|
row = self.rowCount()
|
||||||
DEBUG(f"add_track(track={track}), row={row}")
|
DEBUG(f"playlists.add_track(track={track}), row={row}")
|
||||||
|
|
||||||
|
# We need to add ourself to the session
|
||||||
|
us_in_db = session.query(Playlists).filter(
|
||||||
|
Playlists.id == self.db.id).one()
|
||||||
|
|
||||||
|
us_in_db.add_track(session, track, row)
|
||||||
|
|
||||||
self.insertRow(row)
|
self.insertRow(row)
|
||||||
|
|
||||||
@ -819,6 +825,10 @@ class Playlist(QTableWidget):
|
|||||||
times in one playlist and in multiple playlists.
|
times in one playlist and in multiple playlists.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# We need to add ourself to the session
|
||||||
|
us_in_db = session.query(Playlists).filter(
|
||||||
|
Playlists.id == self.db.id).one()
|
||||||
|
|
||||||
# Notes first
|
# Notes first
|
||||||
# Create dictionaries indexed by note_id
|
# Create dictionaries indexed by note_id
|
||||||
playlist_notes = {}
|
playlist_notes = {}
|
||||||
@ -834,7 +844,7 @@ class Playlist(QTableWidget):
|
|||||||
playlist_notes[note_id] = row
|
playlist_notes[note_id] = row
|
||||||
|
|
||||||
# Database
|
# Database
|
||||||
for note in self.db.notes:
|
for note in us_in_db.notes:
|
||||||
database_notes[note.id] = note.row
|
database_notes[note.id] = note.row
|
||||||
|
|
||||||
# Notes to add to database
|
# Notes to add to database
|
||||||
@ -843,14 +853,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 {self} in database"
|
f"missing from playlist {us_in_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 {self} in database"
|
f"from playlist {us_in_db} in database"
|
||||||
)
|
)
|
||||||
Notes.delete_note(session, note_id)
|
Notes.delete_note(session, note_id)
|
||||||
|
|
||||||
@ -878,8 +888,7 @@ class Playlist(QTableWidget):
|
|||||||
# Database
|
# Database
|
||||||
# Workaround for issue #10
|
# Workaround for issue #10
|
||||||
# for track in self.db.tracks:
|
# for track in self.db.tracks:
|
||||||
for track in session.query(PlaylistTracks).filter(
|
for track in us_in_db.tracks:
|
||||||
PlaylistTracks.playlist_id == self.db.id).all():
|
|
||||||
database_tracks[track.row] = track.track_id
|
database_tracks[track.row] = track.track_id
|
||||||
|
|
||||||
# Tracks rows to add to database
|
# Tracks rows to add to database
|
||||||
@ -907,7 +916,7 @@ class Playlist(QTableWidget):
|
|||||||
):
|
):
|
||||||
if playlist_tracks[row] != database_tracks[row]:
|
if playlist_tracks[row] != database_tracks[row]:
|
||||||
DEBUG(
|
DEBUG(
|
||||||
"_save_playlist(): Update row={row} in database for "
|
"f_save_playlist(): Update row={row} in database for "
|
||||||
f"playlist {self} from track={database_tracks[row]} "
|
f"playlist {self} from track={database_tracks[row]} "
|
||||||
f"to track={playlist_tracks[row]}"
|
f"to track={playlist_tracks[row]}"
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user