Tidy up saving database
This commit is contained in:
parent
d3958db8a3
commit
35b101a538
@ -2,6 +2,7 @@
|
|||||||
#
|
#
|
||||||
import os.path
|
import os.path
|
||||||
import re
|
import re
|
||||||
|
import stackprinter
|
||||||
#
|
#
|
||||||
from dbconfig import Session
|
from dbconfig import Session
|
||||||
#
|
#
|
||||||
@ -417,7 +418,7 @@ class PlaylistRows(Base):
|
|||||||
return (
|
return (
|
||||||
f"<PlaylistRow(id={self.id}, playlist_id={self.playlist_id}, "
|
f"<PlaylistRow(id={self.id}, playlist_id={self.playlist_id}, "
|
||||||
f"track_id={self.track_id}, "
|
f"track_id={self.track_id}, "
|
||||||
f"note={self.note} row_number={self.row_number}>"
|
f"note={self.note}, row_number={self.row_number}>"
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
@ -452,7 +453,7 @@ class PlaylistRows(Base):
|
|||||||
plr.note)
|
plr.note)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def delete_higher_rows(session: Session, playlist_id: int, row: int) \
|
def delete_higher_rows(session: Session, playlist_id: int, maxrow: int) \
|
||||||
-> None:
|
-> None:
|
||||||
"""
|
"""
|
||||||
Delete rows in given playlist that have a higher row number
|
Delete rows in given playlist that have a higher row number
|
||||||
@ -463,7 +464,7 @@ class PlaylistRows(Base):
|
|||||||
rows_to_go = session.execute(
|
rows_to_go = session.execute(
|
||||||
select(PlaylistRows)
|
select(PlaylistRows)
|
||||||
.where(PlaylistRows.playlist_id == playlist_id,
|
.where(PlaylistRows.playlist_id == playlist_id,
|
||||||
PlaylistRows.row_number > row)
|
PlaylistRows.row_number > maxrow)
|
||||||
).scalars().all()
|
).scalars().all()
|
||||||
if not rows_to_go:
|
if not rows_to_go:
|
||||||
return
|
return
|
||||||
@ -472,11 +473,6 @@ class PlaylistRows(Base):
|
|||||||
log.debug(f"Should delete: {row}")
|
log.debug(f"Should delete: {row}")
|
||||||
# If needed later:
|
# If needed later:
|
||||||
# session.delete(row)
|
# session.delete(row)
|
||||||
rows_to_go = session.execute(
|
|
||||||
select(PlaylistRows)
|
|
||||||
.where(PlaylistRows.playlist_id == playlist_id,
|
|
||||||
PlaylistRows.row_number > row)
|
|
||||||
).scalars().all()
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def delete_rows(session: Session, ids: List[int]) -> None:
|
def delete_rows(session: Session, ids: List[int]) -> None:
|
||||||
@ -509,6 +505,42 @@ class PlaylistRows(Base):
|
|||||||
# Ensure new row numbers are available to the caller
|
# Ensure new row numbers are available to the caller
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_plr(session: Session, row_number: int,
|
||||||
|
playlist_id: int) -> "PlaylistRows":
|
||||||
|
"""Return playlistrows object matching passed parameters"""
|
||||||
|
|
||||||
|
return (
|
||||||
|
select(PlaylistRows)
|
||||||
|
.where(
|
||||||
|
PlaylistRows.row_number == row_number,
|
||||||
|
PlaylistRows.playlist_id == playlist_id)
|
||||||
|
.limit(1)
|
||||||
|
).first()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_track_plr(session: Session, track_id: int,
|
||||||
|
playlist_id: int) -> Optional["PlaylistRows"]:
|
||||||
|
"""Return first matching PlaylistRows object or None"""
|
||||||
|
|
||||||
|
return session.scalars(
|
||||||
|
select(PlaylistRows)
|
||||||
|
.where(
|
||||||
|
PlaylistRows.track_id == track_id,
|
||||||
|
PlaylistRows.playlist_id == playlist_id
|
||||||
|
)
|
||||||
|
.limit(1)
|
||||||
|
).first()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_last_used_row(session: Session, playlist_id: int) -> Optional[int]:
|
||||||
|
"""Return the last used row for playlist, or None if no rows"""
|
||||||
|
|
||||||
|
return session.execute(
|
||||||
|
select(func.max(PlaylistRows.row_number))
|
||||||
|
.where(PlaylistRows.playlist_id == playlist_id)
|
||||||
|
).scalar_one()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_played_rows(cls, session: Session,
|
def get_played_rows(cls, session: Session,
|
||||||
playlist_id: int) -> List[int]:
|
playlist_id: int) -> List[int]:
|
||||||
@ -547,15 +579,6 @@ class PlaylistRows(Base):
|
|||||||
|
|
||||||
return plrs
|
return plrs
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_last_used_row(session: Session, playlist_id: int) -> Optional[int]:
|
|
||||||
"""Return the last used row for playlist, or None if no rows"""
|
|
||||||
|
|
||||||
return session.execute(
|
|
||||||
select(func.max(PlaylistRows.row_number))
|
|
||||||
.where(PlaylistRows.playlist_id == playlist_id)
|
|
||||||
).scalar_one()
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_unplayed_rows(cls, session: Session,
|
def get_unplayed_rows(cls, session: Session,
|
||||||
playlist_id: int) -> List[int]:
|
playlist_id: int) -> List[int]:
|
||||||
|
|||||||
@ -739,6 +739,7 @@ class Window(QMainWindow, Ui_MainWindow):
|
|||||||
helpers.set_track_metadata(session, track)
|
helpers.set_track_metadata(session, track)
|
||||||
helpers.normalise_track(track.path)
|
helpers.normalise_track(track.path)
|
||||||
self.visible_playlist_tab().insert_track(session, track)
|
self.visible_playlist_tab().insert_track(session, track)
|
||||||
|
self.visible_playlist_tab().save_playlist(session)
|
||||||
|
|
||||||
def insert_header(self) -> None:
|
def insert_header(self) -> None:
|
||||||
"""Show dialog box to enter header text and add to playlist"""
|
"""Show dialog box to enter header text and add to playlist"""
|
||||||
@ -758,6 +759,7 @@ class Window(QMainWindow, Ui_MainWindow):
|
|||||||
if ok:
|
if ok:
|
||||||
with Session() as session:
|
with Session() as session:
|
||||||
playlist_tab.insert_header(session, dlg.textValue())
|
playlist_tab.insert_header(session, dlg.textValue())
|
||||||
|
self.save_playlist(session)
|
||||||
|
|
||||||
def insert_track(self) -> None:
|
def insert_track(self) -> None:
|
||||||
"""Show dialog box to select and add track from database"""
|
"""Show dialog box to select and add track from database"""
|
||||||
@ -828,13 +830,13 @@ class Window(QMainWindow, Ui_MainWindow):
|
|||||||
|
|
||||||
# Update destination playlist_tab if visible (if not visible, it
|
# Update destination playlist_tab if visible (if not visible, it
|
||||||
# will be re-populated when it is opened)
|
# will be re-populated when it is opened)
|
||||||
destionation_playlist_tab = None
|
destination_playlist_tab = None
|
||||||
for tab in range(self.tabPlaylist.count()):
|
for tab in range(self.tabPlaylist.count()):
|
||||||
if self.tabPlaylist.widget(tab).playlist_id == dlg.playlist.id:
|
if self.tabPlaylist.widget(tab).playlist_id == dlg.playlist.id:
|
||||||
destionation_playlist_tab = self.tabPlaylist.widget(tab)
|
destination_playlist_tab = self.tabPlaylist.widget(tab)
|
||||||
break
|
break
|
||||||
if destionation_playlist_tab:
|
if destination_playlist_tab:
|
||||||
destionation_playlist_tab.populate(session, dlg.playlist.id)
|
destination_playlist_tab.populate(session, dlg.playlist.id)
|
||||||
|
|
||||||
def move_selected(self) -> None:
|
def move_selected(self) -> None:
|
||||||
"""
|
"""
|
||||||
@ -1500,8 +1502,8 @@ class DbDialog(QDialog):
|
|||||||
|
|
||||||
self.parent().visible_playlist_tab().insert_track(
|
self.parent().visible_playlist_tab().insert_track(
|
||||||
self.session, track, note=self.ui.txtNote.text())
|
self.session, track, note=self.ui.txtNote.text())
|
||||||
# Commit session to get correct row numbers if more tracks added
|
# Save to database (which will also commit changes)
|
||||||
self.session.commit()
|
self.parent().visible_playlist_tab().save_playlist(self.session)
|
||||||
# Clear note field and select search text to make it easier for
|
# Clear note field and select search text to make it easier for
|
||||||
# next search
|
# next search
|
||||||
self.ui.txtNote.clear()
|
self.ui.txtNote.clear()
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import re
|
import re
|
||||||
import stackprinter
|
import stackprinter # type: ignore
|
||||||
import subprocess
|
import subprocess
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
@ -199,7 +199,7 @@ class PlaylistTab(QTableWidget):
|
|||||||
# self.setSortingEnabled(True)
|
# self.setSortingEnabled(True)
|
||||||
|
|
||||||
# Now load our tracks and notes
|
# Now load our tracks and notes
|
||||||
self.populate(session, self.playlist_id)
|
self.populate_display(session, self.playlist_id)
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"<PlaylistTab(id={self.playlist_id}>"
|
return f"<PlaylistTab(id={self.playlist_id}>"
|
||||||
@ -623,7 +623,7 @@ class PlaylistTab(QTableWidget):
|
|||||||
# Add track details to items
|
# Add track details to items
|
||||||
try:
|
try:
|
||||||
start_gap = row_data.track.start_gap
|
start_gap = row_data.track.start_gap
|
||||||
except:
|
except AttributeError:
|
||||||
return
|
return
|
||||||
start_gap_item = QTableWidgetItem(str(start_gap))
|
start_gap_item = QTableWidgetItem(str(start_gap))
|
||||||
if start_gap and start_gap >= 500:
|
if start_gap and start_gap >= 500:
|
||||||
@ -771,10 +771,10 @@ class PlaylistTab(QTableWidget):
|
|||||||
self._clear_current_track_row()
|
self._clear_current_track_row()
|
||||||
self.current_track_start_time = None
|
self.current_track_start_time = None
|
||||||
|
|
||||||
def populate(self, session: Session, playlist_id: int,
|
def populate_display(self, session: Session, playlist_id: int,
|
||||||
scroll_to_top: bool = True) -> None:
|
scroll_to_top: bool = True) -> None:
|
||||||
"""
|
"""
|
||||||
Populate from the associated playlist ID
|
Populate display from the associated playlist ID
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Sanity check row numbering before we load
|
# Sanity check row numbering before we load
|
||||||
@ -1344,9 +1344,8 @@ class PlaylistTab(QTableWidget):
|
|||||||
Delete mutliple rows
|
Delete mutliple rows
|
||||||
|
|
||||||
Actions required:
|
Actions required:
|
||||||
- Delete the rows from the PlaylistRows table
|
|
||||||
- Correct the row numbers in the PlaylistRows table
|
|
||||||
- Remove the rows from the display
|
- Remove the rows from the display
|
||||||
|
- Save the playlist
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Delete rows from database
|
# Delete rows from database
|
||||||
@ -1359,14 +1358,11 @@ class PlaylistTab(QTableWidget):
|
|||||||
f"Really delete {row_count} row{plural}?"):
|
f"Really delete {row_count} row{plural}?"):
|
||||||
return
|
return
|
||||||
|
|
||||||
with Session() as session:
|
|
||||||
PlaylistRows.delete_rows(session, plr_ids)
|
|
||||||
|
|
||||||
# Fix up row numbers left in this playlist
|
|
||||||
PlaylistRows.fixup_rownumbers(session, self.playlist_id)
|
|
||||||
# Remove selected rows from display
|
|
||||||
self.remove_selected_rows()
|
self.remove_selected_rows()
|
||||||
|
|
||||||
|
with Session() as session:
|
||||||
|
self.save_playlist(session)
|
||||||
|
|
||||||
def _drop_on(self, event):
|
def _drop_on(self, event):
|
||||||
"""
|
"""
|
||||||
https://stackoverflow.com/questions/26227885/drag-and-drop-rows-within-qtablewidget
|
https://stackoverflow.com/questions/26227885/drag-and-drop-rows-within-qtablewidget
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user