Advertisement
Guest User

Untitled

a guest
Sep 28th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. import django.contrib.auth
  2. import django.core.exceptions
  3. from django import forms
  4. from django.utils.translation import ugettext_lazy as _
  5.  
  6.  
  7. class RequiresPasswordMixin(forms.Form):
  8. error_messages = {
  9. 'password_incorrect': _("Your old password was entered incorrectly. Please enter it again."),
  10. }
  11.  
  12. password = forms.CharField(
  13. label=_("Password"),
  14. strip=False,
  15. widget=forms.PasswordInput,
  16. required=True
  17. )
  18.  
  19. def __init__(self, *args, **kwargs):
  20. self.username = kwargs.pop('username')
  21. super().__init__(*args, **kwargs)
  22.  
  23. def clean_password(self):
  24. self.user_cache = django.contrib.auth.authenticate(username=self.username, password=self.cleaned_data['password'])
  25. if self.user_cache is None:
  26. raise forms.ValidationError(
  27. self.error_messages['password_incorrect'],
  28. code='password_incorrect',
  29. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement