validators.py 896 B

12345678910111213141516171819202122232425262728
  1. import re
  2. from django.forms import ValidationError
  3. from django.utils.translation import gettext as _
  4. FILENAME_CONTENT = re.compile("([a-zA-Z0-9]|\.|_|-)+")
  5. FILENAME_TANGIBILITY = re.compile("[a-zA-Z0-9]")
  6. def validate_css_name(filename):
  7. if not filename.lower().endswith(".css"):
  8. raise ValidationError(_("Name is missing an .css extension."))
  9. if filename.startswith("."):
  10. raise ValidationError(_('Name can\'t start with period (".").'))
  11. if not FILENAME_CONTENT.fullmatch(filename[:-4]):
  12. raise ValidationError(
  13. _(
  14. "Name can contain only latin alphabet characters, "
  15. "digits, dots, underscores and dashes."
  16. )
  17. )
  18. if not FILENAME_TANGIBILITY.match(filename[:-4]):
  19. raise ValidationError(
  20. _("Name has to contain at least one latin alphabet character or digit.")
  21. )