Fix #351: Ensure we do not import artists with empty names
This commit is contained in:
parent
03eb2be2b8
commit
93cd72ff09
|
@ -3,12 +3,19 @@ def load(model, *args, **kwargs):
|
||||||
return importer.load(*args, **kwargs)
|
return importer.load(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
EXCLUDE_VALIDATION = {"Track": ["artist"]}
|
||||||
|
|
||||||
|
|
||||||
class Importer(object):
|
class Importer(object):
|
||||||
def __init__(self, model):
|
def __init__(self, model):
|
||||||
self.model = model
|
self.model = model
|
||||||
|
|
||||||
def load(self, cleaned_data, raw_data, import_hooks):
|
def load(self, cleaned_data, raw_data, import_hooks):
|
||||||
mbid = cleaned_data.pop("mbid")
|
mbid = cleaned_data.pop("mbid")
|
||||||
|
# let's validate data, just in case
|
||||||
|
instance = self.model(**cleaned_data)
|
||||||
|
exclude = EXCLUDE_VALIDATION.get(self.model.__name__, [])
|
||||||
|
instance.full_clean(exclude=["mbid", "uuid"] + exclude)
|
||||||
m = self.model.objects.update_or_create(mbid=mbid, defaults=cleaned_data)[0]
|
m = self.model.objects.update_or_create(mbid=mbid, defaults=cleaned_data)[0]
|
||||||
for hook in import_hooks:
|
for hook in import_hooks:
|
||||||
hook(m, cleaned_data, raw_data)
|
hook(m, cleaned_data, raw_data)
|
||||||
|
|
|
@ -1,10 +1,15 @@
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import pytest
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
from django import forms
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
from funkwhale_api.federation import actors
|
from funkwhale_api.federation import actors
|
||||||
from funkwhale_api.federation import serializers as federation_serializers
|
from funkwhale_api.federation import serializers as federation_serializers
|
||||||
|
from funkwhale_api.music import importers
|
||||||
|
from funkwhale_api.music import models
|
||||||
from funkwhale_api.music import tasks
|
from funkwhale_api.music import tasks
|
||||||
|
|
||||||
DATA_DIR = os.path.dirname(os.path.abspath(__file__))
|
DATA_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
@ -237,3 +242,9 @@ def test__do_import_in_place_mbid(factories, tmpfile):
|
||||||
assert bool(tf.audio_file) is False
|
assert bool(tf.audio_file) is False
|
||||||
assert tf.source == "file://{}".format(path)
|
assert tf.source == "file://{}".format(path)
|
||||||
assert tf.mimetype == "audio/ogg"
|
assert tf.mimetype == "audio/ogg"
|
||||||
|
|
||||||
|
|
||||||
|
def test_importer_cleans():
|
||||||
|
importer = importers.Importer(models.Artist)
|
||||||
|
with pytest.raises(forms.ValidationError):
|
||||||
|
importer.load({"name": "", "mbid": uuid.uuid4()}, {}, [])
|
||||||
|
|
|
@ -126,4 +126,4 @@ def test_can_filter_closed_invitations(factories):
|
||||||
used = factories["users.User"](invited=True).invitation
|
used = factories["users.User"](invited=True).invitation
|
||||||
|
|
||||||
assert models.Invitation.objects.count() == 3
|
assert models.Invitation.objects.count() == 3
|
||||||
assert list(models.Invitation.objects.open(False)) == [expired, used]
|
assert list(models.Invitation.objects.order_by("id").open(False)) == [expired, used]
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Ensure we do not import artists with empty names (#351)
|
Loading…
Reference in New Issue