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 logging
|
||||
import pendulum
|
||||
import re
|
||||
|
||||
import mutagen._util
|
||||
import mutagen.oggtheora
|
||||
|
@ -12,6 +11,8 @@ import mutagen.flac
|
|||
from rest_framework import serializers
|
||||
from rest_framework.compat import Mapping
|
||||
|
||||
from funkwhale_api.tags import models as tags_models
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
NODEFAULT = object()
|
||||
# default title used when imported tracks miss the `Album` tag, see #122
|
||||
|
@ -491,9 +492,6 @@ class PermissiveDateField(serializers.CharField):
|
|||
return None
|
||||
|
||||
|
||||
TAG_REGEX = re.compile(r"^((\w+)([\d_]*))$")
|
||||
|
||||
|
||||
def extract_tags_from_genre(string):
|
||||
tags = []
|
||||
delimiter = "@@@@@"
|
||||
|
@ -511,7 +509,7 @@ def extract_tags_from_genre(string):
|
|||
if not tag:
|
||||
continue
|
||||
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
|
||||
# we simply skip such tags
|
||||
continue
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import re
|
||||
|
||||
from django.contrib.contenttypes.fields import GenericForeignKey
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.contrib.postgres.fields import CICharField
|
||||
|
@ -8,6 +10,9 @@ from django.utils import timezone
|
|||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
|
||||
TAG_REGEX = re.compile(r"^((\w+)([\d_]*))$")
|
||||
|
||||
|
||||
class Tag(models.Model):
|
||||
name = CICharField(max_length=100, unique=True)
|
||||
creation_date = models.DateTimeField(default=timezone.now)
|
||||
|
|
|
@ -7,3 +7,11 @@ class TagSerializer(serializers.ModelSerializer):
|
|||
class Meta:
|
||||
model = models.Tag
|
||||
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
|
||||
|
||||
|
||||
|
@ -12,3 +14,18 @@ def test_tag_serializer(factories):
|
|||
}
|
||||
|
||||
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