options.py 2.9 KB

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