From 97235f9441bea8bc94a6d41c8b96eb4f230b4524 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Tue, 14 Apr 2015 23:46:20 +0200 Subject: [PATCH] core: Don't allow TLIDs in queries, or integers Handle this in tracklist.filter() which is the only API that allows number and/or TLIDs. --- mopidy/core/tracklist.py | 11 ++++++----- mopidy/utils/validation.py | 9 ++------- tests/utils/test_validation.py | 2 +- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/mopidy/core/tracklist.py b/mopidy/core/tracklist.py index 359efa0c..54143578 100644 --- a/mopidy/core/tracklist.py +++ b/mopidy/core/tracklist.py @@ -422,16 +422,17 @@ class TracklistController(object): :rtype: list of :class:`mopidy.models.TlTrack` """ criteria = criteria or kwargs + tlids = criteria.pop('tlid', []) validation.check_query(criteria) + validation.check_instances(tlids, int) # TODO: deprecate kwargs matches = self._tl_tracks for (key, values) in criteria.items(): - if key == 'tlid': - matches = [ct for ct in matches if ct.tlid in values] - else: - matches = [ - ct for ct in matches if getattr(ct.track, key) in values] + matches = [ + ct for ct in matches if getattr(ct.track, key) in values] + if tlids: + matches = [ct for ct in matches if ct.tlid in tlids] return matches def move(self, start, end, to_position): diff --git a/mopidy/utils/validation.py b/mopidy/utils/validation.py index 25e9f227..c158f340 100644 --- a/mopidy/utils/validation.py +++ b/mopidy/utils/validation.py @@ -9,7 +9,7 @@ PLAYBACK_STATES = {'paused', 'stopped', 'playing'} QUERY_FIELDS = { 'uri', 'track_name', 'album', 'artist', 'albumartist', 'composer', - 'performer', 'track_no', 'genre', 'date', 'comment', 'any', 'tlid', 'name'} + 'performer', 'track_no', 'genre', 'date', 'comment', 'any', 'name'} DISTINCT_FIELDS = { 'artist', 'albumartist', 'album', 'composer', 'performer', 'date', 'genre'} @@ -62,9 +62,7 @@ def check_query(arg, list_values=True): # TODO: normalize name -> track_name # TODO: normalize value -> [value] # TODO: normalize blank -> [] or just remove field? - # TODO: normalize int -> str or remove int support? # TODO: remove list_values? - # TODO: don't allow for instance tlid field in all queries? if not isinstance(arg, collections.Mapping): raise exceptions.ValidationError( @@ -83,10 +81,7 @@ def check_query(arg, list_values=True): def _check_query_value(key, arg, msg): - if isinstance(arg, compat.string_types): - if not arg.strip(): - raise exceptions.ValidationError(msg.format(arg=arg, key=key)) - elif not isinstance(arg, (int, long)): + if not isinstance(arg, compat.string_types) or not arg.strip(): raise exceptions.ValidationError(msg.format(arg=arg, key=key)) diff --git a/tests/utils/test_validation.py b/tests/utils/test_validation.py index fdebd4d2..d9413686 100644 --- a/tests/utils/test_validation.py +++ b/tests/utils/test_validation.py @@ -98,7 +98,7 @@ def test_check_mapping_error_message(): def test_check_query_invalid_fields(): - for value in 'wrong', 'bar', 'foo': + for value in 'wrong', 'bar', 'foo', 'tlid': with raises(exceptions.ValidationError): validation.check_query({value: []})