예제 #1
0
파일: views.py 프로젝트: larikkai/PHoF
def auth_signup():
    if request.method == "GET":
        return render_template("auth/signupform.html", form=SignUpForm())

    form = SignUpForm(request.form)

    if not form.validate():
        return render_template("auth/signupform.html",
                               form=SignUpForm(),
                               error="Invalid information")

    user = User.query.filter_by(username=form.username.data).first()

    if user:
        return render_template("auth/signupform.html",
                               form=SignUpForm(),
                               error=user.username + " already taken")

    user = User(form.firstName.data + ' ' + form.lastName.data)

    user_role = Role.query.filter_by(name='User').first()

    user.username = form.username.data
    password = form.password.data
    pw_hash = generate_password_hash(password).decode('utf-8')
    user.password = pw_hash
    user.roles.append(user_role)
    db.session().add(user)
    db.session().commit()
    return redirect(url_for("auth_login"))
예제 #2
0
def signup_new():

    if request.method == "GET":
        return render_template("auth/signupform.html", form=SignUpForm())

    form = SignUpForm(request.form)

    if not form.validate():
        return render_template("auth/signupform.html", form=form)

    user_exists = User.query.filter_by(username=form.username.data).first()

    if user_exists:
        form.username.errors.append(
            "This username is already in use. Please choose another username.")
        return render_template("auth/signupform.html", form=form)

    name = form.name.data
    username = form.username.data
    password = form.password.data

    new_user = User(name, username, password, 0)
    db.session.add(new_user)
    db.session.commit()

    return redirect(url_for("auth_login"))
예제 #3
0
def auth_signup():
    if request.method == "GET":
        return render_template("auth/signupform.html", form=SignUpForm())

    form = SignUpForm(request.form)

    if not form.validate():
        return render_template("auth/signupform.html", form=form)

    wantedUsername = form.username.data
    existingUser = User.query.filter_by(username=wantedUsername).first()

    # jos käyttäjänimi ei ole jo käytössä
    if not existingUser:
        try:
            newUser = User(form.username.data, form.password.data)

            db.session().add(newUser)
            db.session.commit()
        except:
            flash("Error while creating account, please try again",
                  "alert alert-danger")
            return redirect(url_for("auth_signup"))

        # uusi käyttäjä tallennettiin tietokantaan ja kirjataan sisään
        login_user(newUser)
        flash(
            f"Welcome to the Forum, {current_user.username}! You are ready to start posting.",
            "alert alert-info")
        return redirect(url_for("posts_index"))
    else:
        return render_template(
            "auth/signupform.html",
            form=form,
            error="Wanted username already taken, choose another.")
예제 #4
0
def auth_signup():
    if request.method == 'GET':
        return render_template('auth/signup.html', form=SignUpForm())

    form = SignUpForm(request.form)

    if not form.validate():
        return render_template('auth/signup.html', form=form)

    user = User.query.filter_by(username=form.username.data).first()

    if user:
        return render_template('auth/signup.html',
                               form=form,
                               error='Username is taken')

    password = form.password.data.encode()
    salt = bcrypt.gensalt(rounds=10)
    phash = bcrypt.hashpw(password, salt)

    user = User(username=form.username.data, phash=phash.decode())

    with session_scope() as session:
        session.add(user)
        session.flush()

        roles = Role.query.filter(Role.name.in_(['APPROVED', 'USER'])).all()
        session.bulk_save_objects(
            [UserRole(role_id=role.id, account_id=user.id) for role in roles])

        session.commit()

    return redirect(url_for('auth_login'))
예제 #5
0
def auth_create():
    form = SignUpForm(request.form)

    if not form.validate():
        return render_template("auth/new.html", form=form)

    hashed_password = bcrypt.generate_password_hash(
        form.password.data).decode('utf-8')
    t = User(form.name.data, form.username.data, hashed_password)

    db.session().add(t)
    db.session().commit()

    flash('Your account was successfully created')
    return redirect(url_for("auth_login"))
예제 #6
0
def auth_create():
    form = SignUpForm(request.form)

    if not form.validate():
        return render_template("auth/new.html", form=form)

    usernameExists = User.query.filter_by(username=form.username.data).first()
    if usernameExists:
        return render_template("auth/new.html",
                               form=form,
                               error="Username already exists")

    u = User(form.name.data, form.username.data, form.password.data,
             form.role.data)

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

    return redirect(url_for("courses_index"))
예제 #7
0
def auth_signup():
    if request.method == "GET":
        return render_template("auth/signup.html", form = SignUpForm())

    form = SignUpForm(request.form)

    if not form.validate():
        return render_template("auth/signup.html", form = form)

    user = User(form.name.data, form.username.data, form.password.data, form.email.data)

    db.session().add(user)
    db.session().commit()

    user.set_default_role()
    login_user(user)

    print("Rekisteröinti onnistui")
    return redirect(url_for("index"))