예제 #1
0
def register():
    if request.method.upper() == 'GET':
        db = Database()
        questions = db.list_questions()
        return render_template('register.html', questions=questions)
    elif request.method.upper() == 'POST':
        username = request.form['username']
        password = request.form['password']
        password2 = request.form['confirm_password']
        question1 = request.form['question1']
        question2 = request.form['question2']
        question3 = request.form['question3']
        answer1 = request.form['answer1']
        answer2 = request.form['answer2']
        answer3 = request.form['answer3']

        if password != password2:
            flash('Passwords are not the same', 'danger')
            return redirect_to_referrer()

        db = Database()

        if db.does_username_exist(username):
            flash('Username already exists', 'danger')
            return redirect_to_referrer()
        if len([question1, question2, question3]) != len(
                list(set([question1, question2, question3]))):
            flash('Questions are the same', 'danger')
            return redirect_to_referrer()
        first_name = request.form['first_name']
        last_name = request.form['last_name']
        email_address = request.form['email_address']

        if len(first_name) == 0:
            first_name = None

        if len(last_name) == 0:
            last_name = None

        if len(email_address) == 0:
            email_address = None

        userId = db.create_user(db.get_user_role_id(), username, password,
                                first_name, last_name, email_address)
        user = db.get_user(username, password)
        db.create_answer(userId, question1, answer1)
        db.create_answer(userId, question2, answer2)
        db.create_answer(userId, question3, answer3)

        for key in user.keys():
            flask_session[key] = user[key]

        questions = db.list_questions()

    return render_template('user_settings.html',
                           questions=questions,
                           success_message='Account created successfully')
예제 #2
0
def settings():
    if not is_logged_in():  # REDIRECT TO LOGIN
        flash('You must be logged in to view this.', 'danger')
        return redirect('/error')

    db = Database()
    questions = db.list_questions()

    return render_template('user_settings.html', questions=questions)
예제 #3
0
def update_settings():
    if not is_logged_in():
        flash('You must be logged in to perform this operation.', 'danger')
        return redirect('/')

    user_id = get_current_user_id()
    first_name = request.form['first_name']
    last_name = request.form['last_name']
    email = request.form['email']

    db = Database()
    db.update_user_first_name(user_id, first_name)
    db.update_user_last_name(user_id, last_name)
    db.update_user_email_address(user_id, email)

    answer1 = request.form['answer1']
    answer2 = request.form['answer2']
    answer3 = request.form['answer3']
    if (answer1 or answer2 or answer3):
        question1 = request.form['question1']
        question2 = request.form['question2']
        question3 = request.form['question3']

        if len([question1, question2, question3]) != len(
                list(set([question1, question2, question3]))):
            flash('Questions are the same', 'danger')
            return redirect_to_referrer()
        db.delete_user_answers(user_id)
        db.create_answer(user_id, question1, answer1)
        db.create_answer(user_id, question2, answer2)
        db.create_answer(user_id, question3, answer3)

    refresh_cache()

    password = request.form['password']
    password2 = request.form['confirm_password']

    if len(password) > 0 and len(password2) > 0:
        if password == password2:
            db.update_user_password(user_id, password)
        else:
            flash('There was an issue; passwords were not updated.', 'danger')
            return redirect('/settings')

    questions = db.list_questions()
    return render_template('user_settings.html',
                           success_message='Settings updated successfully',
                           questions=questions)