usercp.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.ModelForm):
  53. class Meta:
  54. model = get_user_model()
  55. fields = ['username', 'email', 'title']