Save of new style playlist implemented but not tested

This commit is contained in:
Keith Edmunds 2022-08-06 21:17:11 +01:00
parent 7a14651bd7
commit 32e81fb074
2 changed files with 60 additions and 3 deletions

View File

@ -43,7 +43,7 @@ from sqlalchemy.orm.exc import (
# leading_silence,
# trailing_silence,
# )
# from log import log.debug, log.error
from log import log
#
Base = declarative_base()
@ -442,7 +442,6 @@ class PlaylistRows(Base):
f"note={self.note} row_number={self.row_number}>"
)
# def __init__(
# self, session: Session, playlist_id: int, track_id: int,
# row: int) -> None:
@ -453,6 +452,42 @@ class PlaylistRows(Base):
# self.row = row
# session.add(self)
# session.flush()
#
@staticmethod
def delete_higher_rows(session: Session, playlist_id: int, row: int) \
-> None:
"""
Delete rows in given playlist that have a higher row number
than 'row'
"""
# Log the rows to be deleted
rows_to_go = session.execute(
select(PlaylistRows)
.where(PlaylistRows.playlist_id == playlist_id,
PlaylistRows.row_number > row)
).scalars().all()
if not rows_to_go:
return
for row in rows_to_go:
log.debu(f"Should delete: {row}")
# If needed later:
# session.delete(row)
# @classmethod
# def get_playlist_rows(cls, playlist_id: int) -> \
# Optional[List["PlaylistRows"]]:
# """
# Return a list of PlaylistRows for passed playlist ordered by row
# """
#
# return session.execute(
# select(cls)
# .where(cls.playlist_id == playlist_id)
# .order_by(cls.row_number)
# ).scalars().all()
#
# @staticmethod
# def max_used_row(session: Session, playlist_id: int) -> Optional[int]:

View File

@ -648,6 +648,28 @@ class PlaylistTab(QTableWidget):
# KAE self.save_playlist(session)
self.update_display(session)
def save_playlist(self, session: Session) -> None:
"""
Save playlist to database
"""
# Iteratate through playlist and check that the row in each
# playlist_row object is correct
for row in range(self.rowCount()):
plr = session.get(PlaylistRows, self._get_playlistrow_id(row))
# Set the row number (even if it's already correct)
if plr.row_number != row:
log.debug(
f"Updating PlaylistRow: {plr.row_number=}, {row=}"
)
plr.row_number = row
# Any rows in the database with a row_number higher that the
# current value of 'row' should not be there. Commit session
# first to ensure any changes made above are committed.
session.commit()
PlaylistRows.delete_higher_rows(session, self.playlist_id, row)
# def save_playlist(self, session) -> None:
# """
# Save playlist to database.
@ -1009,7 +1031,7 @@ class PlaylistTab(QTableWidget):
self._set_row_colour(
row, QColor(note_colour)
)
# Notes are always bold
# Section headers are always bold
self._set_row_bold(row)
continue