usercp.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 (PRESENCE_VISIBILITY_CHOICES,
  5. AUTO_SUBSCRIBE_CHOICES)
  6. from misago.users.validators import 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. presence_visibility = forms.TypedChoiceField(
  13. label=_("Show my presence to"),
  14. choices=PRESENCE_VISIBILITY_CHOICES, coerce=int,
  15. help_text=_("If you want to, you can limit other members ability to "
  16. "track your presence on forums."))
  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', 'presence_visibility',
  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 ChangeUsernameForm(forms.Form):
  37. new_username = forms.CharField(label=_("New username"), max_length=200,
  38. required=False)
  39. def __init__(self, *args, **kwargs):
  40. self.user = kwargs.pop('user', None)
  41. super(ChangeUsernameForm, self).__init__(*args, **kwargs)
  42. def clean(self):
  43. data = super(ChangeUsernameForm, self).clean()
  44. new_username = data.get('new_username')
  45. if not new_username:
  46. raise forms.ValidationError(_("Enter new username."))
  47. if new_username == self.user.username:
  48. message = _("New username is same as current one.")
  49. raise forms.ValidationError(message)
  50. validate_username(new_username, exclude=self.user)
  51. return data
  52. class ChangeEmailPasswordForm(forms.Form):
  53. current_password = forms.CharField(
  54. label=_("Current password"),
  55. max_length=200,
  56. required=False,
  57. widget=forms.PasswordInput())
  58. new_email = forms.CharField(
  59. label=_("New e-mail"),
  60. max_length=200,
  61. required=False)
  62. new_password = forms.CharField(
  63. label=_("New password"),
  64. max_length=200,
  65. required=False,
  66. widget=forms.PasswordInput())
  67. def __init__(self, *args, **kwargs):
  68. self.user = kwargs.pop('user', None)
  69. super(ChangeEmailPasswordForm, self).__init__(*args, **kwargs)
  70. def clean(self):
  71. data = super(ChangeEmailPasswordForm, self).clean()
  72. current_password = data.get('current_password')
  73. new_email = data.get('new_email')
  74. new_password = data.get('new_password')
  75. if not data.get('current_password'):
  76. message = _("You have to enter your current password.")
  77. raise forms.ValidationError(message)
  78. if not self.user.check_password(current_password):
  79. message = _("Entered password is invalid.")
  80. raise forms.ValidationError(message)
  81. raise NotImplementedError("change email/pass form is incomplete")
  82. return data