diff --git a/docs/changes.rst b/docs/changes.rst index 1f3d8484..f1df2ae4 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -24,6 +24,15 @@ v0.11.2 (UNRELEASED) - Make ``mopidy-scan`` support symlinks. +**Core API** + +- :meth:`mopidy.core.PlaylistsController.get_playlists` now accepts an argument + ``include_tracks``. This defaults to :class:`True`, which has the same old + behavior. If set to :class:`False`, the tracks are stripped from the + playlists before they are returned. This can be used to limit the amount of + data returned if the response is to be passed out of the application, e.g. to + a web client. (Fixes: :issue:`297`) + v0.11.1 (2012-12-24) ==================== diff --git a/mopidy/core/playlists.py b/mopidy/core/playlists.py index 62098c7f..f0187d44 100644 --- a/mopidy/core/playlists.py +++ b/mopidy/core/playlists.py @@ -15,11 +15,14 @@ class PlaylistsController(object): self.backends = backends self.core = core - def get_playlists(self): + def get_playlists(self, include_tracks=True): futures = [ b.playlists.playlists for b in self.backends.with_playlists] results = pykka.get_all(futures) - return list(itertools.chain(*results)) + playlists = list(itertools.chain(*results)) + if not include_tracks: + playlists = [p.copy(tracks=[]) for p in playlists] + return playlists playlists = property(get_playlists) """ diff --git a/tests/core/playlists_test.py b/tests/core/playlists_test.py index cea93c5b..f11e1776 100644 --- a/tests/core/playlists_test.py +++ b/tests/core/playlists_test.py @@ -46,6 +46,22 @@ class PlaylistsTest(unittest.TestCase): self.assertIn(self.pl2a, result) self.assertIn(self.pl2b, result) + def test_get_playlists_includes_tracks_by_default(self): + result = self.core.playlists.get_playlists() + + self.assertEqual(result[0].name, 'A') + self.assertEqual(len(result[0].tracks), 1) + self.assertEqual(result[1].name, 'B') + self.assertEqual(len(result[1].tracks), 1) + + def test_get_playlist_can_strip_tracks_from_returned_playlists(self): + result = self.core.playlists.get_playlists(include_tracks=False) + + self.assertEqual(result[0].name, 'A') + self.assertEqual(len(result[0].tracks), 0) + self.assertEqual(result[1].name, 'B') + self.assertEqual(len(result[1].tracks), 0) + def test_create_without_uri_scheme_uses_first_backend(self): playlist = Playlist() self.sp1.create().get.return_value = playlist diff --git a/tests/utils/jsonrpc_test.py b/tests/utils/jsonrpc_test.py index 59cb89b5..db53d4e4 100644 --- a/tests/utils/jsonrpc_test.py +++ b/tests/utils/jsonrpc_test.py @@ -603,7 +603,7 @@ class JsonRpcInspectorTest(JsonRpcTestBase): self.assertIn('core.playlists.get_playlists', methods) self.assertEquals( - len(methods['core.playlists.get_playlists']['params']), 0) + len(methods['core.playlists.get_playlists']['params']), 1) self.assertIn('core.tracklist.filter', methods.keys()) self.assertEquals(