User Django's provided form for setting an users password.

This change allows displaying the current password policies
and validation.

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2017-09-17 01:43:02 -04:00
parent 6e93985fe2
commit f14c67a444
3 changed files with 7 additions and 30 deletions

View File

@@ -7,6 +7,9 @@
- Show the number of pages of a document and of document versions in
the document list view and document versions list views respectively.
- Display a document version's thumbnail before other attributes.
- User Django's provided form for setting an users password.
This change allows displaying the current password policies
and validation.
2.7.3 (2017-09-11)
==================

View File

@@ -2,36 +2,9 @@ from __future__ import unicode_literals
from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.password_validation import validate_password
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
class UserForm(forms.ModelForm):
class Meta:
model = get_user_model()
fields = ('username', 'first_name', 'last_name', 'email', 'is_active',)
class PasswordForm(forms.Form):
new_password_1 = forms.CharField(
label=_('New password'), widget=forms.PasswordInput()
)
new_password_2 = forms.CharField(
label=_('Confirm password'), widget=forms.PasswordInput()
)
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
return super(PasswordForm, self).__init__(*args, **kwargs)
def clean(self):
password_1 = self.cleaned_data['new_password_1']
password_2 = self.cleaned_data['new_password_2']
if password_1 != password_2:
raise ValidationError('Passwords do not match.')
else:
if self.user:
validate_password(password_2, self.user)
return self.cleaned_data

View File

@@ -2,6 +2,7 @@ from __future__ import absolute_import, unicode_literals
from django.contrib import messages
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import SetPasswordForm
from django.contrib.auth.models import Group
from django.contrib.contenttypes.models import ContentType
from django.http import HttpResponseRedirect
@@ -15,7 +16,7 @@ from common.views import (
SingleObjectDeleteView, SingleObjectEditView, SingleObjectListView
)
from .forms import PasswordForm, UserForm
from .forms import UserForm
from .permissions import (
permission_group_create, permission_group_delete, permission_group_edit,
permission_group_view, permission_user_create, permission_user_delete,
@@ -247,7 +248,7 @@ class UserListView(SingleObjectListView):
class UserSetPasswordView(MultipleObjectFormActionView):
form_class = PasswordForm
form_class = SetPasswordForm
model = get_user_model()
success_message = _('Password change request performed on %(count)d user')
success_message_plural = _(
@@ -297,7 +298,7 @@ class UserSetPasswordView(MultipleObjectFormActionView):
)
)
else:
instance.set_password(form.cleaned_data['new_password_1'])
instance.set_password(form.cleaned_data['new_password1'])
instance.save()
messages.success(
self.request, _(