usercp.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 ChangeForumOptionsBaseForm(forms.ModelForm):
  9. is_hiding_presence = forms.YesNoSwitch(
  10. label=_("Hide my presence"),
  11. help_text=_("If you hide your presence, only members with permission "
  12. "to see hidden will see when you are online."))
  13. limits_private_thread_invites_to = forms.TypedChoiceField(
  14. label=_("Who can add me to private threads"),
  15. coerce=int,
  16. choices=PRIVATE_THREAD_INVITES_LIMITS_CHOICES)
  17. subscribe_to_started_threads = forms.TypedChoiceField(
  18. label=_("Threads I start"), coerce=int, choices=AUTO_SUBSCRIBE_CHOICES)
  19. subscribe_to_replied_threads = forms.TypedChoiceField(
  20. label=_("Threads I reply to"), coerce=int,
  21. choices=AUTO_SUBSCRIBE_CHOICES)
  22. class Meta:
  23. model = get_user_model()
  24. fields = [
  25. 'is_hiding_presence',
  26. 'limits_private_thread_invites_to',
  27. 'subscribe_to_started_threads',
  28. 'subscribe_to_replied_threads'
  29. ]
  30. def ChangeForumOptionsForm(*args, **kwargs):
  31. FinalFormType = type('FinalChangeForumOptionsForm',
  32. (ChangeForumOptionsBaseForm,))
  33. return FinalFormType(*args, **kwargs)
  34. class EditSignatureForm(forms.ModelForm):
  35. signature = forms.CharField(label=_("Signature"), required=False)
  36. class Meta:
  37. model = get_user_model()
  38. fields = ['signature']
  39. def clean(self):
  40. data = super(EditSignatureForm, self).clean()
  41. length_limit = settings.signature_length_max
  42. if len(data) > length_limit:
  43. raise forms.ValidationError(ungettext(
  44. "Signature can't be longer than %(limit)s character.",
  45. "Signature can't be longer than %(limit)s characters.",
  46. length_limit) % {'limit': length_limit})
  47. return data
  48. class ChangeEmailPasswordForm(forms.Form):
  49. current_password = forms.CharField(
  50. label=_("Current password"),
  51. max_length=200,
  52. required=False,
  53. widget=forms.PasswordInput())
  54. new_email = forms.CharField(
  55. label=_("New e-mail"),
  56. max_length=200,
  57. required=False)
  58. new_password = forms.CharField(
  59. label=_("New password"),
  60. max_length=200,
  61. required=False,
  62. widget=forms.PasswordInput())
  63. def __init__(self, *args, **kwargs):
  64. self.user = kwargs.pop('user', None)
  65. super(ChangeEmailPasswordForm, self).__init__(*args, **kwargs)
  66. def clean(self):
  67. data = super(ChangeEmailPasswordForm, self).clean()
  68. current_password = data.get('current_password')
  69. new_email = data.get('new_email')
  70. new_password = data.get('new_password')
  71. if not data.get('current_password'):
  72. message = _("You have to enter your current password.")
  73. raise forms.ValidationError(message)
  74. if not self.user.check_password(current_password):
  75. raise forms.ValidationError(_("Entered password is invalid."))
  76. if not (new_email or new_password):
  77. message = _("You have to enter new e-mail or password.")
  78. raise forms.ValidationError(message)
  79. if new_email:
  80. if new_email.lower() == self.user.email.lower():
  81. message = _("New e-mail is same as current one.")
  82. raise forms.ValidationError(message)
  83. validate_email(new_email)
  84. if new_password:
  85. validate_password(new_password)
  86. return data