示例#1
0
def register():
    form = CreateAccountForm()
    if form.validate_on_submit():
        user = User(**request.form)
        db.session.add(user)
        db.session.commit()

        email_template, subject = generate_url_and_email_template(
            form.email.data,
            form.username.data,
            form.first_name.data,
            form.last_name.data,
            email_category="verify_email",
        )
        send_email.delay(form.email.data, subject, email_template)
        flash(
            "Your account has been created. Check your inbox to verify your email.",
            "success",
        )
        return redirect(url_for("base_blueprint.login"))

    return render_template("accounts/register.html", form=form)
示例#2
0
def register():
    create_account_form = CreateAccountForm(request.form)
    # -------------------------------------------------------------------------#
    # POST Request - Valid
    if create_account_form.validate_on_submit():

        user = User.query.filter_by(
            username=create_account_form.username.data).first()
        # Check if usename exists
        if user:
            flash("Username already registered.", "info")
            return render_template(
                "accounts/register.html",
                msg="Username already registered.",
                form=create_account_form,
            )

        # Check if email exists
        user = User.query.filter_by(
            email=create_account_form.email.data).first()
        if user:
            flash("Email already registered.", "info")
            return render_template(
                "accounts/register.html",
                msg="Email already registered.",
                form=create_account_form,
            )

        # Create user from data
        user = User(**request.form)

        # Send an activation email
        confirmation_token = generate_confirmation_token(user.email)
        confirm_url = url_for("base_blueprint.confirm_email",
                              token=confirmation_token,
                              _external=True)
        html = render_template("accounts/activate.html",
                               confirm_url=confirm_url)
        subject = "Please confirm your email."
        try:
            send_email(user.email, subject, html)

        except SMTPAuthenticationError:
            flash(
                "Server-side authentication eror. " +
                "Was mail properly configured for this app?",
                "error",
            )
            return redirect(url_for("base_blueprint.login"))

        # Add the unconfirmed user
        db.session.add(user)
        db.session.commit()

        flash("A confirmation email has been sent.", "success")
        return redirect(url_for("base_blueprint.route_default"))

    # GET Request
    else:
        return render_template("accounts/register.html",
                               form=create_account_form)