Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import django.contrib.auth
- import django.core.exceptions
- from django import forms
- from django.utils.translation import ugettext_lazy as _
- class RequiresPasswordMixin(forms.Form):
- error_messages = {
- 'password_incorrect': _("Your old password was entered incorrectly. Please enter it again."),
- }
- password = forms.CharField(
- label=_("Password"),
- strip=False,
- widget=forms.PasswordInput,
- required=True
- )
- def __init__(self, *args, **kwargs):
- self.username = kwargs.pop('username')
- super().__init__(*args, **kwargs)
- def clean_password(self):
- self.user_cache = django.contrib.auth.authenticate(username=self.username, password=self.cleaned_data['password'])
- if self.user_cache is None:
- raise forms.ValidationError(
- self.error_messages['password_incorrect'],
- code='password_incorrect',
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement