def create_quiz(): db_connection = database.create_database_connection() try: body = request.get_json() user_id = extract_field_from_body('user_id', body) short_url = extract_field_from_body('short_url', body) question = extract_field_from_body('question', body) answers_target = extract_field_from_body('answers_target', body) quiz_id = database.insert_quiz(db_connection, user_id, short_url, question, answers_target) except: return Response("Bad request.", status=400, mimetype='application/json') finally: database.close_connection(db_connection) return Response(json.dumps({"id": quiz_id}), status=201, mimetype='application/json')
def get_user(): db_connection = database.create_database_connection() try: body = request.get_json() email = extract_field_from_body('email', body) user = database.get_user_by_email(db_connection, email) data = { 'id': user[0], 'email': user[1], 'password': user[2], 'activated': user[3], 'nickname': user[4], 'created_date': user[5] } except: return Response("Bad request.", status=400, mimetype='application/json') finally: database.close_connection(db_connection) return Response(json.dumps(data), status=201, mimetype='application/json')
def create_user(): try: body = request.get_json() email = extract_field_from_body('email', body) password = extract_field_from_body('password', body) nickname = extract_field_from_body('nickname', body) if email == None: raise Exception('Field email is missing.') if password == None: raise Exception('Field password is missing.') if nickname == None: raise Exception('Field nickname is missing.') data = { 'email': email, 'password': security.hash_password(password), 'nickname': nickname } code, res = io_service.post('/users', data) if code == 201: activation_token = generate_activation_token() data = {'user_id': res['id'], 'token': activation_token} io_service.post('/activation_tokens', data) data = {'email': email, 'token': activation_token} email_service.get('/', data) return Response(json.dumps(res), status=201, mimetype='application/json') return Response('Bad request.', status=400, mimetype='application/json') except Exception as e: return Response(str(e), status=400, mimetype='application/json')
def create_activation_token(): db_connection = database.create_database_connection() try: body = request.get_json() user_id = extract_field_from_body('user_id', body) token = extract_field_from_body('token', body) activation_token_id = database.insert_activation_token( db_connection, user_id, token) except: return Response("Bad request.", status=400, mimetype='application/json') finally: database.close_connection(db_connection) return Response(json.dumps({"id": activation_token_id}), status=201, mimetype='application/json')
def create_user(): db_connection = database.create_database_connection() try: body = request.get_json() email = extract_field_from_body('email', body) password = extract_field_from_body('password', body) nickname = extract_field_from_body('nickname', body) user_id = database.insert_user( db_connection, email, password, nickname) except: return Response("Bad request.", status=400, mimetype='application/json') finally: database.close_connection(db_connection) return Response(json.dumps({"id": user_id}), status=201, mimetype='application/json')
def answer_quiz(quiz_id): db_connection = database.create_database_connection() try: body = request.get_json() user_id = extract_field_from_body('user_id', body) option_id = extract_field_from_body('option_id', body) database.answer(db_connection, quiz_id, user_id, option_id) database.result(db_connection, quiz_id) except: return Response("Bad request.", status=400, mimetype='application/json') finally: database.close_connection(db_connection) return Response('Answer added.', status=201, mimetype='application/json')
def create_options(): db_connection = database.create_database_connection() try: body = request.get_json() texts = extract_field_from_body('texts', body) quiz_id = extract_field_from_body('quiz_id', body) for text in texts: database.insert_option(db_connection, quiz_id, text) except: return Response("Bad request.", status=400, mimetype='application/json') finally: database.close_connection(db_connection) return Response(json.dumps({}), status=201, mimetype='application/json')
def create_quiz(): try: user = get_user(request) body = request.get_json() question = extract_field_from_body('question', body) short_url = extract_field_from_body('short_url', body) answers_target = extract_field_from_body('answers_target', body) texts = extract_field_from_body('texts', body) data = { 'user_id': user['id'], 'question': question, 'short_url': short_url, 'answers_target': answers_target } texts = list(filter(lambda text: text != "", texts)) if len(texts) < 2: raise Exception("Invalid number of options") _, res = io_quizzes_create_service.post('/quizzes', data) quiz_id = res['id'] data = { 'quiz_id': quiz_id, 'texts': texts } io_quizzes_create_service.post('/options', data) except Exception as e: return Response(str(e), status=400, mimetype='application/json') return Response(json.dumps({'id': quiz_id}), status=201, mimetype='application/json')
def auth(): try: body = request.get_json() token = extract_field_from_body('token', body) data = security.jwt_decode(token) return Response(json.dumps(data), status=200, mimetype='application/json') except Exception as e: return Response(str(e), status=401, mimetype='application/json')
def login(): try: body = request.get_json() email = extract_field_from_body('email', body) password = extract_field_from_body('password', body) data = {'email': email} _, user = io_service.get('/users', data) if not user['activated']: return Response('User is not activated.', status=400, mimetype='application/json') if not security.verify_password(user['password'], password): return Response('Incorrect password.', status=400, mimetype='application/json') data = { 'id': user['id'], 'email': user['email'], 'nickname': user['nickname'] } jwt_token = security.jwt_encode(data) data = {'token': jwt_token} return Response(json.dumps(data), status=200, mimetype='application/json') except Exception as e: return Response(str(e), status=400, mimetype='application/json')
def activate_user(): db_connection = database.create_database_connection() try: body = request.get_json() token = extract_field_from_body('token', body) database.activate_user(db_connection, token) except: return Response("Bad request.", status=400, mimetype='application/json') finally: database.close_connection(db_connection) return Response('User activated.', status=200, mimetype='application/json')
def get_quiz(id): db_connection = database.create_database_connection() try: body = request.get_json() user_id = extract_field_from_body('user_id', body) quiz = database.get_quiz(db_connection, id, user_id) except: return Response("Bad request.", status=400, mimetype='application/json') finally: database.close_connection(db_connection) return Response(json.dumps(quiz), status=200, mimetype='application/json')
def answer_quiz(quiz_id): try: user = get_user(request) body = request.get_json() option_id = extract_field_from_body('option_id', body) data = { 'user_id': user['id'], 'option_id': option_id } code, _ = io_quizzes_query_service.post( f'/quizzes/{quiz_id}/answer', data) if code == 201: return Response('Answer added.', status=201, mimetype='application/json') return Response('Bad request.', status=400, mimetype='application/json') except Exception as e: return Response(str(e), status=400, mimetype='application/json')