Exemple #1
0
def create_user():
    """Create a new user
    """
    form = UserCreateForm(request.form)
    form.role.choices = [(r, r) for r in User.ROLES]

    if form.validate_on_submit():
        u = User(username=form.username.data,
                 email=form.email.data,
                 is_active=form.is_active.data,
                 role=form.role.data)
        u.password_hash = custom_app_context.hash(form.password.data)

        if User.username_is_in_use(u.username):
            flash(
                "This username is already been used. Please choose another one!",
                "alert-danger")
            form.username.errors.append('Please correct this field')
            return render_template("dashboard/user_create.html", form=form)

        if User.email_is_in_use(u.email):
            flash(
                "This email is already been used. Please choose another one!",
                "alert-danger")
            form.email.errors.append('Please correct this field')
            return render_template("dashboard/user_create.html", form=form)

        db.session.add(u)
        db.session.commit()

        flash("User created", "alert-success")
        return redirect(url_for('dashboard.users_list'))
    return render_template("dashboard/user_create.html", form=form)
Exemple #2
0
def create_user():
    """Create a new user
    """
    form = UserCreateForm(request.form)
    form.role.choices = [(r, r) for r in User.ROLES]

    if form.validate_on_submit():
        u = User(username=form.username.data,
                 email=form.email.data,
                 is_active=form.is_active.data,
                 role=form.role.data)
        u.password_hash = custom_app_context.hash(form.password.data)

        db.session.add(u)
        db.session.commit()

        flash("User created")
        return redirect(url_for('dashboard.users_list'))
    return render_template("dashboard/user_create.html", form=form)