options.py 2.9 KB

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