See #432: Moved tag validation in the tags app
This commit is contained in:
parent
997dcf5327
commit
bd271c8ead
|
@ -2,7 +2,6 @@ import base64
|
||||||
import datetime
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
import pendulum
|
import pendulum
|
||||||
import re
|
|
||||||
|
|
||||||
import mutagen._util
|
import mutagen._util
|
||||||
import mutagen.oggtheora
|
import mutagen.oggtheora
|
||||||
|
@ -12,6 +11,8 @@ import mutagen.flac
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from rest_framework.compat import Mapping
|
from rest_framework.compat import Mapping
|
||||||
|
|
||||||
|
from funkwhale_api.tags import models as tags_models
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
NODEFAULT = object()
|
NODEFAULT = object()
|
||||||
# default title used when imported tracks miss the `Album` tag, see #122
|
# default title used when imported tracks miss the `Album` tag, see #122
|
||||||
|
@ -491,9 +492,6 @@ class PermissiveDateField(serializers.CharField):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
TAG_REGEX = re.compile(r"^((\w+)([\d_]*))$")
|
|
||||||
|
|
||||||
|
|
||||||
def extract_tags_from_genre(string):
|
def extract_tags_from_genre(string):
|
||||||
tags = []
|
tags = []
|
||||||
delimiter = "@@@@@"
|
delimiter = "@@@@@"
|
||||||
|
@ -511,7 +509,7 @@ def extract_tags_from_genre(string):
|
||||||
if not tag:
|
if not tag:
|
||||||
continue
|
continue
|
||||||
final_tag = ""
|
final_tag = ""
|
||||||
if not TAG_REGEX.match(tag.replace(" ", "")):
|
if not tags_models.TAG_REGEX.match(tag.replace(" ", "")):
|
||||||
# the string contains some non words chars ($, €, etc.), right now
|
# the string contains some non words chars ($, €, etc.), right now
|
||||||
# we simply skip such tags
|
# we simply skip such tags
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import re
|
||||||
|
|
||||||
from django.contrib.contenttypes.fields import GenericForeignKey
|
from django.contrib.contenttypes.fields import GenericForeignKey
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.contrib.postgres.fields import CICharField
|
from django.contrib.postgres.fields import CICharField
|
||||||
|
@ -8,6 +10,9 @@ from django.utils import timezone
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
|
||||||
|
TAG_REGEX = re.compile(r"^((\w+)([\d_]*))$")
|
||||||
|
|
||||||
|
|
||||||
class Tag(models.Model):
|
class Tag(models.Model):
|
||||||
name = CICharField(max_length=100, unique=True)
|
name = CICharField(max_length=100, unique=True)
|
||||||
creation_date = models.DateTimeField(default=timezone.now)
|
creation_date = models.DateTimeField(default=timezone.now)
|
||||||
|
|
|
@ -7,3 +7,11 @@ class TagSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Tag
|
model = models.Tag
|
||||||
fields = ["name", "creation_date"]
|
fields = ["name", "creation_date"]
|
||||||
|
|
||||||
|
|
||||||
|
class TagNameField(serializers.CharField):
|
||||||
|
def to_internal_value(self, value):
|
||||||
|
value = super().to_internal_value(value)
|
||||||
|
if not models.TAG_REGEX.match(value):
|
||||||
|
raise serializers.ValidationError('Invalid tag "{}"'.format(value))
|
||||||
|
return value
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import pytest
|
||||||
|
|
||||||
from funkwhale_api.tags import serializers
|
from funkwhale_api.tags import serializers
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,3 +14,18 @@ def test_tag_serializer(factories):
|
||||||
}
|
}
|
||||||
|
|
||||||
assert serializer.data == expected
|
assert serializer.data == expected
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"name",
|
||||||
|
[
|
||||||
|
"",
|
||||||
|
"invalid because spaces",
|
||||||
|
"invalid-because-dashes",
|
||||||
|
"invalid because non breaking spaces",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_tag_name_field_validation(name):
|
||||||
|
field = serializers.TagNameField()
|
||||||
|
with pytest.raises(serializers.serializers.ValidationError):
|
||||||
|
field.to_internal_value(name)
|
||||||
|
|
Loading…
Reference in New Issue