validators.py 4.1 KB

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