def savepassword(request, username): links="" links1=UniversityModel.objects.filter(uploads="-1") if links1.exists(): for link in links1: links=links+link.university+";" if request.user.is_authenticated: if UserModel.objects.filter(username=request.user.username).exists(): username=request.user.username if request.method == 'POST': form = PasswordChangeForm(request.user, request.POST) if form.is_valid(): user = form.save() #sample=UserModel.objects.get(username=username) #sample.password=form.cleaned_data[''] update_session_auth_hash(request, user) # Important! message1="Your password has successfully updated!" return render(request, 'dashboard.html', {"username":username, "message1":message1}) else: message1="Error Messages:" message2=form.errors return render(request, 'changepassword.html', {"message1":message1, "message2":message2, "username":username}) else: form = PasswordChangeForm(request.user) message1="" return render(request, 'changepassword.html', {"username":username}) else: return redirect('/') else: return redirect('/')
def form_valid(self, form): if isinstance(form, PasswordChangeForm): form.save() # Updating the password logs out all other sessions for the user # except the current one. update_session_auth_hash(self.request, form.user) return super().form_valid(form)
def account_settings(request): """Enables the user to change profile info and change password""" user_info_form = UserForm( instance=request.user ) # display a form of user info with displaying last saved info (instance of saved info in db)) change_password_form = PasswordChangeForm( request.user ) # display a form of changing password that belongs to the user loggedin (which exists in the request) if request.method == "POST": # if incoming request method is POST (which is sent by html form) if "old_password" not in request.POST: # this check prevents clearing the profile info when password change form is submitted. # if we don't make this check, the profile form would send empty values for profile data (except username). user_info_form = UserForm( request.POST, instance=request.user ) # pass the request POST data to the django form to be handled with instance of data that hasn't got changed if user_info_form.is_valid(): # validating the form, if true: user_info_form.save( commit=True ) # the django form save function saves the content to the database messages.success( request, "[+] Your profile got successfully updated" ) # creating a success message to be displayed for the user in the page. return redirect( "settings" ) # redirecting to the same page of settings (the purpose is refreshing). change_password_form = PasswordChangeForm( request.user, request.POST ) # pass the request POST data to the django from to be handled with data of the user (request.user) if change_password_form.is_valid(): # validating the form, if true: user = change_password_form.save( ) # saving new password to the db, making object of django form and its save function to have the object responsible of making operations like save function later on. update_session_auth_hash( request, user ) # updating the user session after saving the new password not to be logged out (passing the request and the user object (contain info about the user and the request)) user.save( ) # saving all changes (new session and new password completely.) messages.success( request, "[+] Your password was successfully changed!" ) # creating a success message to be displayed for the user in the page. return redirect( "settings" ) # redirecting to the same page of settings (the purpose is refreshing). else: # if change password form is not validated messages.info( request, '[-] Data is not valid.' ) # creating an info message to be displayed for the user in the page. return redirect( "settings" ) # redirecting to the same page of settings (the purpose is refreshing). context = { "user_form": user_info_form, "password_form": change_password_form } return render( request, "accounts/account_settings.html", context ) # returning rendering the request, the template (page content), and the forms as context dictionary.
def post(self, request): form = PasswordChangeForm(request.user, request.POST) if form.is_valid(): user = form.save() auth_views.update_session_auth_hash(request, user) messages.success(request, f'Twoje hasło zostało zmienione') return redirect('profile') else: messages.error(request, 'Wprowadzone dane są nieprawidłowe') return render(request, "users/password_reset.html", locals())
def update_view(request): if request.method == 'POST': form = PasswordChangeForm(user=request.user, data=request.POST) if form.is_valid(): form.save() update_session_auth_hash(request, form.user) return HttpResponseRedirect('/') else: form = PasswordChangeForm(user=request.user) return render(request, "change_password.html", {'form': form})
def post(self, request): form = PasswordChangeForm(request.user, request.POST) if form.is_valid(): user = form.save() auth_views.update_session_auth_hash(request, user) messages.success(request, "Hasło zostało zmienione") return redirect("profile") else: messages.error(request, "Prosze wprowadzić poprawne dane") return render(request, "users/user_confirm_password_change.html", locals())
def post(self, request): profile = Profile.objects.get(user_id=request.user.id) form = PasswordChangeForm(request.user, request.POST) if form.is_valid(): user = profile.save() auth_views.update_session_auth_hash(request, user) messages.success(request, "Password has benn changed") return redirect('profile') else: messages.error(request, 'Please type new password') return render(request, 'users/user_confirm_password_change.html', locals())
def password_change_company(request): if request.method == "POST": form = PasswordChangeForm(data=request.POST, user=request.user) if form.is_valid(): form.save() update_session_auth_hash(request, form.user) return redirect("companyhome") else: args = {'form': form} return render(request, 'jobportal/Company/passwordchange.html', args) else: company_instance = get_object_or_404(Company, id=request.session['company_instance_id']) form = PasswordChangeForm(request) args = {'form': form, 'company_instance': company_instance} return render(request, 'jobportal/Company/passwordchange.html', args)
def password_change(request, template_name='registration/password_change_form.html', post_change_redirect=None, password_change_form=PasswordChangeForm, extra_context=None): if post_change_redirect is None: post_change_redirect = reverse('password_change_done') else: post_change_redirect = resolve_url(post_change_redirect) if request.method == "POST": form = password_change_form(user=request.user, data=request.POST) if form.is_valid(): form.save() # Updating the password logs out all other sessions for the user # except the current one if # django.contrib.auth.middleware.SessionAuthenticationMiddleware # is enabled. update_session_auth_hash(request, form.user) # return redirect(post_change_redirect) notify.send(request.user, recipient=request.user, verb='you have change your password.',logined=True, user_name=request.user.username) return TemplateResponse(request, 'users/password_change_done.html', {'logined': True, 'user_name':request.user.username}) else: form = password_change_form(user=request.user) if request.user.useravatar: useravatar = request.user.useravatar else: useravatar = 'avatar/default.png' context = { 'form': form, 'logined': True, 'user_name': request.user.username, 'useravatar': useravatar } if extra_context is not None: context.update(extra_context) return TemplateResponse(request, template_name, context)