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"{from_playlist_id}, row={row}, "
|
||||||
f"to_playlist_id={to_playlist_id})"
|
f"to_playlist_id={to_playlist_id})"
|
||||||
)
|
)
|
||||||
new_row = (
|
max_row = session.query(func.max(PlaylistTracks.row)).filter(
|
||||||
session.query(func.max(PlaylistTracks.row)).filter(
|
|
||||||
PlaylistTracks.playlist_id == to_playlist_id).scalar()
|
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(
|
record = session.query(PlaylistTracks).filter(
|
||||||
PlaylistTracks.playlist_id == from_playlist_id,
|
PlaylistTracks.playlist_id == from_playlist_id,
|
||||||
PlaylistTracks.row == row
|
PlaylistTracks.row == row
|
||||||
|
|||||||
@ -25,7 +25,8 @@ import helpers
|
|||||||
import music
|
import music
|
||||||
|
|
||||||
from config import Config
|
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 playlists import Playlist
|
||||||
from songdb import create_track_from_file
|
from songdb import create_track_from_file
|
||||||
from ui.dlg_search_database_ui import Ui_Dialog
|
from ui.dlg_search_database_ui import Ui_Dialog
|
||||||
@ -299,10 +300,8 @@ class Window(QMainWindow, Ui_MainWindow):
|
|||||||
"Move selected rows to another playlist"
|
"Move selected rows to another playlist"
|
||||||
|
|
||||||
with Session() as session:
|
with Session() as session:
|
||||||
playlists = list(
|
playlists = [p for p in Playlists.get_all_playlists(session)
|
||||||
set(Playlists.get_all_playlists(session)) -
|
if p.id != self.visible_playlist().id]
|
||||||
{self.visible_playlist().db}
|
|
||||||
)
|
|
||||||
dlg = SelectPlaylistDialog(self, playlists=playlists)
|
dlg = SelectPlaylistDialog(self, playlists=playlists)
|
||||||
dlg.exec()
|
dlg.exec()
|
||||||
if not dlg.plid:
|
if not dlg.plid:
|
||||||
@ -311,23 +310,30 @@ class Window(QMainWindow, Ui_MainWindow):
|
|||||||
# If destination playlist is visible, we need to add the moved
|
# If destination playlist is visible, we need to add the moved
|
||||||
# tracks to it. If not, they will be automatically loaded when
|
# tracks to it. If not, they will be automatically loaded when
|
||||||
# the playlistis opened.
|
# the playlistis opened.
|
||||||
destination_playlist = None
|
destination_visible_playlist_tab = None
|
||||||
for tab in range(self.tabPlaylist.count()):
|
for tab in range(self.tabPlaylist.count()):
|
||||||
if self.tabPlaylist.widget(tab).db.id == dlg.plid:
|
if self.tabPlaylist.widget(tab).id == dlg.plid:
|
||||||
destination_playlist = self.tabPlaylist.widget(tab)
|
destination_visible_playlist_tab = (
|
||||||
|
self.tabPlaylist.widget(tab))
|
||||||
break
|
break
|
||||||
|
|
||||||
rows = []
|
rows = []
|
||||||
for (row, track_id) in (
|
for (row, track_id) in (
|
||||||
self.visible_playlist().get_selected_rows_and_tracks()):
|
self.visible_playlist().get_selected_rows_and_tracks()):
|
||||||
rows.append(row)
|
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
|
# Update database
|
||||||
PlaylistTracks.move_track(
|
PlaylistTracks.move_track(
|
||||||
session, self.visible_playlist().db.id, row, dlg.plid)
|
session, self.visible_playlist().id, row, dlg.plid)
|
||||||
# Update destination playlist if visible
|
|
||||||
if destination_playlist:
|
# Update destination playlist if visible
|
||||||
destination_playlist.add_track(
|
if destination_visible_playlist_tab:
|
||||||
session, Tracks.track_from_id(session, track_id))
|
destination_visible_playlist_tab.repaint()
|
||||||
|
|
||||||
# Update source playlist
|
# Update source playlist
|
||||||
self.visible_playlist().remove_rows(rows)
|
self.visible_playlist().remove_rows(rows)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user