upload: Fix docstring and regex in sanitize_name regarding underscore.

Underscore character is already covered by \w, so _ in the regex is
redundant. Also the docstring is mildly incorrect - underscore already
is an allowed character by django's slugify (and always was) for the
aforementioned reason.
This commit is contained in:
Mateusz Mandera
2021-05-02 14:42:23 +02:00
committed by Tim Abbott
parent 1d9fb4f988
commit 389c7bdb5a

View File

@@ -87,11 +87,11 @@ def sanitize_name(value: str) -> str:
This implementation is based on django.utils.text.slugify; it is
modified by:
* adding '.' and '_' to the list of allowed characters.
* adding '.' to the list of allowed characters.
* preserving the case of the value.
"""
value = unicodedata.normalize("NFKC", value)
value = re.sub(r"[^\w\s._-]", "", value, flags=re.U).strip()
value = re.sub(r"[^\w\s.-]", "", value, flags=re.U).strip()
value = re.sub(r"[-\s]+", "-", value, flags=re.U)
assert value not in {"", ".", ".."}
return mark_safe(value)