validators.py 3.6 KB

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