Segregate adding notes, tracks to onscreen playlist and database
This commit is contained in:
parent
e14bed34bd
commit
0ca9bfec0a
@ -175,6 +175,8 @@ class Playlists(Base):
|
||||
if not row:
|
||||
row = PlaylistTracks.new_row(session, self.id)
|
||||
|
||||
DEBUG(f"Playlists:add_track({session=}, {track=}, {row=})")
|
||||
|
||||
glue = PlaylistTracks(row=row)
|
||||
glue.track_id = track.id
|
||||
self.tracks.append(glue)
|
||||
|
||||
@ -25,10 +25,9 @@ import helpers
|
||||
import music
|
||||
|
||||
from config import Config
|
||||
from model import (Notes, Playdates, Playlists, PlaylistTracks,
|
||||
Session, Settings, Tracks)
|
||||
from model import (Notes, Playdates, Playlists, Session, Settings, Tracks)
|
||||
from playlists import Playlist
|
||||
from songdb import add_path_to_db
|
||||
from songdb import create_track_from_file
|
||||
from ui.dlg_search_database_ui import Ui_Dialog
|
||||
from ui.dlg_SelectPlaylist_ui import Ui_dlgSelectPlaylist
|
||||
from ui.main_window_ui import Ui_MainWindow
|
||||
@ -73,10 +72,14 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
if dlg.exec_():
|
||||
with Session() as session:
|
||||
for fname in dlg.selectedFiles():
|
||||
track = add_path_to_db(session, fname)
|
||||
self.visible_playlist().add_to_playlist(session, track)
|
||||
track = create_track_from_file(session, fname)
|
||||
# Add to playlist on screen
|
||||
# If we don't specify "repaint=False", playlist will
|
||||
# also be saved to database
|
||||
self.visible_playlist().insert_track(session, track)
|
||||
|
||||
def set_main_window_size(self):
|
||||
"Set size of window from database"
|
||||
|
||||
with Session() as session:
|
||||
record = Settings.get_int(session, "mainwindow_x")
|
||||
@ -197,7 +200,7 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
DEBUG(f"musicmuster.create_note(text={text}): row={row}")
|
||||
|
||||
note = Notes.add_note(
|
||||
session, self.visible_playlist().db.id, row, text)
|
||||
session, self.visible_playlist().id, row, text)
|
||||
|
||||
return note
|
||||
|
||||
@ -267,7 +270,7 @@ class Window(QMainWindow, Ui_MainWindow):
|
||||
if ok:
|
||||
with Session() as session:
|
||||
note = self.create_note(session, dlg.textValue())
|
||||
self.visible_playlist().add_note(session, note)
|
||||
self.visible_playlist().insert_note(session, note)
|
||||
|
||||
def load_last_playlists(self):
|
||||
"Load the playlists that we loaded at end of last session"
|
||||
@ -688,7 +691,11 @@ class DbDialog(QDialog):
|
||||
|
||||
def add_track(self, track_id):
|
||||
track = Tracks.track_from_id(self.session, track_id)
|
||||
self.parent().visible_playlist().add_to_playlist(self.session, track)
|
||||
# Add to playlist on screen
|
||||
# If we don't specify "repaint=False", playlist will
|
||||
# also be saved to database
|
||||
self.parent().visible_playlist().insert_track(
|
||||
self.session, track)
|
||||
# Select search text to make it easier for next search
|
||||
self.select_searchtext()
|
||||
|
||||
|
||||
@ -167,19 +167,21 @@ class Playlist(QTableWidget):
|
||||
|
||||
# ########## Externally called functions ##########
|
||||
|
||||
def add_note(self, session, note, repaint=True):
|
||||
def insert_note(self, session, note, repaint=True):
|
||||
"""
|
||||
Add note to playlist
|
||||
|
||||
If a row is selected, add note above. Otherwise, add to end of
|
||||
playlist.
|
||||
|
||||
Return the row number that track is now in.
|
||||
"""
|
||||
|
||||
if self.selectionModel().hasSelection():
|
||||
row = self.currentRow()
|
||||
else:
|
||||
row = self.rowCount()
|
||||
DEBUG(f"playlist.add_note(): row={row}")
|
||||
DEBUG(f"playlist.inset_note(): row={row}")
|
||||
|
||||
# Does note end with a time?
|
||||
start_time = None
|
||||
@ -216,38 +218,26 @@ class Playlist(QTableWidget):
|
||||
self._save_playlist(session)
|
||||
self._repaint(clear_selection=False)
|
||||
|
||||
def add_to_playlist(self, session, data, repaint=True):
|
||||
"""
|
||||
Add data to playlist. Data may be either a Tracks object or a
|
||||
Notes object.
|
||||
"""
|
||||
return row
|
||||
|
||||
DEBUG(f"playlists.add_to_playlist(session={session}, data={data})")
|
||||
|
||||
if isinstance(data, Tracks):
|
||||
self.add_track(session, data, repaint=repaint)
|
||||
elif isinstance(data, Notes):
|
||||
self.add_note(session, data, repaint=repaint)
|
||||
|
||||
def add_track(self, session, track, repaint=True):
|
||||
def insert_track(self, session, track, repaint=True):
|
||||
"""
|
||||
Add track to playlist
|
||||
Insert track into on-screen playlist.
|
||||
|
||||
If a row is selected, add track above. Otherwise, add to end of
|
||||
playlist.
|
||||
|
||||
Return the row number that track is now in.
|
||||
"""
|
||||
|
||||
if self.selectionModel().hasSelection():
|
||||
row = self.currentRow()
|
||||
else:
|
||||
row = self.rowCount()
|
||||
DEBUG(f"playlists.add_track(track={track}), row={row}")
|
||||
|
||||
# We need to add ourself to the session
|
||||
playlist_db = session.query(Playlists).filter(
|
||||
Playlists.id == self.id).one()
|
||||
|
||||
playlist_db.add_track(session, track, row)
|
||||
DEBUG(
|
||||
f"playlists.insert_track({session=}, {track=}, {repaint=}), "
|
||||
f"{row=}"
|
||||
)
|
||||
|
||||
self.insertRow(row)
|
||||
|
||||
@ -275,6 +265,8 @@ class Playlist(QTableWidget):
|
||||
self._save_playlist(session)
|
||||
self._repaint(clear_selection=False)
|
||||
|
||||
return row
|
||||
|
||||
def clear_current(self):
|
||||
"Clear current track"
|
||||
|
||||
@ -384,8 +376,12 @@ class Playlist(QTableWidget):
|
||||
self.setRowCount(0)
|
||||
|
||||
# Now add data in row order
|
||||
for item in sorted(data, key=lambda x: x[0]):
|
||||
self.add_to_playlist(session, item[1], repaint=False)
|
||||
for i in sorted(data, key=lambda x: x[0]):
|
||||
item = i[1]
|
||||
if isinstance(item, Tracks):
|
||||
self.insert_track(session, item, repaint=False)
|
||||
elif isinstance(item, Notes):
|
||||
self.insert_note(session, item, repaint=False)
|
||||
|
||||
# Scroll to top
|
||||
scroll_to = self.item(0, self.COL_INDEX)
|
||||
|
||||
@ -34,8 +34,12 @@ def main():
|
||||
INFO("Finished")
|
||||
|
||||
|
||||
def add_path_to_db(session, path):
|
||||
"Add passed path to database along with metadata"
|
||||
def create_track_from_file(session, path):
|
||||
"""
|
||||
Create track in database from passed path.
|
||||
|
||||
Return track.
|
||||
"""
|
||||
|
||||
track = Tracks.get_or_create(session, path)
|
||||
tag = TinyTag.get(path)
|
||||
@ -61,7 +65,7 @@ def add_path_to_db(session, path):
|
||||
fd, temp_path = tempfile.mkstemp()
|
||||
shutil.copyfile(path, temp_path)
|
||||
except Exception as err:
|
||||
DEBUG(f"songdb.add_path_to_db({path}): err1: {str(err)}")
|
||||
DEBUG(f"songdb.create_track_from_file({path}): err1: {str(err)}")
|
||||
return
|
||||
|
||||
# Overwrite original file with normalised output
|
||||
@ -78,7 +82,7 @@ def add_path_to_db(session, path):
|
||||
dst[tag] = src[tag]
|
||||
dst.save()
|
||||
except Exception as err:
|
||||
DEBUG(f"songdb.add_path_to_db({path}): err2: {str(err)}")
|
||||
DEBUG(f"songdb.create_track_from_file({path}): err2: {str(err)}")
|
||||
# Restore original file
|
||||
shutil.copyfile(path, temp_path)
|
||||
finally:
|
||||
@ -190,7 +194,7 @@ def update_db(session):
|
||||
for path in list(os_paths - db_paths):
|
||||
# TODO
|
||||
INFO(f"Adding to dataabase: {path}")
|
||||
add_path_to_db(session, path)
|
||||
create_track_from_file(session, path)
|
||||
|
||||
|
||||
if __name__ == '__main__' and '__file__' in globals():
|
||||
|
||||
Loading…
Reference in New Issue
Block a user