usercp.py 4.4 KB

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