def account_settings(request): current_user = request.user if request.method == 'POST': #user_form = SettingsForm(request.POST, instance = current_user) password_form = UpdatePasswordForm(current_user, request.POST) profile_form = ProfileForm(request.POST, instance = current_user.profile) verify_form = ValidPasswordForm(request.POST, instance = current_user.profile) #user_form.is_valid() #correct_password = authenticate(username=request.POST['username'], password=user_form.cleaned_data['password']) #if not correct_password: # messages.error(request, ('Incorrect password. You must enter your password to save changes.')) # return HttpResponseRedirect(request.path_info) #elif user_form.is_valid() and profile_form.is_valid() and password_form.is_valid(): if verify_form.is_valid(): correct_password = request.user.check_password(verify_form.data.get('password')) if correct_password: if password_form.is_valid(): if password_form.has_changed(): current_user = password_form.save() update_session_auth_hash(request, current_user) messages.success(request, ('Your password was successfully updated!')) else: messages.error(request, ('Please correct the errors in your Password: {}'.format(list(password_form.errors.values())))) if profile_form.is_valid(): if profile_form.has_changed(): #user_form.save() profile = profile_form.save(commit=False) profile.user = request.user profile.save() #user = password_form.save() update_session_auth_hash(request, current_user) messages.success(request, ('Your profile was successfully updated!')) else: messages.error(request, ('Please correct the errors in your Profile data: {}'.format(list(profile_form.errors.values())))) else: messages.error(request, ('Please enter your current password.')) else: messages.error(request, ('Please correct the errors in your Current Password: {}'.format(list(verify_form.errors.values())))) return HttpResponseRedirect(request.path_info) else: # user_form = SettingsForm(instance = current_user) password_form = UpdatePasswordForm(current_user) profile_form = ProfileForm(instance = current_user.profile) verify_form = ValidPasswordForm(instance = current_user) #context = {"user": current_user, 'user_form': user_form, 'password_form': password_form, 'profile_form': profile_form} context = {'password_form': password_form, 'profile_form': profile_form, 'verify_form': verify_form} return render(request, 'account/settings.html', context=context)
def account_settings(request): current_user = request.user if request.method == 'POST': password_form = UpdatePasswordForm(current_user, request.POST) profile_form = ProfileForm(request.POST, instance = current_user.profile) verify_form = ValidPasswordForm(request.POST, instance = current_user.profile) if verify_form.is_valid(): correct_password = request.user.check_password(verify_form.data.get('password')) if correct_password: if "change_password" in request.POST: if password_form.is_valid(): if password_form.has_changed(): current_user = password_form.save() update_session_auth_hash(request, current_user) messages.success(request, ('Your password was successfully updated!')) else: messages.error(request, ('Please correct the errors in your Password: {}'.format(list(password_form.errors.values())))) elif "edit_profile" in request.POST: if profile_form.is_valid(): if profile_form.has_changed(): #user_form.save() profile = profile_form.save(commit=False) profile.user = request.user profile.save() #user = password_form.save() update_session_auth_hash(request, current_user) messages.success(request, ('Your profile was successfully updated!')) else: messages.error(request, ('Please correct the errors in your Profile data: {}'.format(list(profile_form.errors.values())))) else: messages.error(request, ("Error: Unknown Submission Action")) else: messages.error(request, ('Wrong Password. Please enter your current password.')) else: messages.error(request, ('Please correct the errors in your Current Password: {}'.format(list(verify_form.errors.values())))) return HttpResponseRedirect(request.path_info) else: # user_form = SettingsForm(instance = current_user) password_form = UpdatePasswordForm(current_user) profile_form = ProfileForm(instance = current_user.profile) verify_form = ValidPasswordForm(instance = current_user) #context = {"user": current_user, 'user_form': user_form, 'password_form': password_form, 'profile_form': profile_form} context = {'password_form': password_form, 'profile_form': profile_form, 'verify_form': verify_form} return render(request, 'account/settings.html', context=context)
def account_settings(request): current_user = request.user if request.method == "POST": request.POST = request.POST.copy() # Clean date fields for date_field in ["graduation_date", "date_of_birth"]: value = request.POST.get(date_field) if value == "____-__-__": value = "" request.POST[date_field] = value password_form = UpdatePasswordForm(current_user, request.POST) profile_form = ProfileForm( request.POST, request.FILES, instance=current_user.profile ) verify_form = ValidPasswordForm(request.POST, instance=current_user.profile) if verify_form.is_valid(): correct_password = request.user.check_password( verify_form.data.get("password") ) if correct_password: if "change_password" in request.POST: if password_form.is_valid(): if password_form.has_changed(): current_user = password_form.save() update_session_auth_hash(request, current_user) messages.success( request, ("Your password was successfully updated!") ) else: messages.error( request, ( "Please correct the errors in your Password: {}".format( list(password_form.errors.values()) ) ), ) elif "edit_profile" in request.POST: if profile_form.is_valid(): if profile_form.has_changed(): # user_form.save() profile = profile_form.save(commit=False) profile.user = request.user profile.save() # user = password_form.save() update_session_auth_hash(request, current_user) messages.success( request, ("Your profile was successfully updated!") ) else: messages.error( request, ( "Please correct the errors in your Profile data: {}".format( list(profile_form.errors.values()) ) ), ) else: messages.error(request, ("Error: Unknown Submission Action")) else: messages.error( request, ("Wrong Password. Please enter your current password.") ) else: messages.error( request, ( "Please correct the errors in your Current Password: {}".format( list(verify_form.errors.values()) ) ), ) return HttpResponseRedirect(request.path_info) # lgtm [py/url-redirection] else: # user_form = SettingsForm(instance = current_user) password_form = UpdatePasswordForm(current_user) profile_form = ProfileForm(instance=current_user.profile) verify_form = ValidPasswordForm(instance=current_user) # context = {"user": current_user, 'user_form': user_form, 'password_form': password_form, 'profile_form': profile_form} context = { "password_form": password_form, "profile_form": profile_form, "verify_form": verify_form, } return render(request, "account/settings.html", context=context)