options.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 ChangeEmailPasswordForm(forms.Form):
  35. current_password = forms.CharField(
  36. label=_("Current password"),
  37. max_length=200,
  38. required=False,
  39. widget=forms.PasswordInput())
  40. new_email = forms.CharField(
  41. label=_("New e-mail"),
  42. max_length=200,
  43. required=False)
  44. new_password = forms.CharField(
  45. label=_("New password"),
  46. max_length=200,
  47. required=False,
  48. widget=forms.PasswordInput())
  49. def __init__(self, *args, **kwargs):
  50. self.user = kwargs.pop('user', None)
  51. super(ChangeEmailPasswordForm, self).__init__(*args, **kwargs)
  52. def clean(self):
  53. data = super(ChangeEmailPasswordForm, self).clean()
  54. current_password = data.get('current_password')
  55. new_email = data.get('new_email')
  56. new_password = data.get('new_password')
  57. if not data.get('current_password'):
  58. message = _("You have to enter your current password.")
  59. raise forms.ValidationError(message)
  60. if not self.user.check_password(current_password):
  61. raise forms.ValidationError(_("Entered password is invalid."))
  62. if not (new_email or new_password):
  63. message = _("You have to enter new e-mail or password.")
  64. raise forms.ValidationError(message)
  65. if new_email:
  66. if new_email.lower() == self.user.email.lower():
  67. message = _("New e-mail is same as current one.")
  68. raise forms.ValidationError(message)
  69. validate_email(new_email)
  70. if new_password:
  71. validate_password(new_password)
  72. return data