options.py 3.0 KB

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