Esempio n. 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')
Esempio n. 2
0
def toggle_admin_for_user(user_id):
    db = Database()

    if is_logged_in() and db.is_user_id_administrator(
            flask_session['user_id']):
        user = db.get_user_by_id(user_id)
        role_id = user['role_id']
        if role_id == db.get_administrator_role_id():
            role_id = db.get_user_role_id()
        else:
            role_id = db.get_administrator_role_id()

        db.update_users_role(user_id, role_id)
        return redirect_to_referrer()
    else:
        flash(
            'You must be logged in as an administrator to perform this operation.',
            'danger')
        return redirect_to_referrer()