Fix moving tracks between playlists
This commit is contained in:
parent
823d0b6628
commit
37ccf7c325
10
app/model.py
10
app/model.py
@ -286,10 +286,14 @@ class PlaylistTracks(Base):
|
||||
f"{from_playlist_id}, row={row}, "
|
||||
f"to_playlist_id={to_playlist_id})"
|
||||
)
|
||||
new_row = (
|
||||
session.query(func.max(PlaylistTracks.row)).filter(
|
||||
max_row = session.query(func.max(PlaylistTracks.row)).filter(
|
||||
PlaylistTracks.playlist_id == to_playlist_id).scalar()
|
||||
) + 1
|
||||
if max_row is None:
|
||||
# Destination playlist is empty; use row 0
|
||||
new_row = 0
|
||||
else:
|
||||
# Destination playlist has tracks; add to end
|
||||
new_row = max_row + 1
|
||||
record = session.query(PlaylistTracks).filter(
|
||||
PlaylistTracks.playlist_id == from_playlist_id,
|
||||
PlaylistTracks.row == row
|
||||
|
||||
@ -25,7 +25,8 @@ import helpers
|
||||
import music
|
||||
|
||||
from config import Config
|
||||
from model import (Notes, Playdates, Playlists, Session, Settings, Tracks)
|
||||
from model import (Notes, Playdates, Playlists, PlaylistTracks,
|
||||
Session, Settings, Tracks)
|
||||
from playlists import Playlist
|
||||
from songdb import create_track_from_file
|
||||
from ui.dlg_search_database_ui import Ui_Dialog
|
||||
@ -299,10 +300,8 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
"Move selected rows to another playlist"
|
||||
|
||||
with Session() as session:
|
||||
playlists = list(
|
||||
set(Playlists.get_all_playlists(session)) -
|
||||
{self.visible_playlist().db}
|
||||
)
|
||||
playlists = [p for p in Playlists.get_all_playlists(session)
|
||||
if p.id != self.visible_playlist().id]
|
||||
dlg = SelectPlaylistDialog(self, playlists=playlists)
|
||||
dlg.exec()
|
||||
if not dlg.plid:
|
||||
@ -311,23 +310,30 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
# If destination playlist is visible, we need to add the moved
|
||||
# tracks to it. If not, they will be automatically loaded when
|
||||
# the playlistis opened.
|
||||
destination_playlist = None
|
||||
destination_visible_playlist_tab = None
|
||||
for tab in range(self.tabPlaylist.count()):
|
||||
if self.tabPlaylist.widget(tab).db.id == dlg.plid:
|
||||
destination_playlist = self.tabPlaylist.widget(tab)
|
||||
if self.tabPlaylist.widget(tab).id == dlg.plid:
|
||||
destination_visible_playlist_tab = (
|
||||
self.tabPlaylist.widget(tab))
|
||||
break
|
||||
|
||||
rows = []
|
||||
for (row, track_id) in (
|
||||
self.visible_playlist().get_selected_rows_and_tracks()):
|
||||
rows.append(row)
|
||||
track = Tracks.track_from_id(session, track_id)
|
||||
if destination_visible_playlist_tab:
|
||||
# Insert with repaint=False to not update database
|
||||
destination_visible_playlist_tab.insert_track(
|
||||
session, track, repaint=False)
|
||||
|
||||
# Update database
|
||||
PlaylistTracks.move_track(
|
||||
session, self.visible_playlist().db.id, row, dlg.plid)
|
||||
# Update destination playlist if visible
|
||||
if destination_playlist:
|
||||
destination_playlist.add_track(
|
||||
session, Tracks.track_from_id(session, track_id))
|
||||
session, self.visible_playlist().id, row, dlg.plid)
|
||||
|
||||
# Update destination playlist if visible
|
||||
if destination_visible_playlist_tab:
|
||||
destination_visible_playlist_tab.repaint()
|
||||
|
||||
# Update source playlist
|
||||
self.visible_playlist().remove_rows(rows)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user