validators.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import re
  2. from django.core.exceptions import ValidationError
  3. from django.core.validators import validate_email as validate_email_content
  4. from django.utils.translation import ungettext, ugettext_lazy as _
  5. from django.contrib.auth import get_user_model
  6. from misago.conf import settings
  7. ALPHANUMERICS_RE = re.compile('[\W_]+', re.UNICODE)
  8. USERNAME_RE = re.compile(r'^[0-9a-z]+$', re.IGNORECASE)
  9. def validate_email_available(value):
  10. User = get_user_model()
  11. try:
  12. User.objects.get_by_email(value)
  13. except User.DoesNotExist:
  14. pass
  15. else:
  16. raise ValidationError(_("This e-mail address is not available."))
  17. def validate_email_banned(value):
  18. """TODO for when bans will be reimplemented from 0.5"""
  19. def validate_email(value):
  20. """shortcut function that does complete validation of email"""
  21. validate_email_content(value)
  22. validate_email_available(value)
  23. validate_email_banned(value)
  24. def _validate_password_alphanumerics(value):
  25. digits_len = len(filter(type(value).isdigit, value))
  26. if not digits_len or digits_len == len(value):
  27. raise ValidationError(
  28. _("Password must contain digits in addition to other characters."))
  29. def _validate_password_case(value):
  30. for char in value:
  31. if char != char.lower():
  32. break
  33. else:
  34. raise ValidationError(
  35. _("Password must contain characters with different cases."))
  36. def _validate_password_special(value):
  37. alphanumerics_len = len(ALPHANUMERICS_RE.sub('', value))
  38. if not alphanumerics_len or alphanumerics_len == len(value):
  39. raise ValidationError(
  40. _("Password must contain special signs "
  41. "in addition to other characters."))
  42. PASSWORD_COMPLEXITY_RULES = {
  43. 'alphanumerics': _validate_password_alphanumerics,
  44. 'case': _validate_password_case,
  45. 'special': _validate_password_special,
  46. }
  47. def validate_password_complexity(value):
  48. for test in settings.password_complexity:
  49. PASSWORD_COMPLEXITY_RULES[test](value)
  50. def validate_password_length(value):
  51. if len(value) < settings.password_length_min:
  52. message = ungettext(
  53. 'Valid password must be at least one character long.',
  54. 'valid password must be at least %(length)d characters long.',
  55. settings.password_length_min)
  56. message = message % {'length': settings.password_length_min}
  57. raise ValidationError(message)
  58. def validate_password(value):
  59. """shortcut function that does complete validation of password"""
  60. validate_password_length(value)
  61. validate_password_complexity(value)
  62. def validate_username_available(value):
  63. User = get_user_model()
  64. try:
  65. User.objects.get_by_username(value)
  66. except User.DoesNotExist:
  67. pass
  68. else:
  69. raise ValidationError(_("This username is not available."))
  70. def validate_username_banned(value):
  71. """TODO for when bans will be reimplemented from 0.5"""
  72. def validate_username_content(value):
  73. if not USERNAME_RE.match(value):
  74. raise ValidationError(
  75. _("Username can only contain latin alphabet letters and digits."))
  76. def validate_username_length(value):
  77. if len(value) < settings.username_length_min:
  78. message = ungettext(
  79. 'Username must be at least one character long.',
  80. 'Username must be at least %(length)d characters long.',
  81. settings.username_length_min)
  82. message = message % {'length': settings.username_length_min}
  83. raise ValidationError(message)
  84. if len(value) > settings.username_length_max:
  85. message = ungettext(
  86. "Username cannot be longer than one characters.",
  87. "Username cannot be longer than %(length)d characters.",
  88. settings.username_length_max)
  89. message = message % {'length': settings.username_length_max}
  90. raise ValidationError(message)
  91. def validate_username(value):
  92. """shortcut function that does complete validation of username"""
  93. validate_username_content(value)
  94. validate_username_length(value)
  95. validate_username_available(value)
  96. validate_username_banned(value)