def forgotten_password(): form = ForgotPasswordForm() if form.validate_on_submit(): user = User.query.filter_by(username=form.username.data, email=form.email.data).first() if not user: flash("Account not found!", "danger") return redirect(url_for("core.forgotten_password")) user.generate_activation_key() send_email( [user.email], "[Ignition] Password Reset Link", render_template( "core/email_registration.txt", username=user.username, siteurl=url_for("core.home", _external=True), activationurl=url_for( "core.reset_password", username=user.username, key=user.activation_key, _external=True ), ), render_template( "core/email_registration.html", username=user.username, siteurl=url_for("core.home", _external=True), activationurl=url_for( "core.reset_password", username=user.username, key=user.activation_key, _external=True ), ), ) db.session.add(user) db.session.commit() flash("An email has been dispatched and you will have a password reset link shortly", "success") return redirect(url_for("core.login")) else: flash_errors(form) return render_template("core/forgot_password.html", form=form)
def register(): form = RegistrationForm() if form.validate_on_submit(): # Some extra validation - we should check to see if there's already a user registered with either that email # or that username existing_username = User.query.filter_by(username=form.username.data).first() if existing_username: flash("That username is already in use!", "danger") return redirect(url_for("core.register")) existing_email = User.query.filter_by(email=form.email.data).first() if existing_email: flash("That email address is already in use!", "danger") return redirect(url_for("core.register")) # Create user model new_user = User(username=form.username.data, email=form.email.data, password=form.password.data) if app.config["FORCE_EMAIL_ACTIVATION"]: # Set activation key new_user.generate_activation_key() # Send the new user their activation code send_email( [new_user.email], "[Ignition] Welcome to Ignition, Please Activate Your Account", render_template( "core/email_registration.txt", username=new_user.username, siteurl=url_for("core.home", _external=True), activationurl=url_for( "core.validate_registration", username=new_user.username, key=new_user.activation_key, _external=True, ), ), render_template( "core/email_registration.html", username=new_user.username, siteurl=url_for("core.home", _external=True), activationurl=url_for( "core.validate_registration", username=new_user.username, key=new_user.activation_key, _external=True, ), ), ) post = url_for("core.post_register") else: new_user.activate() flash("Account created, you may now log in", "success") post = url_for("core.home") # Save user db.session.add(new_user) db.session.commit() return redirect(post) else: flash_errors(form) return render_template("core/register.html", form=form)