From b34e0a014ac97ca08aaa7be292211e5e82781171 Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Sun, 13 Apr 2025 19:48:29 +0100 Subject: [PATCH] WIP: more database updates; all tests run --- app/ds.py | 37 +++++++++++++++++++++---------------- app/musicmuster.py | 2 +- app/playlistmodel.py | 17 ++++++++++++----- app/playlistrow.py | 27 ++------------------------- 4 files changed, 36 insertions(+), 47 deletions(-) diff --git a/app/ds.py b/app/ds.py index 45ebfb3..da9e95a 100644 --- a/app/ds.py +++ b/app/ds.py @@ -434,21 +434,6 @@ def tracks_filtered(filter: Filter) -> list[TrackDTO]: return results -def track_set_intro(track_id: int, intro: int) -> None: - """ - Set track intro time - """ - - with db.Session() as session: - session.execute( - update(Tracks) - .where(Tracks.id == track_id) - .values(intro=intro) - ) - - session.commit() - - # @log_call def track_update( track_id: int, metadata: dict[str, str | int | float] @@ -918,7 +903,6 @@ def playlist_remove_rows(playlist_id: int, row_numbers: list[int]) -> None: ) # Fixup row number to remove gaps _playlist_check_playlist(session, playlist_id, fix=True) - session.commit() @@ -1029,6 +1013,27 @@ def playlistrows_by_playlist( return dto_list +def playlistrow_update_note(playlistrow_id: int, note: str) -> PlaylistRowDTO: + """ + Update the note on a playlist row + """ + + with db.Session() as session: + plr = session.get(PlaylistRows, playlistrow_id) + + if not plr: + raise ApplicationError(f"Can't retrieve Playlistrow ({playlistrow_id=})") + + plr.note = note + + session.commit() + + new_plr = playlistrow_by_id(playlistrow_id) + if not new_plr: + raise ApplicationError(f"Can't retrieve new Playlistrow ({playlistrow_id=})") + + return new_plr + # Playdates # @log_call def playdates_get_last(track_id: int, limit: int = 5) -> str: diff --git a/app/musicmuster.py b/app/musicmuster.py index 61bbed4..3e0ecb6 100755 --- a/app/musicmuster.py +++ b/app/musicmuster.py @@ -2248,7 +2248,7 @@ class Window(QMainWindow): return intro = round(self.preview_manager.get_playtime() / 100) * 100 - ds.track_set_intro(track_id, intro) + ds.track_update(track_id, dict(intro=intro)) self.preview_manager.set_intro(intro) self.current.base_model.refresh_row(row_number) roles = [ diff --git a/app/playlistmodel.py b/app/playlistmodel.py index d9b3524..4653bc4 100644 --- a/app/playlistmodel.py +++ b/app/playlistmodel.py @@ -1303,14 +1303,21 @@ class PlaylistModel(QAbstractTableModel): column = index.column() plr = self.playlist_rows[row_number] + if column == Col.NOTE.value: + ds.playlistrow_update_note(plr.playlistrow_id, str(value)) + return True + + track_id = plr.track_id + if not track_id: + raise ApplicationError(f"No track_id when editing {plr=}") + if column == Col.TITLE.value: - plr.title = str(value) + ds.track_update(track_id, dict(title=str(value))) elif column == Col.ARTIST.value: - plr.artist = str(value) + ds.track_update(track_id, dict(artist=str(value))) elif column == Col.INTRO.value: - plr.intro = int(round(float(value), 1) * 1000) - elif column == Col.NOTE.value: - plr.note = str(value) + intro = int(round(float(value), 1) * 1000) + ds.track_update(track_id, dict(intro=intro)) else: raise ApplicationError(f"setData called with unexpected column ({column=})") diff --git a/app/playlistrow.py b/app/playlistrow.py index 0928b88..b813efb 100644 --- a/app/playlistrow.py +++ b/app/playlistrow.py @@ -70,10 +70,6 @@ class PlaylistRow: else: return "" - @artist.setter - def artist(self, value: str) -> None: - print(f"set artist attribute for {self=}, {value=}") - @property def bitrate(self): if self.dto.track: @@ -102,10 +98,6 @@ class PlaylistRow: else: return 0 - @intro.setter - def intro(self, value: int) -> None: - print(f"set intro attribute for {self=}, {value=}") - @property def lastplayed(self): if self.dto.track: @@ -141,10 +133,6 @@ class PlaylistRow: else: return "" - @title.setter - def title(self, value: str) -> None: - print(f"set title attribute for {self=}, {value=}") - @property def track_id(self): if self.dto.track: @@ -174,22 +162,10 @@ class PlaylistRow: def note(self): return self.dto.note - @note.setter - def note(self, value: str) -> None: - # TODO set up write access to db - print(f"set note attribute for {self=}, {value=}") - # self.dto.note = value - @property def played(self): return self.dto.played - @played.setter - def played(self, value: bool = True) -> None: - # TODO set up write access to db - print(f"set played attribute for {self=}") - # self.dto.played = value - @property def playlist_id(self): return self.dto.playlist_id @@ -204,7 +180,8 @@ class PlaylistRow: @row_number.setter def row_number(self, value: int) -> None: - # TODO do we need to set up write access to db? + # This does not update the database which must take place + # elsewhere self.dto.row_number = value def check_for_end_of_track(self) -> None: