Skip to content

Commit c34a7b0

Browse files
committed
save call to lookup() when creating playlist
1 parent 191f2a8 commit c34a7b0

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

mopidy_spotify/playlists.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,21 @@ def refresh(self):
146146
self._loaded = True
147147

148148
def create(self, name):
149-
logger.info(f"Creating Spotify playlist '{name}'")
150-
uri = web.create_playlist(self._backend._web_client, name)
151-
return self.lookup(uri) if uri else None
149+
if not name:
150+
return None
151+
web_playlist = web.create_playlist(self._backend._web_client, name)
152+
if web_playlist is None:
153+
logger.error(f"Failed to create Spotify playlist '{name}'")
154+
return
155+
logger.info(f"Created Spotify playlist '{name}'")
156+
return translator.to_playlist(
157+
web_playlist,
158+
username=self._backend._web_client.user_id,
159+
bitrate=self._backend._bitrate,
160+
# Note: we are not filtering out (currently) unplayable tracks;
161+
# otherwise they would be removed when editing the playlist.
162+
check_playable=False,
163+
)
152164

153165
def delete(self, uri):
154166
logger.info(f"Deleting Spotify playlist {uri!r}")

mopidy_spotify/web.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ def create_playlist(web_client, name, public=False):
573573
url = f"users/{web_client.user_id}/playlists"
574574
response = web_client.post(url, json={"name": name, "public": public})
575575
web_client.remove_from_cache("me/playlists")
576-
return response["uri"] if response.status_ok else None
576+
return response if response.status_ok else None
577577

578578

579579
def delete_playlist(web_client, uri):

tests/test_playlists.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from mopidy.models import Ref, Track
88

99
import spotify
10-
from mopidy_spotify import playlists
10+
from mopidy_spotify import playlists, web
1111

1212

1313
@pytest.fixture
@@ -238,8 +238,11 @@ def request(method, path, *, json=None):
238238
return mock.Mock(status_ok=rv)
239239
elif re.fullmatch(r"users/(.*?)/playlists", path):
240240
ok, playlist_id = _create_playlist(method, json)
241-
rv = mock.MagicMock(status_ok=ok)
242-
rv.__getitem__.return_value = playlist_id
241+
rv = mock.MagicMock(status_ok=ok, spec=web.WebResponse)
242+
rv.__getitem__.side_effect = web_playlists_map[
243+
playlist_id
244+
].__getitem__
245+
rv.get = web_playlists_map[playlist_id].get
243246
return rv
244247
elif re.fullmatch(r"playlists/(.*?)/followers", path):
245248
playlist_id = path_parts[1]
@@ -336,8 +339,7 @@ def test_playlist_save_failed1(provider, mopidy_track_factory, caplog):
336339
assert retval is None
337340
assert (
338341
"Cannot modify Spotify playlist 'spotify:user:bob:playlist:baz' "
339-
"owned by other user bob"
340-
in caplog.text
342+
"owned by other user bob" in caplog.text
341343
)
342344

343345

@@ -367,17 +369,13 @@ def test_playlist_save_failed2(provider, mopidy_track_factory, caplog):
367369
def test_playlist_save_failed3(provider, caplog):
368370
playlist = provider.lookup("spotify:user:alice:playlist:foo")
369371
tracks = list(playlist.tracks)
370-
tracks.append(
371-
Track(
372-
name="non-spotify track",
373-
uri="some:other:uri",
374-
)
375-
)
372+
tracks.append(Track(name="non-spotify track", uri="some:other:uri"))
376373
new_pl = playlist.replace(tracks=tracks)
377374
retval = provider.save(new_pl)
378375
assert retval is playlist
379376
assert (
380-
"Skipping adding non-Spotify tracks to Spotify playlist 'spotify:user:alice:playlist:foo'"
377+
"Skipping adding non-Spotify tracks to Spotify playlist "
378+
"'spotify:user:alice:playlist:foo'"
381379
in caplog.text
382380
)
383381

0 commit comments

Comments
 (0)