def password_reset(request, pk, template_name='Accounts/password_reset.html'): if request.method == 'GET': form = SetPasswordForm() return render(request, template_name, { 'form': form, 'username': request.session['username'] }) else: form = SetPasswordForm(request.POST) if form.is_valid(): username = User.objects.get(pk=pk).username old_password = form.cleaned_data['old_password'] user = auth.authenticate(username=username, password=old_password) if user is not None: # print "Here is OK" # if user.is_active: new_password = form.cleaned_data['verify_password'] user.set_password(new_password) user.save() # messages.success(request, 'Password has been reset.') return redirect('user_list') else: # print "here is failed" return render( request, template_name, { 'form': form, 'old_password_is_wrong': True, 'username': request.session['username'] }) else: return render(request, template_name, { 'form': form, 'username': request.session['username'] })
def password_view(request, activation_key): user = request.user if request.method == 'POST': form = SetPasswordForm(request.POST) if form.is_valid(): user = CustomUser.objects.get(activation_key=activation_key) user.set_password(form.clean_password()) user.is_active = True user.save() return HttpResponseRedirect(reverse('index')) else: form = SetPasswordForm() context = {'form': form, 'user': user} return render(request, "password.html", context)
def activate(request, activation_key, template_name='user_creation/activate.html'): """ Activates an account if the given key exists and if a password is given through the form. """ activation_key = activation_key.lower() # Normalize before trying anything with it. if request.method == 'POST': form = SetPasswordForm('', request.POST) account = True if form.is_valid(): account = ActivationProfile.objects.activate_user(form.cleaned_data['activation_key']) if account: form.user = account form.save(True) user = authenticate(username=account.username, password=form.cleaned_data['new_password1']) user.backend = settings.AUTHENTICATION_BACKENDS[0] login(request, user) messages.success(request, _("Your password has been set, \ please remember to remember it. You are now logged in.")) return HttpResponseRedirect(getattr(settings, 'LOGIN_REDIRECT_URL','/')) else: form.error_messages={'error': _('Tampered activation key')} else: account = ActivationProfile.objects.check_key(activation_key) form = SetPasswordForm(account, initial={'activation_key':activation_key}) return TemplateResponse(request, template_name, { 'account': account, 'expiration_days': ACCOUNT_ACTIVATION_DAYS, 'set_password_form':form })
def password_reset_confirm(request, confirm_code): ''' контрол восстановления пароля принмает строку с хешем, при валидности отдаёт форму для сброса пароля ''' template = 'accounts_password_change.html' data = dict() try: q_user = CustomUser.objects.get(activation_code=confirm_code) if request.method == 'POST': form = SetPasswordForm(q_user, request.POST) if form.is_valid(): form.save() messages.add_message(request, messages.INFO, 'пароль изменён', fail_silently=True) template = 'accounts_password_change_done.html' else: form = SetPasswordForm(q_user, request.POST) else: form = SetPasswordForm(q_user) data['form'] = form except CustomUser.DoesNotExist: messages.add_message(request, messages.INFO, 'Что-то не так!', fail_silently=True) data['confirm_code'] = confirm_code t = loader.get_template(template) c = RequestContext(request,data, processors=[custom_proc]) return HttpResponse(t.render(c))
def set_pw(): '''Route to set a user's password''' error = None form = SetPasswordForm() if form.validate_on_submit(): username = form.username.data password = form.password.data user = User.query.filter_by(username=username).first() if user: user.set_pw(password) return redirect(url_for('user', username=user.username)) error = 'Unable to reset password.' return render_template('user/set_password.html', form=form, user=None, error=error) username = request.args.get('username') user = User.query.filter_by(username=username).first() key = request.args.get('k') if user and user.pw_reset_key is not None and user.pw_reset_key == key: form = SetPasswordForm(username=user.username) form.validate() return render_template('user/set_password.html', form=form, user=None, error=error) form = PasswordResetForm() error = 'Invalid link, please complete this form to receive a new link' return render_template('user/reset_password.html', form=form, user=None, error=error)