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