forms.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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)
  11. email = forms.EmailField(max_length=255)
  12. email_rep = forms.EmailField(max_length=255)
  13. password = forms.CharField(max_length=255,widget=forms.PasswordInput)
  14. password_rep = forms.CharField(max_length=255,widget=forms.PasswordInput)
  15. captcha_qa = captcha.QACaptchaField()
  16. recaptcha = captcha.ReCaptchaField()
  17. accept_tos = forms.BooleanField(required=True,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', {'label': _('Username'), 'help_text': _("Your displayed username. Between 3 and 15 characters, only letters and digits are allowed."),'attrs': {'placeholder': _("Enter your desired username")}})]
  29. ),
  30. (
  31. None,
  32. [('nested', [('email', {'label': _('E-mail address'), 'help_text': _("Working e-mail inbox is required to maintain control over your forum account."), 'attrs': {'placeholder': _("Enter your e-mail")}, 'width': 50}), ('email_rep', {'attrs': {'placeholder': _("Repeat your e-mail")}, 'width': 50})]),
  33. ('nested', [('password', {'label': _('Password'), 'help_text': _("Password you will be using to sign in to your account. Make sure it's strong."), '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', {'label': _("Forum Terms of Service"), 'inline': _("I have read and accept this forums Terms of Service.")})]
  42. ),
  43. ]
  44. def clean_username(self):
  45. new_user = User.objects.get_blank_user()
  46. new_user.set_username(self.cleaned_data['username'])
  47. try:
  48. new_user.full_clean()
  49. except ValidationError as e:
  50. new_user.is_username_valid(e)
  51. return self.cleaned_data['username']
  52. def clean_email(self):
  53. new_user = User.objects.get_blank_user()
  54. new_user.set_email(self.cleaned_data['email'])
  55. try:
  56. new_user.full_clean()
  57. except ValidationError as e:
  58. new_user.is_email_valid(e)
  59. return self.cleaned_data['email']
  60. def clean_password(self):
  61. new_user = User.objects.get_blank_user()
  62. new_user.set_password(self.cleaned_data['password'])
  63. try:
  64. new_user.full_clean()
  65. except ValidationError as e:
  66. new_user.is_password_valid(e)
  67. validate_password(self.cleaned_data['password'])
  68. return self.cleaned_data['password']
  69. class UserSendSpecialMailForm(Form):
  70. email = forms.EmailField(max_length=255,label=_("Your E-mail Address"),help_text=_("Your account's email address."))
  71. captcha_qa = captcha.QACaptchaField()
  72. recaptcha = captcha.ReCaptchaField()
  73. error_source = 'email'
  74. layout = [
  75. (
  76. None,
  77. [('email', {'placeholder': _("Enter your e-mail address.")})]
  78. ),
  79. (
  80. None,
  81. ['captcha_qa', 'recaptcha']
  82. ),
  83. ]
  84. def clean_email(self):
  85. try:
  86. email = self.cleaned_data['email'].lower()
  87. email_hash = hashlib.md5(email).hexdigest()
  88. self.found_user = User.objects.get(email_hash=email_hash)
  89. except User.DoesNotExist:
  90. raise ValidationError(_("There is no user with such e-mail address."))
  91. return email
  92. class QuickFindUserForm(Form):
  93. username = forms.CharField()