def user_email_change(): form = EmailForm() if request.method == 'POST': if form.validate_on_submit(): try: user_check = User.query.filter_by( email=form.email.data).first() if user_check is None: user = current_user user.email = form.email.data user.email_confirmed = False user.email_confirmed_on = None user.email_confirmation_sent_on = datetime.now() db.session.add(user) db.session.commit() send_confirmation_email(user.email) flash( 'Email changed! Please confirm your new email address (link sent to new email).', 'success') return redirect(url_for('users.user_profile')) else: flash('Sorry, that email already exist', 'error') except IntegrityError: flash('Error! That email already exist!', 'error') return render_template('email_change.html', form=form)
def reset(): form = EmailForm() if form.validate_on_submit(): try: user = User.query.filter_by(email=form.email.data).first_or_404() except: flash('Invalid email address!', 'error') return render_template('password_reset_email.html', form=form) if user.email_confirmed: send_password_reset_email(user.email) flash('Please check your email for a password reset link', 'success') else: flash( 'Your email address must be confirmed before attemping a password reset.', 'error') return redirect(url_for('users.login')) return render_template('password_reset_email.html', form=form)
def user_email_change(): form = EmailForm(request.form) if request.method == 'POST': if form.validate_on_submit(): email_exists = User.query.filter_by(email=form.email.data).first() if email_exists is None: user = current_user new_email = form.email.data send_token_email(new_email, 'user.confirm_email', 'email_confirmation.html', 'Confirm your email address.', [user.email, new_email], 'email-confirmation') flash( 'A verification email is sent to your new email address. Please verify this new address.', 'success') return redirect(url_for('user.user_profile')) else: flash('Sorry, that email is already registered.', 'error') return render_template('form_change_email.html', form=form)
def reset(): form = EmailForm() if request.method == 'POST': if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if user is None: flash('Invalid email adress!', 'error') return render_template('form_password_reset.html', form=form) if user.email_confirmed: send_password_reset_email(user.email) flash('Please check your email for a password reset link.', 'success') else: flash( 'Your email address must be confirmed before attempting a password reset.', 'error') return redirect(url_for('user.login')) return render_template('form_password_reset.html', form=form)