# -*- coding: utf-8 -*-

import json
import os.path
import random
import re

from flask import Flask, send_from_directory
from flask import request, abort

from flaskrun.flaskrun import flask_run
import datab.social_database as db

app = Flask(__name__)

# Regular expression to only accept certain files
fileChecker = re.compile(r"(.*\.js|.*\.html|.*\.png|.*\.css|.*\.map)$")
numberOfAnswers = 4

random.seed(7)


def root_dir():  # pragma: no cover
    return os.path.abspath(os.path.dirname(__file__))


@app.route('/')
def root():
    return index("index2.html")


@app.route('/<path:filename>')
def index(filename):
    if fileChecker.match(filename):
        return send_from_directory(os.path.join(root_dir(), 'static'), filename)
    abort(403)


@app.route('/register')
def register():
    # To obtain the mail
    email = request.args.get('email')
    print(email)
    if email is None:
        return json.dumps({})

    id_user = db.register_or_get_email(email)
    return json.dumps({"id": id_user})


@app.route('/join_room')
def join_room():
    room_id = request.args.get('room_id')
    email = request.args.get('email')
    user_id = db.register_or_get_email(email)
    db.exec_query("REPLACE INTO room_members (room_id, user_id) VALUES (%s,%s)", [room_id, user_id])
    return json.dumps({"id": user_id})


@app.route('/answered_room')
def answered_room():
    room_id = request.args.get('room_id')
    user_id = request.args.get('user_id')
    values = db.exec_query("SELECT a.id "
                           "FROM answer a INNER JOIN question q "
                           "WHERE a.question_id = q.id AND q.room_id = %s AND a.user_id= %s",
                           [room_id, user_id])
    return json.dumps({"answered": len(values) > 0})


@app.route('/get_user_id')
def get_user_id():
    email = request.args.get('email')
    id_user = db.register_or_get_email(email)
    return json.dumps({"id": id_user})


@app.route('/create_room')
def create_room():
    user_id = request.args.get('user_id')
    room_id = db.exec_query("INSERT INTO room (creator) VALUES (%s)", [user_id])
    return json.dumps({"id": room_id})


@app.route('/get_rooms')
def get_rooms():
    user_id = request.args.get('user_id')
    values = db.exec_query("SELECT r.id, r.status FROM room r WHERE r.creator=%s", [user_id])
    response = []
    for val in values:
        response.append({"id": val[0], "status": val[1]})
    return json.dumps(response)


@app.route('/fill_room', methods=['POST'])
def fill_room():
    json_data = request.get_json()
    if json_data is None:
        return json.dumps({"error": "no JSON found"})
    else:
        room_id = json_data["room_id"]
        questions = json_data["question"]
        for q in questions:
            db.exec_query("INSERT INTO question (room_id, question) VALUES (%s, %s)", [room_id, q])

        return json.dumps({"info": "Data received"})


@app.route('/open_room')
def open_room():
    room_id = request.args.get('room_id')
    print(room_id)
    db.exec_query("UPDATE room r SET r.status='started' WHERE r.id = %s", [room_id])
    return json.dumps({"info": "The room has been opened successfully", "status": "started"})


@app.route('/close_room')
def close_room():
    room_id = request.args.get('room_id')
    db.exec_query("UPDATE room  r SET r.status='closed' WHERE r.id = %s", [room_id])
    return json.dumps({"info": "The room has been closed successfully", "status": "closed"})


@app.route('/finish_room')
def finish_room():
    room_id = request.args.get('room_id')
    db.exec_query("UPDATE room  r SET r.status='finished' WHERE r.id = %s", [room_id])
    # for
    # SELECT id, COUNT(a.id), COUNT(a.id) FROM Room r INNER JOIN
    values = db.exec_query("SELECT u.email , COUNT(qq.id) "
                           "FROM quiz_question qq "
                           "INNER JOIN users u ON (qq.asked_user_id = u.id) "
                           "INNER JOIN room_members rm ON (u.id = rm.user_id) "
                           "WHERE qq.correct_answer_id = qq.answered_id AND rm.room_id = %s "
                           "GROUP BY u.email "
                           "ORDER BY COUNT(qq.id) DESC",
                           [room_id])
    ranking = []
    for row in values:
        ranking.append({"email": row[0], "correct": row[1]})

    return json.dumps({"ranking": ranking})


@app.route('/room_status')
def status_room():
    room_id = request.args.get('room_id')
    # SELECT status FROM Room WHERE id = 1
    values = db.exec_query("SELECT status FROM room WHERE id = %s", [room_id])
    return json.dumps({
        "status": values[0][0]
    })


@app.route('/get_room_questions')
def get_room_question():
    room_id = request.args.get('room_id')
    values = db.exec_query("SELECT id, question FROM question WHERE room_id = %s", [room_id])

    response = []
    for val in values:
        response.append({"id": val[0], "text": val[1]})

    return json.dumps({"questions": response})


@app.route('/post_room_answers', methods=['POST'])
def post_room_answers():
    json_data = request.get_json()
    if json_data is None:
        return json.dumps({"error": "no JSON found"}), 404
    user_id = json_data["user_id"]
    values = []
    for a in json_data["answers"]:
        values.append((a["id"], user_id, a["text"]))
        print(values[len(values) - 1])
    db.exec_many_query("INSERT INTO answer (question_id, user_id, answer) VALUES(%s,%s,%s)", values)
    return json.dumps({"info": "Data received"})


@app.route('/get_quiz_question')
def get_question():
    room_id = int(request.args.get('room_id'))
    user_id = int(request.args.get('user_id'))

    possible_questions = db.get_non_answered_questions(room_id, user_id)
    possible_users_to_ask = db.get_non_answered_people(room_id, user_id)

    question_id = []
    asked_about_id = []

    if len(possible_questions) > 0:
        question_id = random.sample(possible_questions, 1)
    else:
        possible_questions = db.get_all_questions(room_id)
        if len(possible_questions) > 0:
            question_id = random.sample(possible_questions, 1)
    if len(possible_users_to_ask) > 0:
        asked_about_id = random.sample(possible_users_to_ask, 1)
    else:
        possible_users_to_ask = db.get_all_different_people(room_id, user_id)
        if len(possible_questions) > 0:
            asked_about_id = random.sample(possible_users_to_ask, 1)

    if len(question_id) > 0 and 0 < len(asked_about_id):
        quiz_question_id = db.insert_quiz_question(user_id, asked_about_id[0], question_id[0])

        other_users = db.get_all_different_people(room_id, asked_about_id[0])
        random.shuffle(other_users)

        answers = []
        (answer_id, text_id) = db.get_answer(question_id[0], asked_about_id[0])

        db.exec_query("UPDATE quiz_question SET correct_answer_id=%s WHERE id = %s", [answer_id, quiz_question_id])

        answers.append((answer_id, text_id))
        if min(numberOfAnswers - 1, len(other_users)) > 0:
            for i in range(min(numberOfAnswers - 1, len(other_users))):
                (answer_id, text_id) = db.get_answer(question_id[0], other_users[i])
                answers.append((answer_id, text_id))

        # if commented the first answer will be the correct one
        random.shuffle(answers)

        answer_json = []
        for (answer_id, text_id) in answers:
            answer_json.append({"id": answer_id, "text": text_id})

        print(quiz_question_id)
        # SELECT 'question' FROM 'Question' WHERE 'id' = 3
        value = db.exec_query("SELECT id "
                              "FROM quiz_question "
                              "WHERE asked_user_id = %s AND about_user_id = %s AND question_id = %s",
                              [user_id, asked_about_id[0], question_id[0]])
        quiz_question_id = value[0][0]

        value = db.exec_query("SELECT q.question "
                              "FROM question q "
                              "WHERE q.id = %s",
                              [question_id[0]])

        question_text = value[0][0]

        value = db.exec_query("SELECT u.email "
                              "FROM users u "
                              "WHERE u.id=%s",
                              [asked_about_id[0]])
        user_name = value[0][0]

        question_text = "What did %s answer to '%s' ?" % (user_name, question_text)

        return json.dumps({
            "id":       quiz_question_id,
            "question": question_text,
            "answers":  answer_json
        })
    else:
        return json.dumps({"error": "Not available questions for this user in this room"})


@app.route('/post_quiz_answer')
def post_answer():
    quiz_question_id = request.args.get('quiz_question_id')
    quiz_answer_id = request.args.get('quiz_answer_id')

    db.exec_query("UPDATE quiz_question SET answered_id = %s WHERE id = %s", [quiz_answer_id, quiz_question_id])

    value = db.exec_query("SELECT qq.answered_id, qq.correct_answer_id, qq.question_id  "
                          "FROM quiz_question qq "
                          "WHERE qq.id = %s", [quiz_question_id])

    answered_id = value[0][0]
    correct_answer_id = value[0][1]
    question_id = value[0][2]

    value = db.exec_query("SELECT a.answer FROM answer a WHERE a.id = %s ", [correct_answer_id])

    if len(value) > 0:
        text = value[0][0]
    else:
        text = "something when wrong"

    if value is None:
        return json.dumps({"error": "Internal server error"})
    return json.dumps({
        "correct":       answered_id == correct_answer_id,
        "question":      question_id,
        "correct_answer": {"id": correct_answer_id, "text": text}
    })


if __name__ == '__main__':
    flask_run(app)

[((14, 6, 14, 21), 'flask.Flask', 'Flask', ({(14, 12, 14, 20): '__name__'}, {}), '(__name__)', False, 'from flask import Flask, send_from_directory\n'), ((17, 14, 17, 71), 're.compile', 're.compile', ({(17, 25, 17, 70): '"""(.*\\\\.js|.*\\\\.html|.*\\\\.png|.*\\\\.css|.*\\\\.map)$"""'}, {}), "('(.*\\\\.js|.*\\\\.html|.*\\\\.png|.*\\\\.css|.*\\\\.map)$')", False, 'import re\n'), ((20, 0, 20, 14), 'random.seed', 'random.seed', ({(20, 12, 20, 13): '(7)'}, {}), '(7)', False, 'import random\n'), ((36, 4, 36, 14), 'flask.abort', 'abort', ({(36, 10, 36, 13): '(403)'}, {}), '(403)', False, 'from flask import request, abort\n'), ((42, 12, 42, 37), 'flask.request.args.get', 'request.args.get', ({(42, 29, 42, 36): '"""email"""'}, {}), "('email')", False, 'from flask import request, abort\n'), ((47, 14, 47, 45), 'datab.social_database.register_or_get_email', 'db.register_or_get_email', ({(47, 39, 47, 44): 'email'}, {}), '(email)', True, 'import datab.social_database as db\n'), ((48, 11, 48, 38), 'json.dumps', 'json.dumps', ({(48, 22, 48, 37): "{'id': id_user}"}, {}), "({'id': id_user})", False, 'import json\n'), ((53, 14, 53, 41), 'flask.request.args.get', 'request.args.get', ({(53, 31, 53, 40): '"""room_id"""'}, {}), "('room_id')", False, 'from flask import request, abort\n'), ((54, 12, 54, 37), 'flask.request.args.get', 'request.args.get', ({(54, 29, 54, 36): '"""email"""'}, {}), "('email')", False, 'from flask import request, abort\n'), ((55, 14, 55, 45), 'datab.social_database.register_or_get_email', 'db.register_or_get_email', ({(55, 39, 55, 44): 'email'}, {}), '(email)', True, 'import datab.social_database as db\n'), ((56, 4, 56, 100), 'datab.social_database.exec_query', 'db.exec_query', ({(56, 18, 56, 79): '"""REPLACE INTO room_members (room_id, user_id) VALUES (%s,%s)"""', (56, 81, 56, 99): '[room_id, user_id]'}, {}), "('REPLACE INTO room_members (room_id, user_id) VALUES (%s,%s)',\n    [room_id, user_id])", True, 'import datab.social_database as db\n'), ((57, 11, 57, 38), 'json.dumps', 'json.dumps', ({(57, 22, 57, 37): "{'id': user_id}"}, {}), "({'id': user_id})", False, 'import json\n'), ((62, 14, 62, 41), 'flask.request.args.get', 'request.args.get', ({(62, 31, 62, 40): '"""room_id"""'}, {}), "('room_id')", False, 'from flask import request, abort\n'), ((63, 14, 63, 41), 'flask.request.args.get', 'request.args.get', ({(63, 31, 63, 40): '"""user_id"""'}, {}), "('user_id')", False, 'from flask import request, abort\n'), ((64, 13, 67, 46), 'datab.social_database.exec_query', 'db.exec_query', ({(64, 27, 66, 92): '"""SELECT a.id FROM answer a INNER JOIN question q WHERE a.question_id = q.id AND q.room_id = %s AND a.user_id= %s"""', (67, 27, 67, 45): '[room_id, user_id]'}, {}), "(\n    'SELECT a.id FROM answer a INNER JOIN question q WHERE a.question_id = q.id AND q.room_id = %s AND a.user_id= %s'\n    , [room_id, user_id])", True, 'import datab.social_database as db\n'), ((73, 12, 73, 37), 'flask.request.args.get', 'request.args.get', ({(73, 29, 73, 36): '"""email"""'}, {}), "('email')", False, 'from flask import request, abort\n'), ((74, 14, 74, 45), 'datab.social_database.register_or_get_email', 'db.register_or_get_email', ({(74, 39, 74, 44): 'email'}, {}), '(email)', True, 'import datab.social_database as db\n'), ((75, 11, 75, 38), 'json.dumps', 'json.dumps', ({(75, 22, 75, 37): "{'id': id_user}"}, {}), "({'id': id_user})", False, 'import json\n'), ((80, 14, 80, 41), 'flask.request.args.get', 'request.args.get', ({(80, 31, 80, 40): '"""user_id"""'}, {}), "('user_id')", False, 'from flask import request, abort\n'), ((81, 14, 81, 80), 'datab.social_database.exec_query', 'db.exec_query', ({(81, 28, 81, 68): '"""INSERT INTO room (creator) VALUES (%s)"""', (81, 70, 81, 79): '[user_id]'}, {}), "('INSERT INTO room (creator) VALUES (%s)', [user_id])", True, 'import datab.social_database as db\n'), ((82, 11, 82, 38), 'json.dumps', 'json.dumps', ({(82, 22, 82, 37): "{'id': room_id}"}, {}), "({'id': room_id})", False, 'import json\n'), ((87, 14, 87, 41), 'flask.request.args.get', 'request.args.get', ({(87, 31, 87, 40): '"""user_id"""'}, {}), "('user_id')", False, 'from flask import request, abort\n'), ((88, 13, 88, 93), 'datab.social_database.exec_query', 'db.exec_query', ({(88, 27, 88, 81): '"""SELECT r.id, r.status FROM room r WHERE r.creator=%s"""', (88, 83, 88, 92): '[user_id]'}, {}), "('SELECT r.id, r.status FROM room r WHERE r.creator=%s', [user_id]\n    )", True, 'import datab.social_database as db\n'), ((92, 11, 92, 31), 'json.dumps', 'json.dumps', ({(92, 22, 92, 30): 'response'}, {}), '(response)', False, 'import json\n'), ((97, 16, 97, 34), 'flask.request.get_json', 'request.get_json', ({}, {}), '()', False, 'from flask import request, abort\n'), ((111, 14, 111, 41), 'flask.request.args.get', 'request.args.get', ({(111, 31, 111, 40): '"""room_id"""'}, {}), "('room_id')", False, 'from flask import request, abort\n'), ((113, 4, 113, 84), 'datab.social_database.exec_query', 'db.exec_query', ({(113, 18, 113, 72): '"""UPDATE room r SET r.status=\'started\' WHERE r.id = %s"""', (113, 74, 113, 83): '[room_id]'}, {}), '("UPDATE room r SET r.status=\'started\' WHERE r.id = %s", [room_id]\n    )', True, 'import datab.social_database as db\n'), ((114, 11, 114, 93), 'json.dumps', 'json.dumps', ({(114, 22, 114, 92): "{'info': 'The room has been opened successfully', 'status': 'started'}"}, {}), "({'info': 'The room has been opened successfully', 'status':\n    'started'})", False, 'import json\n'), ((119, 14, 119, 41), 'flask.request.args.get', 'request.args.get', ({(119, 31, 119, 40): '"""room_id"""'}, {}), "('room_id')", False, 'from flask import request, abort\n'), ((120, 4, 120, 84), 'datab.social_database.exec_query', 'db.exec_query', ({(120, 18, 120, 72): '"""UPDATE room  r SET r.status=\'closed\' WHERE r.id = %s"""', (120, 74, 120, 83): '[room_id]'}, {}), '("UPDATE room  r SET r.status=\'closed\' WHERE r.id = %s", [room_id]\n    )', True, 'import datab.social_database as db\n'), ((121, 11, 121, 92), 'json.dumps', 'json.dumps', ({(121, 22, 121, 91): "{'info': 'The room has been closed successfully', 'status': 'closed'}"}, {}), "({'info': 'The room has been closed successfully', 'status':\n    'closed'})", False, 'import json\n'), ((126, 14, 126, 41), 'flask.request.args.get', 'request.args.get', ({(126, 31, 126, 40): '"""room_id"""'}, {}), "('room_id')", False, 'from flask import request, abort\n'), ((127, 4, 127, 86), 'datab.social_database.exec_query', 'db.exec_query', ({(127, 18, 127, 74): '"""UPDATE room  r SET r.status=\'finished\' WHERE r.id = %s"""', (127, 76, 127, 85): '[room_id]'}, {}), '("UPDATE room  r SET r.status=\'finished\' WHERE r.id = %s", [\n    room_id])', True, 'import datab.social_database as db\n'), ((130, 13, 137, 37), 'datab.social_database.exec_query', 'db.exec_query', ({(130, 27, 136, 55): '"""SELECT u.email , COUNT(qq.id) FROM quiz_question qq INNER JOIN users u ON (qq.asked_user_id = u.id) INNER JOIN room_members rm ON (u.id = rm.user_id) WHERE qq.correct_answer_id = qq.answered_id AND rm.room_id = %s GROUP BY u.email ORDER BY COUNT(qq.id) DESC"""', (137, 27, 137, 36): '[room_id]'}, {}), "(\n    'SELECT u.email , COUNT(qq.id) FROM quiz_question qq INNER JOIN users u ON (qq.asked_user_id = u.id) INNER JOIN room_members rm ON (u.id = rm.user_id) WHERE qq.correct_answer_id = qq.answered_id AND rm.room_id = %s GROUP BY u.email ORDER BY COUNT(qq.id) DESC'\n    , [room_id])", True, 'import datab.social_database as db\n'), ((142, 11, 142, 43), 'json.dumps', 'json.dumps', ({(142, 22, 142, 42): "{'ranking': ranking}"}, {}), "({'ranking': ranking})", False, 'import json\n'), ((147, 14, 147, 41), 'flask.request.args.get', 'request.args.get', ({(147, 31, 147, 40): '"""room_id"""'}, {}), "('room_id')", False, 'from flask import request, abort\n'), ((149, 13, 149, 78), 'datab.social_database.exec_query', 'db.exec_query', ({(149, 27, 149, 66): '"""SELECT status FROM room WHERE id = %s"""', (149, 68, 149, 77): '[room_id]'}, {}), "('SELECT status FROM room WHERE id = %s', [room_id])", True, 'import datab.social_database as db\n'), ((150, 11, 152, 6), 'json.dumps', 'json.dumps', ({(150, 22, 152, 5): "{'status': values[0][0]}"}, {}), "({'status': values[0][0]})", False, 'import json\n'), ((157, 14, 157, 41), 'flask.request.args.get', 'request.args.get', ({(157, 31, 157, 40): '"""room_id"""'}, {}), "('room_id')", False, 'from flask import request, abort\n'), ((158, 13, 158, 93), 'datab.social_database.exec_query', 'db.exec_query', ({(158, 27, 158, 81): '"""SELECT id, question FROM question WHERE room_id = %s"""', (158, 83, 158, 92): '[room_id]'}, {}), "('SELECT id, question FROM question WHERE room_id = %s', [room_id]\n    )", True, 'import datab.social_database as db\n'), ((164, 11, 164, 46), 'json.dumps', 'json.dumps', ({(164, 22, 164, 45): "{'questions': response}"}, {}), "({'questions': response})", False, 'import json\n'), ((169, 16, 169, 34), 'flask.request.get_json', 'request.get_json', ({}, {}), '()', False, 'from flask import request, abort\n'), ((177, 4, 177, 100), 'datab.social_database.exec_many_query', 'db.exec_many_query', ({(177, 23, 177, 91): '"""INSERT INTO answer (question_id, user_id, answer) VALUES(%s,%s,%s)"""', (177, 93, 177, 99): 'values'}, {}), "(\n    'INSERT INTO answer (question_id, user_id, answer) VALUES(%s,%s,%s)',\n    values)", True, 'import datab.social_database as db\n'), ((178, 11, 178, 48), 'json.dumps', 'json.dumps', ({(178, 22, 178, 47): "{'info': 'Data received'}"}, {}), "({'info': 'Data received'})", False, 'import json\n'), ((186, 25, 186, 72), 'datab.social_database.get_non_answered_questions', 'db.get_non_answered_questions', ({(186, 55, 186, 62): 'room_id', (186, 64, 186, 71): 'user_id'}, {}), '(room_id, user_id)', True, 'import datab.social_database as db\n'), ((187, 28, 187, 72), 'datab.social_database.get_non_answered_people', 'db.get_non_answered_people', ({(187, 55, 187, 62): 'room_id', (187, 64, 187, 71): 'user_id'}, {}), '(room_id, user_id)', True, 'import datab.social_database as db\n'), ((263, 23, 263, 59), 'flask.request.args.get', 'request.args.get', ({(263, 40, 263, 58): '"""quiz_question_id"""'}, {}), "('quiz_question_id')", False, 'from flask import request, abort\n'), ((264, 21, 264, 55), 'flask.request.args.get', 'request.args.get', ({(264, 38, 264, 54): '"""quiz_answer_id"""'}, {}), "('quiz_answer_id')", False, 'from flask import request, abort\n'), ((266, 4, 266, 112), 'datab.social_database.exec_query', 'db.exec_query', ({(266, 18, 266, 75): '"""UPDATE quiz_question SET answered_id = %s WHERE id = %s"""', (266, 77, 266, 111): '[quiz_answer_id, quiz_question_id]'}, {}), "('UPDATE quiz_question SET answered_id = %s WHERE id = %s', [\n    quiz_answer_id, quiz_question_id])", True, 'import datab.social_database as db\n'), ((268, 12, 270, 65), 'datab.social_database.exec_query', 'db.exec_query', ({(268, 26, 270, 44): '"""SELECT qq.answered_id, qq.correct_answer_id, qq.question_id  FROM quiz_question qq WHERE qq.id = %s"""', (270, 46, 270, 64): '[quiz_question_id]'}, {}), "(\n    'SELECT qq.answered_id, qq.correct_answer_id, qq.question_id  FROM quiz_question qq WHERE qq.id = %s'\n    , [quiz_question_id])", True, 'import datab.social_database as db\n'), ((276, 12, 276, 96), 'datab.social_database.exec_query', 'db.exec_query', ({(276, 26, 276, 74): '"""SELECT a.answer FROM answer a WHERE a.id = %s """', (276, 76, 276, 95): '[correct_answer_id]'}, {}), "('SELECT a.answer FROM answer a WHERE a.id = %s ', [\n    correct_answer_id])", True, 'import datab.social_database as db\n'), ((285, 11, 289, 6), 'json.dumps', 'json.dumps', ({(285, 22, 289, 5): "{'correct': answered_id == correct_answer_id, 'question': question_id,\n    'correct_answer': {'id': correct_answer_id, 'text': text}}"}, {}), "({'correct': answered_id == correct_answer_id, 'question':\n    question_id, 'correct_answer': {'id': correct_answer_id, 'text': text}})", False, 'import json\n'), ((293, 4, 293, 18), 'flaskrun.flaskrun.flask_run', 'flask_run', ({(293, 14, 293, 17): 'app'}, {}), '(app)', False, 'from flaskrun.flaskrun import flask_run\n'), ((45, 15, 45, 29), 'json.dumps', 'json.dumps', ({(45, 26, 45, 28): '{}'}, {}), '({})', False, 'import json\n'), ((99, 15, 99, 53), 'json.dumps', 'json.dumps', ({(99, 26, 99, 52): "{'error': 'no JSON found'}"}, {}), "({'error': 'no JSON found'})", False, 'import json\n'), ((106, 15, 106, 52), 'json.dumps', 'json.dumps', ({(106, 26, 106, 51): "{'info': 'Data received'}"}, {}), "({'info': 'Data received'})", False, 'import json\n'), ((183, 18, 183, 45), 'flask.request.args.get', 'request.args.get', ({(183, 35, 183, 44): '"""room_id"""'}, {}), "('room_id')", False, 'from flask import request, abort\n'), ((184, 18, 184, 45), 'flask.request.args.get', 'request.args.get', ({(184, 35, 184, 44): '"""user_id"""'}, {}), "('user_id')", False, 'from flask import request, abort\n'), ((193, 22, 193, 58), 'random.sample', 'random.sample', ({(193, 36, 193, 54): 'possible_questions', (193, 56, 193, 57): '1'}, {}), '(possible_questions, 1)', False, 'import random\n'), ((195, 29, 195, 58), 'datab.social_database.get_all_questions', 'db.get_all_questions', ({(195, 50, 195, 57): 'room_id'}, {}), '(room_id)', True, 'import datab.social_database as db\n'), ((199, 25, 199, 64), 'random.sample', 'random.sample', ({(199, 39, 199, 60): 'possible_users_to_ask', (199, 62, 199, 63): '1'}, {}), '(possible_users_to_ask, 1)', False, 'import random\n'), ((201, 32, 201, 77), 'datab.social_database.get_all_different_people', 'db.get_all_different_people', ({(201, 60, 201, 67): 'room_id', (201, 69, 201, 76): 'user_id'}, {}), '(room_id, user_id)', True, 'import datab.social_database as db\n'), ((206, 27, 206, 94), 'datab.social_database.insert_quiz_question', 'db.insert_quiz_question', ({(206, 51, 206, 58): 'user_id', (206, 60, 206, 77): 'asked_about_id[0]', (206, 79, 206, 93): 'question_id[0]'}, {}), '(user_id, asked_about_id[0], question_id[0])', True, 'import datab.social_database as db\n'), ((208, 22, 208, 77), 'datab.social_database.get_all_different_people', 'db.get_all_different_people', ({(208, 50, 208, 57): 'room_id', (208, 59, 208, 76): 'asked_about_id[0]'}, {}), '(room_id, asked_about_id[0])', True, 'import datab.social_database as db\n'), ((209, 8, 209, 35), 'random.shuffle', 'random.shuffle', ({(209, 23, 209, 34): 'other_users'}, {}), '(other_users)', False, 'import random\n'), ((212, 31, 212, 79), 'datab.social_database.get_answer', 'db.get_answer', ({(212, 45, 212, 59): 'question_id[0]', (212, 61, 212, 78): 'asked_about_id[0]'}, {}), '(question_id[0], asked_about_id[0])', True, 'import datab.social_database as db\n'), ((214, 8, 214, 115), 'datab.social_database.exec_query', 'db.exec_query', ({(214, 22, 214, 83): '"""UPDATE quiz_question SET correct_answer_id=%s WHERE id = %s"""', (214, 85, 214, 114): '[answer_id, quiz_question_id]'}, {}), "('UPDATE quiz_question SET correct_answer_id=%s WHERE id = %s',\n    [answer_id, quiz_question_id])", True, 'import datab.social_database as db\n'), ((223, 8, 223, 31), 'random.shuffle', 'random.shuffle', ({(223, 23, 223, 30): 'answers'}, {}), '(answers)', False, 'import random\n'), ((231, 16, 234, 75), 'datab.social_database.exec_query', 'db.exec_query', ({(231, 30, 233, 100): '"""SELECT id FROM quiz_question WHERE asked_user_id = %s AND about_user_id = %s AND question_id = %s"""', (234, 30, 234, 74): '[user_id, asked_about_id[0], question_id[0]]'}, {}), "(\n    'SELECT id FROM quiz_question WHERE asked_user_id = %s AND about_user_id = %s AND question_id = %s'\n    , [user_id, asked_about_id[0], question_id[0]])", True, 'import datab.social_database as db\n'), ((237, 16, 240, 47), 'datab.social_database.exec_query', 'db.exec_query', ({(237, 30, 239, 47): '"""SELECT q.question FROM question q WHERE q.id = %s"""', (240, 30, 240, 46): '[question_id[0]]'}, {}), "('SELECT q.question FROM question q WHERE q.id = %s', [\n    question_id[0]])", True, 'import datab.social_database as db\n'), ((244, 16, 247, 50), 'datab.social_database.exec_query', 'db.exec_query', ({(244, 30, 246, 45): '"""SELECT u.email FROM users u WHERE u.id=%s"""', (247, 30, 247, 49): '[asked_about_id[0]]'}, {}), "('SELECT u.email FROM users u WHERE u.id=%s', [asked_about_id[0]])", True, 'import datab.social_database as db\n'), ((252, 15, 256, 10), 'json.dumps', 'json.dumps', ({(252, 26, 256, 9): "{'id': quiz_question_id, 'question': question_text, 'answers': answer_json}"}, {}), "({'id': quiz_question_id, 'question': question_text, 'answers':\n    answer_json})", False, 'import json\n'), ((258, 15, 258, 90), 'json.dumps', 'json.dumps', ({(258, 26, 258, 89): "{'error': 'Not available questions for this user in this room'}"}, {}), "({'error': 'Not available questions for this user in this room'})", False, 'import json\n'), ((284, 15, 284, 61), 'json.dumps', 'json.dumps', ({(284, 26, 284, 60): "{'error': 'Internal server error'}"}, {}), "({'error': 'Internal server error'})", False, 'import json\n'), ((104, 12, 104, 99), 'datab.social_database.exec_query', 'db.exec_query', ({(104, 26, 104, 84): '"""INSERT INTO question (room_id, question) VALUES (%s, %s)"""', (104, 86, 104, 98): '[room_id, q]'}, {}), "('INSERT INTO question (room_id, question) VALUES (%s, %s)', [\n    room_id, q])", True, 'import datab.social_database as db\n'), ((171, 15, 171, 53), 'json.dumps', 'json.dumps', ({(171, 26, 171, 52): "{'error': 'no JSON found'}"}, {}), "({'error': 'no JSON found'})", False, 'import json\n'), ((197, 26, 197, 62), 'random.sample', 'random.sample', ({(197, 40, 197, 58): 'possible_questions', (197, 60, 197, 61): '1'}, {}), '(possible_questions, 1)', False, 'import random\n'), ((203, 29, 203, 68), 'random.sample', 'random.sample', ({(203, 43, 203, 64): 'possible_users_to_ask', (203, 66, 203, 67): '1'}, {}), '(possible_users_to_ask, 1)', False, 'import random\n'), ((219, 39, 219, 84), 'datab.social_database.get_answer', 'db.get_answer', ({(219, 53, 219, 67): 'question_id[0]', (219, 69, 219, 83): 'other_users[i]'}, {}), '(question_id[0], other_users[i])', True, 'import datab.social_database as db\n')]