validators.py 623 B

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