WIP V3: catch proposed duplicate playlist name

Fixes #197
This commit is contained in:
Keith Edmunds 2023-11-19 11:13:49 +00:00
parent 4f4408400f
commit 262ab202fc
2 changed files with 35 additions and 12 deletions

View File

@ -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

View File

@ -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"""