123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- from django.contrib.auth import get_user_model
- from django.utils.translation import ugettext_lazy as _
- from misago.core import forms, timezones
- from misago.users.models import (PRESENCE_VISIBILITY_CHOICES,
- AUTO_SUBSCRIBE_CHOICES)
- from misago.users.validators import validate_username
- class ChangeForumOptionsBaseForm(forms.ModelForm):
- timezone = forms.ChoiceField(
- label=_("Your current timezone"), choices=[],
- help_text=_("If dates and hours displayed by forums are inaccurate, "
- "you can fix it by adjusting timezone setting."))
- presence_visibility = forms.TypedChoiceField(
- label=_("Show my presence to"),
- choices=PRESENCE_VISIBILITY_CHOICES, coerce=int,
- help_text=_("If you want to, you can limit other members ability to "
- "track your presence on forums."))
- subscribe_to_started_threads = forms.TypedChoiceField(
- label=_("Threads I start"), coerce=int, choices=AUTO_SUBSCRIBE_CHOICES)
- subscribe_to_replied_threads = forms.TypedChoiceField(
- label=_("Threads I reply to"), coerce=int,
- choices=AUTO_SUBSCRIBE_CHOICES)
- class Meta:
- model = get_user_model()
- fields = ['timezone', 'presence_visibility',
- 'subscribe_to_started_threads',
- 'subscribe_to_replied_threads']
- def ChangeForumOptionsForm(*args, **kwargs):
- timezone = forms.ChoiceField(
- label=_("Your current timezone"), choices=timezones.choices(),
- help_text=_("If dates and hours displayed by forums are inaccurate, "
- "you can fix it by adjusting timezone setting."))
- FinalFormType = type('FinalChangeForumOptionsForm',
- (ChangeForumOptionsBaseForm,),
- {'timezone': timezone})
- return FinalFormType(*args, **kwargs)
- class ChangeUsernameForm(forms.Form):
- new_username = forms.CharField(label=_("New username"), max_length=200,
- required=False)
- def __init__(self, *args, **kwargs):
- self.user = kwargs.pop('user', None)
- super(ChangeUsernameForm, self).__init__(*args, **kwargs)
- def clean(self):
- data = super(ChangeUsernameForm, self).clean()
- new_username = data.get('new_username')
- if not new_username:
- raise forms.ValidationError(_("Enter new username."))
- if new_username == self.user.username:
- message = _("New username is same as current one.")
- raise forms.ValidationError(message)
- validate_username(new_username, exclude=self.user)
- return data
- class ChangeEmailPasswordForm(forms.Form):
- current_password = forms.CharField(
- label=_("Current password"),
- max_length=200,
- required=False,
- widget=forms.PasswordInput())
- new_email = forms.CharField(
- label=_("New e-mail"),
- max_length=200,
- required=False)
- new_password = forms.CharField(
- label=_("New password"),
- max_length=200,
- required=False,
- widget=forms.PasswordInput())
- def __init__(self, *args, **kwargs):
- self.user = kwargs.pop('user', None)
- super(ChangeEmailPasswordForm, self).__init__(*args, **kwargs)
- def clean(self):
- data = super(ChangeEmailPasswordForm, self).clean()
- current_password = data.get('current_password')
- new_email = data.get('new_email')
- new_password = data.get('new_password')
- if not data.get('current_password'):
- message = _("You have to enter your current password.")
- raise forms.ValidationError(message)
- if not self.user.check_password(current_password):
- message = _("Entered password is invalid.")
- raise forms.ValidationError(message)
- raise NotImplementedError("change email/pass form is incomplete")
- return data
|