usercp.py 4.8 KB

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