def password_change(request): if request.method == 'POST': password_change_form = auth_views.PasswordChangeForm(user=request.user, data=request.POST) if password_change_form.is_valid(): password_change_form.save() update_session_auth_hash(request, password_change_form.user) return redirect('update-user') else: password_change_form = auth_views.PasswordChangeForm(user=request.user) return render(request, 'users/change-password.html', {'form': password_change_form})
def change_password(request): if request.user.is_authenticated: if request.method == 'POST': form = views.PasswordChangeForm(request.user, data=request.POST) if form.is_valid(): form.save() update_session_auth_hash(request, form.user) return redirect('index') else: form = views.PasswordChangeForm(request.user) return render(request, 'registration/password_change.html', {'form': form}) else: return redirect('index')
def post(self, request, *args, **kwargs): form = views.PasswordChangeForm(request.user, request.POST) if form.is_valid(): user = form.save() update_session_auth_hash(request, user) messages.success(request, 'Senha alterada com sucesso') return redirect('users:index') else: messages.error(request, 'Por favor verifique os erros abaixo.')
def testPasswordChange(self): # Get created user user = User.objects.get(username="******") # Test Change Password Form is found response = self.client.get(reverse('changepassword')) assert (response.status_code == 200 or response.status_code == 302) # Test Password Change Form is validated form = auth_views.PasswordChangeForm(user=user, data={ 'old_password': '******', 'new_password1': 'newTestPassword', 'new_password2': 'newTestPassword' }) self.assertTrue(form.is_valid())
def get(self, request, *args, **kwargs): form = views.PasswordChangeForm(request.user) return render(request, self.template_name, { 'form': form, })