def change_password(request): """page to change password""" help_texts = password_validators_help_texts(password_validators=None) if request.method == 'POST': form = ChangePasswordForm(request.POST, request=request) if form.is_valid(): current_password = form.cleaned_data['current_password'] if request.user.check_password(current_password): new_password = form.cleaned_data.get('new_password') user = request.user user.set_password(new_password) user.save() messages.success(request, "Password updated!") return redirect('accounts:profile') else: messages.error( request, "The password did not match \ your current password!") return redirect('accounts:change_password') else: form = ChangePasswordForm() help_texts = help_texts[:-1] help_texts.append("Your first name, last name, or username" " cannot be in your new password.") return render(request, 'accounts/change_password.html', { 'form': form, 'help_texts': help_texts })
def change_password(request): if request.is_ajax(): user = User.objects.get(id=request.user.id) response = {"status": True, "errors": []} if request.method == "POST": form = ChangePasswordForm(request.user, request.POST or None, use_required_attribute=False) if form.is_valid(): form.save() update_session_auth_hash(request, user) messages.add_message( request, messages.INFO, "Password has been updated successfully! Please Login again" ) else: response["status"] = False for key, value in form.errors.items(): tmp = {'key': key, 'error': value.as_text()} response['errors'].append(tmp) return HttpResponse(json.dumps(response))
def post(self, request): form = ChangePasswordForm(request.user, request.POST) if form.is_valid(): user = form.save() update_session_auth_hash(request, user) messages.success( request, 'Udało sie zmienić hasło. Proszę zalogować się ponownie, nowym hasłem.' ) return redirect('url_login') return render(request, 'change-password.html', {'form': form})
def post(self, request, *arg, **kwargs): user = request.user username = user.get_username() form = ChangePasswordForm(request.POST) if form.is_valid(): account = User.objects.get(username__exact=username) account.set_password(form.cleaned_data.get('password1')) account.save() auth.login(request, account) return redirect('home') else: return render(request, 'registration/change_password.html', {'form': form})
def change_pass(request, pk, token): user = get_object_or_404(User, pk=pk) if request.method == 'POST': form = ChangePasswordForm(request.POST) if form.is_valid(): user.set_password(form.cleaned_data['password1']) user.save() return HttpResponseRedirect('/accounts/login/?username={}'.format(user.username)) return render(request, 'accounts/change_password_form.html', {'form': form}) else: if token_generator.check_token(user, token): form = ChangePasswordForm() return render(request, 'accounts/change_password_form.html', {'form': form}) return render(request, 'accounts/invalid_link.html')
def change_password(request): if request.method == 'POST': user = request.user form = ChangePasswordForm(request.POST, request=request) if form.is_valid(): user.set_password(request.POST['password1']) user.save() response_data = {'error': False, "response": 'Your password is updated !'} else: response_data = {'error': True, 'response': form.errors} return HttpResponse(json.dumps(response_data)) return render(request, 'user/change_password.html')
def test_return_form_invalid_if_current_password_incorrect(self): expected = False form = ChangePasswordForm(data={ 'current_password': '******', 'new_password': '******', 'confirm_password': '******' }, user=self.user, profile=self.profile) result = form.is_valid() self.assertEqual(expected, result)
def change_password(request): if request.method == 'POST': user = request.user form = ChangePasswordForm(request.POST, request=request) if form.is_valid(): user.set_password(request.POST['password1']) user.save() response_data = {'error': False, "response": 'Your password is updated !'} messages.success(request, 'Your password updated successfully!') else: response_data = {'error': True, 'response': form.errors} return HttpResponse(json.dumps(response_data)) return render(request, 'user/change_password.html', {'notification_list': get_notification_list(request.user)})
def test_return_form_valid_if_contains_username(self): expected = True # change password using the password change form form = ChangePasswordForm(data={ 'current_password': self.user.password, 'new_password': '******', 'confirm_password': '******' }, user=self.user, profile=self.profile) result = form.is_valid() self.assertEqual(expected, result)
def test_return_form_invalid_if_contains_last_name(self): expected = False # change password using the password change form form = ChangePasswordForm(data={ 'current_password': self.user.password, 'new_password': '******', 'confirm_password': '******' }, user=self.user, profile=self.profile) result = form.is_valid() self.assertEqual(expected, result)
def test_return_form_invalid_if_not_contain_special_characters(self): expected = False # change password using the password change form form = ChangePasswordForm(data={ 'current_password': self.user.password, 'new_password': '******', 'confirm_password': '******' }, user=self.user, profile=self.profile) result = form.is_valid() self.assertEqual(expected, result)
def password(request): password_changed = False if request.method == "POST": form = ChangePasswordForm(request.user, request.POST) if form.is_valid(): request.user.set_password(form.cleaned_data["new_password"]) request.user.save() update_session_auth_hash(request, request.user) password_changed = True else: form = ChangePasswordForm(request.user) return render(request, "accounts/password.html", { "form": form, "password_changed": password_changed })
def change_password_view(request): if not request.user.is_authenticated() or request.user.type != 'student': return redirect('login') else: form = ChangePasswordForm(request.POST or None) if form.is_valid(): user = get_object_or_404(MyUser, id=request.user.id) user.set_password(form.cleaned_data["password"]) user.save() user_login = authenticate(username=request.user.username, password=form.cleaned_data["password"]) login(request, user_login) messages.success(request, "You have successfully changed password.") return redirect('student:index') context = {'form': form} return render(request, "student/change_password.html", context)
def PasswordChange(request): user = request.user if request.method == 'POST': form = ChangePasswordForm(request.POST) if form.is_valid(): new_password = form.cleaned_data.get('new_password') user.set_password(new_password) user.save() update_session_auth_hash(request, user) return redirect('change_password_done') else: form = ChangePasswordForm(instance=user) context = { 'form': form, } return render(request, 'change_password.html', context)
def change_password_view(request): # if this is a POST request we need to process the form data if request.method == 'POST': # create a form instance and populate it with data from the request: form = ChangePasswordForm(request.POST, user = request.user) # check whether it's valid: if form.is_valid(): # process the data in form.cleaned_data as required # ... # redirect to a new URL: new_password = form.cleaned_data['newPassword'] change_password(request.user, new_password) logout(request) return render(request, "accounts/change_password_done.html", {}) # if a GET (or any other method) we'll create a blank form else: form = ChangePasswordForm(user=request.user) return render(request, "accounts/change_password.html", {'form': form})
def PasswordChange(request): user = request.user if request.method == 'POST': form = ChangePasswordForm(request.POST) if form.is_valid(): old_password = form.cleaned_data.get('old_password') new_password = form.cleaned_data.get('new_password') confirm_password = form.cleaned_data.get('confirm_password') id = request.user.id password = User.objects.get(pk=id) print(password) if not password.check_password(old_password): messages.error(request, 'old password does not match') else: if new_password == confirm_password: user.set_password(new_password) user.save() current_site = get_current_site(request) mail_subject = 'Password change.' message = "your password changed successfully" to_email = request.user.email email = EmailMessage(mail_subject, message, to=[to_email]) email.send() messages.success(request, 'password saved') # if new_password != confirm_password: # messages.success(request,'password does not match') else: messages.error(request, 'password does not match') # update_session_auth_hash(request, user) return redirect('change_password') else: form = ChangePasswordForm() context = { 'form': form, } return render(request, 'accounts/change_password.html', context)
def change_password(request): if not request.user.is_authenticated: messages.error(request, "로그인 하셔야 합니다.") context = {} return redirect('accounts:login') else: if request.method == "POST": change_password_form = ChangePasswordForm(request.user, request.POST) if change_password_form.is_valid(): user = change_password_form.save() update_session_auth_hash(request, user) messages.info(request, '비밀번호 변경 완료') return redirect('accounts:view_profile') else: messages.error(request, '비밀번호 변경 실패') else: change_password_form = ChangePasswordForm(request.user) context = { 'password_change_form': change_password_form, } return render(request, 'accounts/password_change.html', context=context)
def test_return_form_invalid_if_not_contain_upper_and_lower(self): expected = False # change password using the password change form form1 = ChangePasswordForm(data={ 'current_password': self.user.password, 'new_password': '******', 'confirm_password': '******' }, user=self.user, profile=self.profile) form2 = ChangePasswordForm(data={ 'current_password': self.user.password, 'new_password': '******', 'confirm_password': '******' }, user=self.user, profile=self.profile) result1 = form1.is_valid() result2 = form2.is_valid() self.assertEqual(expected, result1) self.assertEqual(expected, result2)
def _get_forms(self): forms = dict() forms['base_form'] = UpdateBaseUserInstance(instance=self.request.user) forms['password_form'] = ChangePasswordForm() if self.request.user.is_company: forms['profile_contact_form'] = UpdateCorporateProfileContactForm(instance=self._get_model()) forms['profile_info_form'] = UpdateCorporateProfileInformationForm(instance=self._get_model()) forms['profile_logo_form'] = UpdateCompanyLogoForm(instance=self._get_model().company_name) else: forms['profile_form'] = UpdateIndividualProfileForm(instance=self._get_model()) forms['profile_info_form'] = UpdatePrivacySettingsForm(instance=self._get_model()) return forms
def post(self, request): form = DeleteAccountForm(request.POST, instance=request.user) if form.is_valid(): form.save() logout(request) return HttpResponseRedirect(reverse('accounts:deleted')) return self.render_to_response( dict(user_form=UserForm(instance=request.user), instructor_form=InstructorForm( instance=request.user.instructor), password_form=ChangePasswordForm(), delete_account_form=form, person=request.user))
def password(request, pk): try: obj = User.objects.get(pk=pk) except User.DoesNotExist: messages.warning(request, "Usuario no encontrada.") return HttpResponseRedirect(reverse("usuarios")) if request.method == 'POST': form = ChangePasswordForm(request.POST) if form.is_valid(): obj.set_password(request.POST.get("new_password")) obj.save() messages.success(request, "Contraseña actualizada.") return HttpResponseRedirect( reverse("empresas_detalle", args=(pk, ))) else: messages.warning(request, 'Verifique la información completada.') else: form = ChangePasswordForm() ctx = { 'form': form, } return render(request, 'usuarios/password.html', ctx)
def change_password(request): if request.method == 'POST': form = ChangePasswordForm(user=request.user, data=request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('logout')) else: form = ChangePasswordForm(user=request.user) results = { 'form': form, 'request': request, } return render(request, 'accounts/change_password.html', results)
def change_password(request): temp_name = "accounts/accounts-header.html" if request.method == 'POST': form = ChangePasswordForm(user=request.user, data=request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('user_list')) else: form = ChangePasswordForm(user=request.user) kwargs = { 'form': form, 'request': request, 'temp_name': temp_name, } return render(request, 'accounts/change_password.html', kwargs)
def change_password(request): user = request.user if request.method == "POST": form = ChangePasswordForm(request.POST) if form.is_valid(): if user.check_password(form.cleaned_data['old_password']) is False: messages.error(request, _('Password is wrong.')) form = ChangePasswordForm() elif form.cleaned_data['new_password1'] != form.cleaned_data[ 'new_password2']: messages.error(request, _('Passwords are not matched.')) form = ChangePasswordForm() else: user.set_password(form.cleaned_data['new_password1']) user.save() messages.success(request, _('Password changed')) return redirect(reverse('profile')) else: messages.error(request, _('Change is failed.')) return redirect(reverse('changepassword')) else: form = ChangePasswordForm() return render(request, 'accounts/change_password.html', {'form': form})
def change_password(request): if request.method == 'POST': JSON_RESULT = {'status': 200, 'message': '', 'error': '', 'data': []} forms = ChangePasswordForm(request, data=request.POST) if forms.is_valid(): forms.save() auth.update_session_auth_hash(request, request.user) JSON_RESULT['message'] = '修改成功' return JsonResponse(JSON_RESULT) JSON_RESULT['status'] = 201 JSON_RESULT['error'] = forms.errors.as_json() return JsonResponse(JSON_RESULT) else: forms = ChangePasswordForm(request) html_title = '修改密码' cancel = reverse('accounts:user-list') return render(request, 'accounts/change_password.html', locals())
def change_email(request): user = request.user data = request.POST.copy() if request.method == 'POST': form = EmailChangeForm(request.POST) if form.is_valid(): if user.check_password(form.cleaned_data['password']) is False: messages.error(request, _('Password is wrong.')) form = ChangePasswordForm() else: user.email = form.cleaned_data['email'] user.is_active = False user.save() act_key = tasks.generate_activation_key(data['email']) exp_key = tasks.generate_key_expires_date() # tasks.send_activation_code(act_key, data['email']) try: userprofile = user.userprofile except UserProfile.DoesNotExist: userprofile = form.instance userprofile.user = user userprofile.is_verified = False userprofile.act_key = act_key userprofile.exp_key = exp_key userprofile.save() messages.success( request, _('Email is changed. Verification mail sent.')) return redirect(reverse('index')) else: messages.error(request, _('Change failed. Enter your Password Correct.')) return redirect(reverse('changeemail')) else: form = EmailChangeForm() return render(request, 'accounts/email_change.html', {'form': form})
def change_password(request): if request.method == "POST": form = ChangePasswordForm(data=request.POST) user = request.user new_password = request.POST['new_password'] current_password = request.POST['current_password'] if not user.check_password(current_password): form.add_error( None, 'Please provide correct password to apply changes.') if form.is_valid(): user.set_password(new_password) user.save() update_session_auth_hash(request, user) return redirect('base:index') else: form = ChangePasswordForm() return render(request, 'accounts/change-password-form.html', context={'form': form})
def change_password(request): message = '' if request.method == 'POST': form = ChangePasswordForm(request.user, request.POST) form_help = PasswordChangeForm(request.user) if form.is_valid(): user = form.save() update_session_auth_hash(request, user) # Important! message = 'Hasło zmienione poprawnie!' else: messages.error(request, 'Please correct the error below.') else: form = ChangePasswordForm(request.user) form_help = PasswordChangeForm(request.user) return render(request, 'accounts/password_change.html', { 'form': form, 'message': message, 'form_help': form_help })
def get(self, request, *arg, **kwargs): form = ChangePasswordForm() return render(request, 'registration/change_password.html', {'form': form})
def post(self, request, *args, **kwargs): if not self.request.user.is_authenticated: messages.error(request=request, message=_('Forbidden request.')) return HttpResponseRedirect("/") if not self.request.POST.get('form-name'): return self.get(self.request) context = self.get_context_data() form_name = self.request.POST.get('form-name') if form_name == 'base-user': request = self.request.POST.copy() request['email'] = self.request.user.email form = UpdateBaseUserInstance(request, instance=self.request.user) if form.is_valid(): form = form.save(commit=False) form.save() messages.success(request=self.request, message=_("User data updated successfully.")) else: context.update({'base_form': form}) return render(self.request, self._get_template(), context) elif form_name == 'public-settings': form = UpdateIndividualProfileForm(request.POST, files=self.request.FILES, instance=self._get_model()) if form.is_valid(): form = form.save(commit=False) form.save() messages.success(request=self.request, message=_("Profile data updated successfully.")) else: context.update({'profile_form': form}) return render(self.request, self._get_template(), context) elif form_name == 'corporate-contact': form = UpdateCorporateProfileContactForm(data=request.POST, files=self.request.FILES, instance=self._get_model()) if form.is_valid(): form = form.save(commit=False) form.save() messages.success(request=request, message=_("Contact information changed successfully.")) else: context.update({'profile_contact_form': form}) return render(self.request, self._get_template(), context) elif form_name == 'corporate-profile': data = self.request.POST.copy() data['operator_type'] = self._get_model().operator_type form = UpdateCorporateProfileInformationForm(data, request.FILES, instance=self._get_model()) form2 = UpdateCompanyLogoForm(request.POST, request.FILES, instance=self._get_model().company_name) if form.is_valid() and form2.is_valid(): form = form.save(commit=False) form.save() form2.save() messages.success(request=request, message=_("Profile information updated successfully.")) else: context.update({'profile_info_form': form}) return render(self.request, self._get_template(), context) elif form_name == 'privacy-settings': form = UpdatePrivacySettingsForm(self.request.POST, files=self.request.FILES, instance=self._get_model()) if form.is_valid(): form = form.save(commit=False) form.save() messages.success(request=self.request, message=_("Privacy settings updated successfully.")) else: context.update({'profile_info_form': form}) return render(self.request, self._get_template(), context) elif form_name == 'password-settings': form = ChangePasswordForm(request.POST, instance=self.request.user) old_password = self.request.user.password if form.is_valid(): user = self.request.user user.password = make_password(request.POST.get('password')) user.save() messages.success(request=request, message=_("Password changed successfully.")) return HttpResponseRedirect('/') context.update({'profile_info_form': form}) return render(self.request, self._get_template(), context) return self.get(self.request)
def get(self, request): form = ChangePasswordForm(request.user) return render(request, 'change-password.html', {'form': form})
def test_oldpassword_help_text(self): form = ChangePasswordForm() self.assertEqual( form.fields['oldpassword'].help_text, 'You must provide your current password in order to change it.')