validators.py 636 B

123456789101112131415161718192021
  1. from django.core.exceptions import ValidationError
  2. from django.utils.translation import gettext_lazy as _
  3. from .utils import slugify
  4. class validate_sluggable:
  5. def __init__(self, error_short=None, error_long=None):
  6. self.error_short = error_short or _(
  7. "Value has to contain alpha-numerical characters."
  8. )
  9. self.error_long = error_long or _("Value is too long.")
  10. def __call__(self, value):
  11. slug = slugify(value)
  12. if not slug.replace("-", ""):
  13. raise ValidationError(self.error_short)
  14. if len(slug) > 255:
  15. raise ValidationError(self.error_long)