2009 allow special char in tags

This commit is contained in:
petitminion 2024-07-02 15:09:22 +00:00
parent cf32e16547
commit 615ebde282
3 changed files with 9 additions and 33 deletions
api
funkwhale_api/music
tests/music
changes/changelog.d

View File

@ -10,8 +10,6 @@ import mutagen.oggtheora
import mutagen.oggvorbis
from rest_framework import serializers
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
@ -618,38 +616,15 @@ class PermissiveDateField(serializers.CharField):
def extract_tags_from_genre(string):
tags = []
delimiter = "@@@@@"
for d in [" - ", ",", ";", "/"]:
for d in [" - ", ", ", ",", "; ", ";", "/"]:
# Replace common tags separators by a custom delimiter
string = string.replace(d, delimiter)
# loop on the parts (splitting on our custom delimiter)
for tag in string.split(delimiter):
tag = tag.strip()
for d in ["-"]:
# preparation for replacement so that Pop-Rock becomes Pop Rock, then PopRock
# (step 1, step 2 happens below)
tag = tag.replace(d, " ")
if not tag:
continue
final_tag = ""
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
# concatenate the parts and uppercase them so that 'pop rock' becomes 'PopRock'
if len(tag.split(" ")) == 1:
# we append the tag "as is", because it doesn't contain any space
tags.append(tag)
continue
for part in tag.split(" "):
# the tag contains space, there's work to do to have consistent case
# 'pop rock' -> 'PopRock'
# (step 2)
if not part:
continue
final_tag += part[0].upper() + part[1:]
if final_tag:
tags.append(final_tag)
tags.append(tag)
return tags

View File

@ -743,13 +743,13 @@ def test_artist_field_featuring():
[
("Pop", ["Pop"]),
("pop", ["pop"]),
("Pop-Rock", ["PopRock"]),
("Pop - Rock", ["Pop", "Rock"]),
("Soundtrack - Cute Anime", ["Soundtrack", "CuteAnime"]),
("Soundtrack - Cute Anime", ["Soundtrack", "Cute Anime"]),
("Pop, Rock", ["Pop", "Rock"]),
("Chanson française", ["ChansonFrançaise"]),
("Unhandled❤", []),
("tagwithnon-breakingspaces", []),
("Chanson française", ["Chanson française"]),
(
"tagwithnon-breakingspaces",
["tag\u202fwith\u202fnon-breaking\u202fspaces"],
),
],
)
def test_acquire_tags_from_genre(genre, expected_tags):

View File

@ -0,0 +1 @@
Allow special characters in tags (#2009)