forms.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import hashlib
  2. from django import forms
  3. from django.core.exceptions import ValidationError
  4. from django.utils.translation import ugettext_lazy as _
  5. from misago.forms import Form
  6. from misago.security import captcha
  7. from misago.users.models import User
  8. from misago.users.validators import validate_password, validate_email
  9. class UserRegisterForm(Form):
  10. username = forms.CharField(max_length=15,help_text=_("Between 3 and 15 characters, only letters and digits are allowed."))
  11. email = forms.EmailField(max_length=255,help_text=_("Working e-mail inbox is required to maintain control over your forum account."))
  12. email_rep = forms.EmailField(max_length=255)
  13. password = forms.CharField(max_length=255,help_text=_("Password you will be using to sign in to your account. Make sure it's strong."))
  14. password_rep = forms.CharField(max_length=255)
  15. captcha_qa = captcha.QACaptchaField()
  16. recaptcha = captcha.ReCaptchaField()
  17. accept_tos = forms.BooleanField(required=True,label=_("Forum Terms of Service"),error_messages={'required': _("Acceptation of board ToS is mandatory for membership.")})
  18. validate_repeats = (('email', 'email_rep'), ('password', 'password_rep'))
  19. repeats_errors = [{
  20. 'different': _("Entered addresses do not match."),
  21. },
  22. {
  23. 'different': _("Entered passwords do not match."),
  24. }]
  25. layout = [
  26. (
  27. None,
  28. [('username', {'attrs': {'placeholder': _("Enter your desired username")}})]
  29. ),
  30. (
  31. None,
  32. [('nested', [('email', {'label': _('E-mail address'), 'attrs': {'placeholder': _("Enter your e-mail")}, 'width': 50}), ('email_rep', {'attrs': {'placeholder': _("Repeat your e-mail")}, 'width': 50})]),
  33. ('nested', [('password', {'label': _('Password'), 'has_value': False, 'attrs': {'placeholder': _("Enter your password")}, 'width': 50}), ('password_rep', {'has_value': False, 'attrs': {'placeholder': _("Repeat your password")}, 'width': 50})])]
  34. ),
  35. (
  36. None,
  37. ['captcha_qa', 'recaptcha']
  38. ),
  39. (
  40. None,
  41. [('accept_tos', {'inline': _("I have read and accept this forums Terms of Service.")})]
  42. ),
  43. ]
  44. class Meta:
  45. widgets = {
  46. 'password': forms.PasswordInput(),
  47. 'password_rep': forms.PasswordInput(),
  48. }
  49. def clean_username(self):
  50. new_user = User.objects.get_blank_user()
  51. new_user.set_username(self.cleaned_data['username'])
  52. try:
  53. new_user.full_clean()
  54. except ValidationError as e:
  55. new_user.is_username_valid(e)
  56. return self.cleaned_data['username']
  57. def clean_email(self):
  58. new_user = User.objects.get_blank_user()
  59. new_user.set_email(self.cleaned_data['email'])
  60. try:
  61. new_user.full_clean()
  62. except ValidationError as e:
  63. new_user.is_email_valid(e)
  64. return self.cleaned_data['email']
  65. def clean_password(self):
  66. new_user = User.objects.get_blank_user()
  67. new_user.set_password(self.cleaned_data['password'])
  68. try:
  69. new_user.full_clean()
  70. except ValidationError as e:
  71. new_user.is_password_valid(e)
  72. validate_password(self.cleaned_data['password'])
  73. return self.cleaned_data['password']
  74. class UserSendSpecialMailForm(Form):
  75. email = forms.EmailField(max_length=255,label=_("Your E-mail Address"),help_text=_("Your account's email address."))
  76. captcha_qa = captcha.QACaptchaField()
  77. recaptcha = captcha.ReCaptchaField()
  78. error_source = 'email'
  79. layout = [
  80. (
  81. None,
  82. [('email', {'placeholder': _("Enter your e-mail address.")})]
  83. ),
  84. (
  85. None,
  86. ['captcha_qa', 'recaptcha']
  87. ),
  88. ]
  89. def clean_email(self):
  90. try:
  91. email = self.cleaned_data['email'].lower()
  92. email_hash = hashlib.md5(email).hexdigest()
  93. self.found_user = User.objects.get(email_hash=email_hash)
  94. except User.DoesNotExist:
  95. raise ValidationError(_("There is no user with such e-mail address."))
  96. return email