From 262ab202fcc5394de0cf6760e42ff2ec001fe97c Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Sun, 19 Nov 2023 11:13:49 +0000 Subject: [PATCH] WIP V3: catch proposed duplicate playlist name Fixes #197 --- app/models.py | 11 +++++++++++ app/musicmuster.py | 36 ++++++++++++++++++++++++------------ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/app/models.py b/app/models.py index 229eeb8..d30b35c 100644 --- a/app/models.py +++ b/app/models.py @@ -315,6 +315,17 @@ class Playlists(Base): self.open = True + @staticmethod + def name_is_available(session: scoped_session, name: str) -> bool: + """ + Return True if no playlist of this name exists else false. + """ + + return session.execute( + select(Playlists) + .where(Playlists.name == name) + ).first() is None + def rename(self, session: scoped_session, new_name: str) -> None: """ Rename playlist diff --git a/app/musicmuster.py b/app/musicmuster.py index b2f7309..064f09d 100755 --- a/app/musicmuster.py +++ b/app/musicmuster.py @@ -561,7 +561,7 @@ class Window(QMainWindow, Ui_MainWindow): ) -> Optional[Playlists]: """Create new playlist""" - playlist_name = self.solicit_playlist_name() + playlist_name = self.solicit_playlist_name(session) if not playlist_name: return None playlist = Playlists(session, playlist_name) @@ -997,7 +997,7 @@ class Window(QMainWindow, Ui_MainWindow): dlg.exec() template = dlg.playlist if template: - playlist_name = self.solicit_playlist_name() + playlist_name = self.solicit_playlist_name(session) if not playlist_name: return playlist = Playlists.create_playlist_from_template( @@ -1364,20 +1364,32 @@ class Window(QMainWindow, Ui_MainWindow): # self.tabPlaylist.setCurrentWidget(self.next_track.playlist_tab) # self.tabPlaylist.currentWidget().scroll_next_to_top() - def solicit_playlist_name(self, default: Optional[str] = "") -> Optional[str]: - """Get name of playlist from user""" + def solicit_playlist_name( + self, session: scoped_session, default: str = "" + ) -> Optional[str]: + """Get name of new playlist from user""" dlg = QInputDialog(self) dlg.setInputMode(QInputDialog.InputMode.TextInput) dlg.setLabelText("Playlist name:") - if default: - dlg.setTextValue(default) - dlg.resize(500, 100) - ok = dlg.exec() - if ok: - return dlg.textValue() - else: - return None + while True: + if default: + dlg.setTextValue(default) + dlg.resize(500, 100) + ok = dlg.exec() + if ok: + proposed_name = dlg.textValue() + if Playlists.name_is_available(session, proposed_name): + return proposed_name + else: + helpers.show_warning( + self, + "Name in use", + f"There's already a playlist called '{proposed_name}'", + ) + continue + else: + return None def stop(self) -> None: """Stop playing immediately"""