From 1921127689d291463eedeb6f25fc8d661f6e5567 Mon Sep 17 00:00:00 2001
From: Agate <me@agate.blue>
Date: Thu, 11 Jun 2020 18:06:00 +0200
Subject: [PATCH] Fix #1158: crash when loading recent albums via Subsonic

---
 api/funkwhale_api/subsonic/views.py |  4 +---
 api/tests/subsonic/test_views.py    | 15 +++++++++++++++
 changes/changelog.d/1158.bugfix     |  1 +
 3 files changed, 17 insertions(+), 3 deletions(-)
 create mode 100644 changes/changelog.d/1158.bugfix

diff --git a/api/funkwhale_api/subsonic/views.py b/api/funkwhale_api/subsonic/views.py
index e7c619a796..4f743abc32 100644
--- a/api/funkwhale_api/subsonic/views.py
+++ b/api/funkwhale_api/subsonic/views.py
@@ -458,9 +458,7 @@ class SubsonicViewSet(viewsets.GenericViewSet):
         elif type == "alphabeticalByName" or not type:
             queryset = queryset.order_by("artist__title")
         elif type == "recent" or not type:
-            queryset = queryset.exclude(release_date__in=["", None]).order_by(
-                "-release_date"
-            )
+            queryset = queryset.exclude(release_date=None).order_by("-release_date")
         elif type == "newest" or not type:
             queryset = queryset.order_by("-creation_date")
         elif type == "byGenre" and data.get("genre"):
diff --git a/api/tests/subsonic/test_views.py b/api/tests/subsonic/test_views.py
index 3c9bae5f19..d8bc5aeebb 100644
--- a/api/tests/subsonic/test_views.py
+++ b/api/tests/subsonic/test_views.py
@@ -435,6 +435,21 @@ def test_get_album_list2(
     playable_by.assert_called_once()
 
 
+def test_get_album_list2_recent(db, logged_in_api_client, factories):
+    url = reverse("api:subsonic-get_album_list2")
+    assert url.endswith("getAlbumList2") is True
+    factories["music.Album"](playable=True, release_date=None)
+    album2 = factories["music.Album"](playable=True)
+    album3 = factories["music.Album"](playable=True)
+    response = logged_in_api_client.get(url, {"f": "json", "type": "recent"})
+
+    assert response.status_code == 200
+    expected_albums = reversed(sorted([album3, album2], key=lambda a: a.release_date))
+    assert response.data == {
+        "albumList2": {"album": serializers.get_album_list2_data(expected_albums)}
+    }
+
+
 @pytest.mark.parametrize("f", ["json"])
 def test_get_album_list2_pagination(f, db, logged_in_api_client, factories):
     url = reverse("api:subsonic-get_album_list2")
diff --git a/changes/changelog.d/1158.bugfix b/changes/changelog.d/1158.bugfix
new file mode 100644
index 0000000000..ff7fdd4074
--- /dev/null
+++ b/changes/changelog.d/1158.bugfix
@@ -0,0 +1 @@
+Fixed crash when loading recent albums via Subsonic (#1158)
-- 
GitLab