usercp.py 3.8 KB

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