From 9d9676aa1779595ffd485f706cce2938bf8f9849 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Wed, 6 Jun 2018 21:41:38 +0200
Subject: [PATCH] Fix #237: Do not crash when importing track with an artist
 that do not match the release artist

---
 api/funkwhale_api/music/models.py | 10 ++++++++--
 changes/changelog.d/237.bugfix    |  1 +
 2 files changed, 9 insertions(+), 2 deletions(-)
 create mode 100644 changes/changelog.d/237.bugfix

diff --git a/api/funkwhale_api/music/models.py b/api/funkwhale_api/music/models.py
index 0ba4d22c..3c488fca 100644
--- a/api/funkwhale_api/music/models.py
+++ b/api/funkwhale_api/music/models.py
@@ -334,6 +334,11 @@ class TrackQuerySet(models.QuerySet):
                     .prefetch_related('files'))
 
 
+def get_artist(release_list):
+    return Artist.get_or_create_from_api(
+        mbid=release_list[0]['artist-credits'][0]['artists']['id'])[0]
+
+
 class Track(APIModelMixin):
     title = models.CharField(max_length=255)
     artist = models.ForeignKey(
@@ -363,8 +368,9 @@ class Track(APIModelMixin):
             'musicbrainz_field_name': 'title'
         },
         'artist': {
-            'musicbrainz_field_name': 'artist-credit',
-            'converter': lambda v: Artist.get_or_create_from_api(mbid=v[0]['artist']['id'])[0],
+            # we use the artist from the release to avoid #237
+            'musicbrainz_field_name': 'release-list',
+            'converter': get_artist,
         },
         'album': {
             'musicbrainz_field_name': 'release-list',
diff --git a/changes/changelog.d/237.bugfix b/changes/changelog.d/237.bugfix
new file mode 100644
index 00000000..8b529f5f
--- /dev/null
+++ b/changes/changelog.d/237.bugfix
@@ -0,0 +1 @@
+Do not crash when importing track with an artist that do not match the release artist (#237)
-- 
GitLab