From 7c47348855115b49a4842a67aebd5d4067b49936 Mon Sep 17 00:00:00 2001 From: Eliot Berriot Date: Tue, 5 Jun 2018 19:44:00 +0200 Subject: [PATCH] Fix #267: Do not crash when tag contains multiple uuids with a / separator --- api/funkwhale_api/music/metadata.py | 19 ++++++++++++++++--- api/tests/music/test_metadata.py | 14 ++++++++++++++ changes/changelog.d/267.bugfix | 1 + 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 changes/changelog.d/267.bugfix diff --git a/api/funkwhale_api/music/metadata.py b/api/funkwhale_api/music/metadata.py index 3637b1c8c..4c17c42c0 100644 --- a/api/funkwhale_api/music/metadata.py +++ b/api/funkwhale_api/music/metadata.py @@ -91,10 +91,23 @@ def convert_track_number(v): pass + +class FirstUUIDField(forms.UUIDField): + def to_python(self, value): + try: + # sometimes, Picard leaves to uuids in the field, separated + # by a slash + value = value.split('/')[0] + except (AttributeError, IndexError, TypeError): + pass + + return super().to_python(value) + + VALIDATION = { - 'musicbrainz_artistid': forms.UUIDField(), - 'musicbrainz_albumid': forms.UUIDField(), - 'musicbrainz_recordingid': forms.UUIDField(), + 'musicbrainz_artistid': FirstUUIDField(), + 'musicbrainz_albumid': FirstUUIDField(), + 'musicbrainz_recordingid': FirstUUIDField(), } CONF = { diff --git a/api/tests/music/test_metadata.py b/api/tests/music/test_metadata.py index 326f18324..a4f15b335 100644 --- a/api/tests/music/test_metadata.py +++ b/api/tests/music/test_metadata.py @@ -95,3 +95,17 @@ def test_can_get_metadata_from_flac_file_not_crash_if_empty(): with pytest.raises(metadata.TagNotFound): data.get('test') + + +@pytest.mark.parametrize('field_name', [ + 'musicbrainz_artistid', + 'musicbrainz_albumid', + 'musicbrainz_recordingid', +]) +def test_mbid_clean_keeps_only_first(field_name): + u1 = str(uuid.uuid4()) + u2 = str(uuid.uuid4()) + field = metadata.VALIDATION[field_name] + result = field.to_python('/'.join([u1, u2])) + + assert str(result) == u1 diff --git a/changes/changelog.d/267.bugfix b/changes/changelog.d/267.bugfix new file mode 100644 index 000000000..8d0bfa5c5 --- /dev/null +++ b/changes/changelog.d/267.bugfix @@ -0,0 +1 @@ +Do not crash when tag contains multiple uuids with a / separator (#267)