-
Notifications
You must be signed in to change notification settings - Fork 102
Open
Description
I just wrote my own PasswordChangeForm/PasswordChangeView in one of my project. (The form is two password fields, and ask for the current password). I also created EmailChangeForm/EmailChangeView (with newemail/password confirmation)
IMHO, it sounds like something that should go in authtools. What do you think?
FYI, It looks like this:
class UpdatePasswordForm(forms.Form):
password1 = forms.CharField(label='New password',
widget=forms.PasswordInput)
password2 = forms.CharField(
label='Password confirmation',
help_text='Same password as above.',
widget=forms.PasswordInput,
)
current = forms.CharField(label='Current password',
widget=forms.PasswordInput)
def __init__(self, *args, **kwargs):
self.instance = kwargs.pop('instance', None)
assert self.instance is not None
super(UpdatePasswordForm, self).__init__(*args, **kwargs)
def clean_current(self):
password = self.cleaned_data['current']
assert self.instance is not None
if not self.instance.check_password(password):
raise forms.ValidationError("The current password was invalid.")
return password
def clean(self):
cleaned_data = super(UpdatePasswordForm, self).clean()
if cleaned_data['password1'] != cleaned_data['password2']:
raise forms.ValidationError("Passwords didn't match")
return cleaned_data
def save(self, commit=True):
assert self.instance is not None
self.instance.set_password(self.cleaned_data['password1'])
if commit:
self.instance.save()
return self.instance
class UpdatePasswordView(EnsureAuthMixin, UpdateView):
form_class = UpdatePasswordForm
template_name = 'auth/update_password.html'
def get_object(self):
user = self.request.user
assert not user.is_anonymous()
return userMetadata
Metadata
Assignees
Labels
No labels