options.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from django import forms
  2. from django.contrib.auth import get_user_model
  3. from django.contrib.auth.password_validation import validate_password
  4. from django.utils.translation import ugettext_lazy as _
  5. from django.utils.translation import ungettext
  6. from misago.conf import settings
  7. from misago.core.forms import YesNoSwitch
  8. from ..validators import validate_email
  9. UserModel = get_user_model()
  10. class ForumOptionsForm(forms.ModelForm):
  11. is_hiding_presence = YesNoSwitch()
  12. limits_private_thread_invites_to = forms.TypedChoiceField(
  13. choices=UserModel.LIMIT_INVITES_TO_CHOICES,
  14. coerce=int,
  15. )
  16. subscribe_to_started_threads = forms.TypedChoiceField(
  17. choices=UserModel.SUBSCRIBE_CHOICES,
  18. coerce=int,
  19. )
  20. subscribe_to_replied_threads = forms.TypedChoiceField(
  21. choices=UserModel.SUBSCRIBE_CHOICES,
  22. coerce=int,
  23. )
  24. class Meta:
  25. model = UserModel
  26. fields = [
  27. 'is_hiding_presence',
  28. 'limits_private_thread_invites_to',
  29. 'subscribe_to_started_threads',
  30. 'subscribe_to_replied_threads'
  31. ]
  32. class EditSignatureForm(forms.ModelForm):
  33. signature = forms.CharField(required=False)
  34. class Meta:
  35. model = UserModel
  36. fields = ['signature']
  37. def clean(self):
  38. data = super(EditSignatureForm, self).clean()
  39. if len(data.get('signature', '')) > settings.signature_length_max:
  40. raise forms.ValidationError(_("Signature is too long."))
  41. return data
  42. class ChangePasswordForm(forms.Form):
  43. password = forms.CharField(max_length=200)
  44. new_password = forms.CharField(max_length=200)
  45. def __init__(self, *args, **kwargs):
  46. self.user = kwargs.pop('user', None)
  47. super(ChangePasswordForm, self).__init__(*args, **kwargs)
  48. def clean_password(self):
  49. if not self.user.check_password(self.cleaned_data['password']):
  50. raise forms.ValidationError(_("Entered password is invalid."))
  51. def clean_new_password(self):
  52. data = self.cleaned_data['new_password']
  53. validate_password(data, user=self.user)
  54. return data
  55. class ChangeEmailForm(forms.Form):
  56. password = forms.CharField(max_length=200)
  57. new_email = forms.CharField(max_length=200)
  58. def __init__(self, *args, **kwargs):
  59. self.user = kwargs.pop('user', None)
  60. super(ChangeEmailForm, self).__init__(*args, **kwargs)
  61. def clean_password(self):
  62. if not self.user.check_password(self.cleaned_data['password']):
  63. raise forms.ValidationError(_("Entered password is invalid."))
  64. def clean_new_email(self):
  65. data = self.cleaned_data['new_email']
  66. if not data:
  67. message = _("You have to enter new e-mail address.")
  68. raise forms.ValidationError(message)
  69. if data.lower() == self.user.email.lower():
  70. message = _("New e-mail is same as current one.")
  71. raise forms.ValidationError(message)
  72. validate_email(data)
  73. return data