subsonic: Don't crash when serialising artist with no name

If the name of an artist is not set, the serialiser will crash. Instead,
just skip such an artist when serialising a list of artists.

Also add test for serialising an artist with an empty name.
Signed-off-by: Toke Høiland-Jørgensen's avatarToke Høiland-Jørgensen <toke@toke.dk>
parent 2f46d838
...@@ -24,7 +24,8 @@ class GetArtistsSerializer(serializers.Serializer): ...@@ -24,7 +24,8 @@ class GetArtistsSerializer(serializers.Serializer):
first_letter_mapping = collections.defaultdict(list) first_letter_mapping = collections.defaultdict(list)
for artist in values: for artist in values:
first_letter_mapping[artist["name"][0].upper()].append(artist) if artist["name"]:
first_letter_mapping[artist["name"][0].upper()].append(artist)
for letter, artists in sorted(first_letter_mapping.items()): for letter, artists in sorted(first_letter_mapping.items()):
letter_data = { letter_data = {
......
...@@ -6,6 +6,7 @@ def test_get_artists_serializer(factories): ...@@ -6,6 +6,7 @@ def test_get_artists_serializer(factories):
artist1 = factories["music.Artist"](name="eliot") artist1 = factories["music.Artist"](name="eliot")
artist2 = factories["music.Artist"](name="Ellena") artist2 = factories["music.Artist"](name="Ellena")
artist3 = factories["music.Artist"](name="Rilay") artist3 = factories["music.Artist"](name="Rilay")
artist4 = factories["music.Artist"](name="") # Shouldn't be serialised
factories["music.Album"].create_batch(size=3, artist=artist1) factories["music.Album"].create_batch(size=3, artist=artist1)
factories["music.Album"].create_batch(size=2, artist=artist2) factories["music.Album"].create_batch(size=2, artist=artist2)
...@@ -28,7 +29,7 @@ def test_get_artists_serializer(factories): ...@@ -28,7 +29,7 @@ def test_get_artists_serializer(factories):
} }
queryset = artist1.__class__.objects.filter( queryset = artist1.__class__.objects.filter(
pk__in=[artist1.pk, artist2.pk, artist3.pk] pk__in=[artist1.pk, artist2.pk, artist3.pk, artist4.pk]
) )
assert serializers.GetArtistsSerializer(queryset).data == expected assert serializers.GetArtistsSerializer(queryset).data == expected
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment