From 4dcdc93958507cb670621b0fbcf0aa13907a0def Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Thu, 14 Nov 2019 15:47:18 +0100
Subject: [PATCH] Fix #966: More robust importer against malformed dates

---
 api/funkwhale_api/music/metadata.py | 2 +-
 api/tests/music/test_metadata.py    | 1 +
 changes/changelog.d/966.bugfix      | 1 +
 3 files changed, 3 insertions(+), 1 deletion(-)
 create mode 100644 changes/changelog.d/966.bugfix

diff --git a/api/funkwhale_api/music/metadata.py b/api/funkwhale_api/music/metadata.py
index d22f637fd1..9b3b6a387c 100644
--- a/api/funkwhale_api/music/metadata.py
+++ b/api/funkwhale_api/music/metadata.py
@@ -556,7 +556,7 @@ class PermissiveDateField(serializers.CharField):
         try:
             parsed = pendulum.parse(str(value))
             return datetime.date(parsed.year, parsed.month, parsed.day)
-        except pendulum.exceptions.ParserError:
+        except (pendulum.exceptions.ParserError, ValueError):
             pass
 
         return None
diff --git a/api/tests/music/test_metadata.py b/api/tests/music/test_metadata.py
index 6c9b038468..f6aa513ab9 100644
--- a/api/tests/music/test_metadata.py
+++ b/api/tests/music/test_metadata.py
@@ -239,6 +239,7 @@ def test_can_get_metadata_from_flac_file_not_crash_if_empty():
         ("2017-12-31", datetime.date(2017, 12, 31)),
         ("2017-14-01 01:32", datetime.date(2017, 1, 14)),  # deezer format
         ("2017-02", datetime.date(2017, 1, 1)),  # weird format that exists
+        ("0000", None),
         ("nonsense", None),
     ],
 )
diff --git a/changes/changelog.d/966.bugfix b/changes/changelog.d/966.bugfix
new file mode 100644
index 0000000000..c3718fcb6c
--- /dev/null
+++ b/changes/changelog.d/966.bugfix
@@ -0,0 +1 @@
+More robust importer against malformed dates (#966)
-- 
GitLab