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')
def login(): if request.method.upper() == 'GET': return render_template('/session/viewall.html') elif request.method.upper() == 'POST': username = request.form['username'] password = request.form['password'] db = Database() user = db.get_user(username, password) if user is None: flash('Invalid Credentials.', 'danger') return redirect('/error') else: for key in user.keys(): flask_session[key] = user[key] flash('Successfully logged in', "success") db.update_user_question_guess(user['user_id'], 3) return redirect('/session/viewall') else: redirect_to_referrer()