validators.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import re
  2. from django.core.exceptions import ValidationError
  3. from django.utils.translation import ungettext, ugettext_lazy as _
  4. from misago.conf import settings
  5. from misago.models import Ban
  6. from misago.utils.strings import slugify
  7. class validate_sluggable(object):
  8. def __init__(self, error_short=None, error_long=None):
  9. self.error_short = error_short if error_short else _("Value has to contain alpha-numerical characters.")
  10. self.error_long = error_long if error_long else _("Value is too long.")
  11. def __call__(self, value):
  12. slug = slugify(value)
  13. if not slug:
  14. raise ValidationError(self.error_short)
  15. if len(slug) > 255:
  16. raise ValidationError(self.error_long)
  17. def validate_username(value):
  18. value = unicode(value).strip()
  19. if len(value) < settings.username_length_min:
  20. raise ValidationError(ungettext(
  21. 'Username must be at least one character long.',
  22. 'Username must be at least %(count)d characters long.',
  23. settings.username_length_min
  24. ) % {
  25. 'count': settings.username_length_min,
  26. })
  27. if len(value) > settings.username_length_max:
  28. raise ValidationError(ungettext(
  29. 'Username cannot be longer than one characters.',
  30. 'Username cannot be longer than %(count)d characters.',
  31. settings.username_length_max
  32. ) % {
  33. 'count': settings.username_length_max,
  34. })
  35. if settings.UNICODE_USERNAMES:
  36. if not re.search('^[^\W_]+$', value, re.UNICODE):
  37. raise ValidationError(_("Username can only contain letters and digits."))
  38. else:
  39. if not re.search('^[^\W_]+$', value):
  40. raise ValidationError(_("Username can only contain latin alphabet letters and digits."))
  41. if Ban.objects.check_ban(username=value):
  42. raise ValidationError(_("This username is forbidden."))
  43. def validate_password(value):
  44. value = unicode(value).strip()
  45. if len(value) < settings.password_length:
  46. raise ValidationError(ungettext(
  47. 'Correct password has to be at least one character long.',
  48. 'Correct password has to be at least %(count)d characters long.',
  49. settings.password_length
  50. ) % {
  51. 'count': settings.password_length,
  52. })
  53. for test in settings.password_complexity:
  54. if test in ('case', 'digits', 'special'):
  55. if not re.search('[a-zA-Z]', value):
  56. raise ValidationError(_("Password must contain alphabetical characters."))
  57. if test == 'case':
  58. if not (re.search('[a-z]', value) and re.search('[A-Z]', value)):
  59. raise ValidationError(_("Password must contain characters that have different case."))
  60. if test == 'digits':
  61. if not re.search('[0-9]', value):
  62. raise ValidationError(_("Password must contain digits in addition to characters."))
  63. if test == 'special':
  64. if not re.search('[^0-9a-zA-Z]', value):
  65. raise ValidationError(_("Password must contain special (non alphanumerical) characters."))
  66. def validate_email(value):
  67. value = unicode(value).strip()
  68. if Ban.objects.check_ban(email=value):
  69. raise ValidationError(_("This board forbids registrations using this e-mail address."))