usercp.py 4.0 KB

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