usercp.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from django.contrib.auth import get_user_model
  2. from django.utils.translation import ugettext_lazy as _
  3. from misago.core import forms, timezones
  4. from misago.users.models import AUTO_SUBSCRIBE_CHOICES
  5. from misago.users.validators import (validate_email, validate_password,
  6. validate_username)
  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 ChangeUsernameForm(forms.Form):
  36. new_username = forms.CharField(label=_("New username"), max_length=200,
  37. required=False)
  38. def __init__(self, *args, **kwargs):
  39. self.user = kwargs.pop('user', None)
  40. super(ChangeUsernameForm, self).__init__(*args, **kwargs)
  41. def clean(self):
  42. data = super(ChangeUsernameForm, self).clean()
  43. new_username = data.get('new_username')
  44. if not new_username:
  45. raise forms.ValidationError(_("Enter new username."))
  46. if new_username == self.user.username:
  47. message = _("New username is same as current one.")
  48. raise forms.ValidationError(message)
  49. validate_username(new_username, exclude=self.user)
  50. return data
  51. class ChangeEmailPasswordForm(forms.Form):
  52. current_password = forms.CharField(
  53. label=_("Current password"),
  54. max_length=200,
  55. required=False,
  56. widget=forms.PasswordInput())
  57. new_email = forms.CharField(
  58. label=_("New e-mail"),
  59. max_length=200,
  60. required=False)
  61. new_password = forms.CharField(
  62. label=_("New password"),
  63. max_length=200,
  64. required=False,
  65. widget=forms.PasswordInput())
  66. def __init__(self, *args, **kwargs):
  67. self.user = kwargs.pop('user', None)
  68. super(ChangeEmailPasswordForm, self).__init__(*args, **kwargs)
  69. def clean(self):
  70. data = super(ChangeEmailPasswordForm, self).clean()
  71. current_password = data.get('current_password')
  72. new_email = data.get('new_email')
  73. new_password = data.get('new_password')
  74. if not data.get('current_password'):
  75. message = _("You have to enter your current password.")
  76. raise forms.ValidationError(message)
  77. if not self.user.check_password(current_password):
  78. raise forms.ValidationError(_("Entered password is invalid."))
  79. if not (new_email or new_password):
  80. message = _("You have to enter new e-mail or password.")
  81. raise forms.ValidationError(message)
  82. if new_email:
  83. if new_email.lower() == self.user.email.lower():
  84. message = _("New e-mail is same as current one.")
  85. raise forms.ValidationError(message)
  86. validate_email(new_email)
  87. if new_password:
  88. validate_password(new_password)
  89. return data