forms.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.timezones import tzlist
  8. from misago.users.models import User
  9. from misago.users.validators import validate_password, validate_email
  10. class UserRegisterForm(Form):
  11. username = forms.CharField(max_length=15)
  12. email = forms.EmailField(max_length=255)
  13. email_rep = forms.EmailField(max_length=255)
  14. password = forms.CharField(max_length=255,widget=forms.PasswordInput)
  15. password_rep = forms.CharField(max_length=255,widget=forms.PasswordInput)
  16. captcha_qa = captcha.QACaptchaField()
  17. recaptcha = captcha.ReCaptchaField()
  18. accept_tos = forms.BooleanField(required=True,error_messages={'required': _("Acceptation of board ToS is mandatory for membership.")})
  19. validate_repeats = (('email', 'email_rep'), ('password', 'password_rep'))
  20. repeats_errors = [{
  21. 'different': _("Entered addresses do not match."),
  22. },
  23. {
  24. 'different': _("Entered passwords do not match."),
  25. }]
  26. layout = [
  27. (
  28. None,
  29. [('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")}})]
  30. ),
  31. (
  32. None,
  33. [('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})]),
  34. ('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})])]
  35. ),
  36. (
  37. None,
  38. ['captcha_qa', 'recaptcha']
  39. ),
  40. (
  41. None,
  42. [('accept_tos', {'label': _("Forum Terms of Service"), 'inline': _("I have read and accept this forums Terms of Service.")})]
  43. ),
  44. ]
  45. def clean_username(self):
  46. new_user = User.objects.get_blank_user()
  47. new_user.set_username(self.cleaned_data['username'])
  48. try:
  49. new_user.full_clean()
  50. except ValidationError as e:
  51. new_user.is_username_valid(e)
  52. return self.cleaned_data['username']
  53. def clean_email(self):
  54. new_user = User.objects.get_blank_user()
  55. new_user.set_email(self.cleaned_data['email'])
  56. try:
  57. new_user.full_clean()
  58. except ValidationError as e:
  59. new_user.is_email_valid(e)
  60. return self.cleaned_data['email']
  61. def clean_password(self):
  62. new_user = User.objects.get_blank_user()
  63. new_user.set_password(self.cleaned_data['password'])
  64. try:
  65. new_user.full_clean()
  66. except ValidationError as e:
  67. new_user.is_password_valid(e)
  68. validate_password(self.cleaned_data['password'])
  69. return self.cleaned_data['password']
  70. class UserSendSpecialMailForm(Form):
  71. email = forms.EmailField(max_length=255)
  72. captcha_qa = captcha.QACaptchaField()
  73. recaptcha = captcha.ReCaptchaField()
  74. error_source = 'email'
  75. layout = [
  76. (
  77. None,
  78. [('email', {'label': _("Your E-mail Address"), 'help_text': _("Enter email address you use to sign in to forums."), 'attrs': {'placeholder': _("Enter your e-mail address.")}})]
  79. ),
  80. (
  81. None,
  82. ['captcha_qa', 'recaptcha']
  83. ),
  84. ]
  85. def clean_email(self):
  86. try:
  87. email = self.cleaned_data['email'].lower()
  88. email_hash = hashlib.md5(email).hexdigest()
  89. self.found_user = User.objects.get(email_hash=email_hash)
  90. except User.DoesNotExist:
  91. raise ValidationError(_("There is no user with such e-mail address."))
  92. return email
  93. class QuickFindUserForm(Form):
  94. username = forms.CharField()
  95. class UserForumOptionsForm(Form):
  96. timezone = forms.ChoiceField(choices=tzlist())
  97. layout = (
  98. (
  99. _("Date and Time"),
  100. (
  101. ('timezone', {'label': _("Your Current Timezone"), 'help_text': _("If dates and hours displayed by forums are inaccurate, you can fix it by adjusting timezone setting.")}),
  102. )
  103. ),
  104. )