# -*- 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)

[((238, 253), 'flask.Flask', 'Flask', (['__name__'], {}), '(__name__)\n', (243, 253), False, 'from flask import Flask, send_from_directory\n'), ((319, 380), 're.compile', 're.compile', (['"""(.*\\\\.js|.*\\\\.html|.*\\\\.png|.*\\\\.css|.*\\\\.map)$"""'], {}), "('(.*\\\\.js|.*\\\\.html|.*\\\\.png|.*\\\\.css|.*\\\\.map)$')\n", (329, 380), False, 'import re\n'), ((398, 412), 'random.seed', 'random.seed', (['(7)'], {}), '(7)\n', (409, 412), False, 'import random\n'), ((742, 752), 'flask.abort', 'abort', (['(403)'], {}), '(403)\n', (747, 752), False, 'from flask import request, abort\n'), ((832, 857), 'flask.request.args.get', 'request.args.get', (['"""email"""'], {}), "('email')\n", (848, 857), False, 'from flask import request, abort\n'), ((942, 973), 'datab.social_database.register_or_get_email', 'db.register_or_get_email', (['email'], {}), '(email)\n', (966, 973), True, 'import datab.social_database as db\n'), ((985, 1012), 'json.dumps', 'json.dumps', (["{'id': id_user}"], {}), "({'id': id_user})\n", (995, 1012), False, 'import json\n'), ((1071, 1098), 'flask.request.args.get', 'request.args.get', (['"""room_id"""'], {}), "('room_id')\n", (1087, 1098), False, 'from flask import request, abort\n'), ((1111, 1136), 'flask.request.args.get', 'request.args.get', (['"""email"""'], {}), "('email')\n", (1127, 1136), False, 'from flask import request, abort\n'), ((1151, 1182), 'datab.social_database.register_or_get_email', 'db.register_or_get_email', (['email'], {}), '(email)\n', (1175, 1182), True, 'import datab.social_database as db\n'), ((1187, 1287), 'datab.social_database.exec_query', 'db.exec_query', (['"""REPLACE INTO room_members (room_id, user_id) VALUES (%s,%s)"""', '[room_id, user_id]'], {}), "('REPLACE INTO room_members (room_id, user_id) VALUES (%s,%s)',\n    [room_id, user_id])\n", (1200, 1287), True, 'import datab.social_database as db\n'), ((1295, 1322), 'json.dumps', 'json.dumps', (["{'id': user_id}"], {}), "({'id': user_id})\n", (1305, 1322), False, 'import json\n'), ((1389, 1416), 'flask.request.args.get', 'request.args.get', (['"""room_id"""'], {}), "('room_id')\n", (1405, 1416), False, 'from flask import request, abort\n'), ((1431, 1458), 'flask.request.args.get', 'request.args.get', (['"""user_id"""'], {}), "('user_id')\n", (1447, 1458), False, 'from flask import request, abort\n'), ((1472, 1630), 'datab.social_database.exec_query', '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]'], {}), "(\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])\n", (1485, 1630), True, 'import datab.social_database as db\n'), ((1821, 1846), 'flask.request.args.get', 'request.args.get', (['"""email"""'], {}), "('email')\n", (1837, 1846), False, 'from flask import request, abort\n'), ((1861, 1892), 'datab.social_database.register_or_get_email', 'db.register_or_get_email', (['email'], {}), '(email)\n', (1885, 1892), True, 'import datab.social_database as db\n'), ((1904, 1931), 'json.dumps', 'json.dumps', (["{'id': id_user}"], {}), "({'id': id_user})\n", (1914, 1931), False, 'import json\n'), ((1994, 2021), 'flask.request.args.get', 'request.args.get', (['"""user_id"""'], {}), "('user_id')\n", (2010, 2021), False, 'from flask import request, abort\n'), ((2036, 2102), 'datab.social_database.exec_query', 'db.exec_query', (['"""INSERT INTO room (creator) VALUES (%s)"""', '[user_id]'], {}), "('INSERT INTO room (creator) VALUES (%s)', [user_id])\n", (2049, 2102), True, 'import datab.social_database as db\n'), ((2114, 2141), 'json.dumps', 'json.dumps', (["{'id': room_id}"], {}), "({'id': room_id})\n", (2124, 2141), False, 'import json\n'), ((2200, 2227), 'flask.request.args.get', 'request.args.get', (['"""user_id"""'], {}), "('user_id')\n", (2216, 2227), False, 'from flask import request, abort\n'), ((2241, 2326), 'datab.social_database.exec_query', 'db.exec_query', (['"""SELECT r.id, r.status FROM room r WHERE r.creator=%s"""', '[user_id]'], {}), "('SELECT r.id, r.status FROM room r WHERE r.creator=%s', [user_id]\n    )\n", (2254, 2326), True, 'import datab.social_database as db\n'), ((2432, 2452), 'json.dumps', 'json.dumps', (['response'], {}), '(response)\n', (2442, 2452), False, 'import json\n'), ((2531, 2549), 'flask.request.get_json', 'request.get_json', ([], {}), '()\n', (2547, 2549), False, 'from flask import request, abort\n'), ((2961, 2988), 'flask.request.args.get', 'request.args.get', (['"""room_id"""'], {}), "('room_id')\n", (2977, 2988), False, 'from flask import request, abort\n'), ((3012, 3097), 'datab.social_database.exec_query', 'db.exec_query', (['"""UPDATE room r SET r.status=\'started\' WHERE r.id = %s"""', '[room_id]'], {}), '("UPDATE room r SET r.status=\'started\' WHERE r.id = %s", [room_id]\n    )\n', (3025, 3097), True, 'import datab.social_database as db\n'), ((3104, 3190), 'json.dumps', 'json.dumps', (["{'info': 'The room has been opened successfully', 'status': 'started'}"], {}), "({'info': 'The room has been opened successfully', 'status':\n    'started'})\n", (3114, 3190), False, 'import json\n'), ((3247, 3274), 'flask.request.args.get', 'request.args.get', (['"""room_id"""'], {}), "('room_id')\n", (3263, 3274), False, 'from flask import request, abort\n'), ((3279, 3364), 'datab.social_database.exec_query', 'db.exec_query', (['"""UPDATE room  r SET r.status=\'closed\' WHERE r.id = %s"""', '[room_id]'], {}), '("UPDATE room  r SET r.status=\'closed\' WHERE r.id = %s", [room_id]\n    )\n', (3292, 3364), True, 'import datab.social_database as db\n'), ((3371, 3456), 'json.dumps', 'json.dumps', (["{'info': 'The room has been closed successfully', 'status': 'closed'}"], {}), "({'info': 'The room has been closed successfully', 'status':\n    'closed'})\n", (3381, 3456), False, 'import json\n'), ((3515, 3542), 'flask.request.args.get', 'request.args.get', (['"""room_id"""'], {}), "('room_id')\n", (3531, 3542), False, 'from flask import request, abort\n'), ((3547, 3634), 'datab.social_database.exec_query', 'db.exec_query', (['"""UPDATE room  r SET r.status=\'finished\' WHERE r.id = %s"""', '[room_id]'], {}), '("UPDATE room  r SET r.status=\'finished\' WHERE r.id = %s", [\n    room_id])\n', (3560, 3634), True, 'import datab.social_database as db\n'), ((3718, 4013), 'datab.social_database.exec_query', '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]'], {}), "(\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])\n", (3731, 4013), True, 'import datab.social_database as db\n'), ((4324, 4356), 'json.dumps', 'json.dumps', (["{'ranking': ranking}"], {}), "({'ranking': ranking})\n", (4334, 4356), False, 'import json\n'), ((4419, 4446), 'flask.request.args.get', 'request.args.get', (['"""room_id"""'], {}), "('room_id')\n", (4435, 4446), False, 'from flask import request, abort\n'), ((4503, 4568), 'datab.social_database.exec_query', 'db.exec_query', (['"""SELECT status FROM room WHERE id = %s"""', '[room_id]'], {}), "('SELECT status FROM room WHERE id = %s', [room_id])\n", (4516, 4568), True, 'import datab.social_database as db\n'), ((4580, 4616), 'json.dumps', 'json.dumps', (["{'status': values[0][0]}"], {}), "({'status': values[0][0]})\n", (4590, 4616), False, 'import json\n'), ((4706, 4733), 'flask.request.args.get', 'request.args.get', (['"""room_id"""'], {}), "('room_id')\n", (4722, 4733), False, 'from flask import request, abort\n'), ((4747, 4832), 'datab.social_database.exec_query', 'db.exec_query', (['"""SELECT id, question FROM question WHERE room_id = %s"""', '[room_id]'], {}), "('SELECT id, question FROM question WHERE room_id = %s', [room_id]\n    )\n", (4760, 4832), True, 'import datab.social_database as db\n'), ((4938, 4973), 'json.dumps', 'json.dumps', (["{'questions': response}"], {}), "({'questions': response})\n", (4948, 4973), False, 'import json\n'), ((5068, 5086), 'flask.request.get_json', 'request.get_json', ([], {}), '()\n', (5084, 5086), False, 'from flask import request, abort\n'), ((5354, 5459), 'datab.social_database.exec_many_query', 'db.exec_many_query', (['"""INSERT INTO answer (question_id, user_id, answer) VALUES(%s,%s,%s)"""', 'values'], {}), "(\n    'INSERT INTO answer (question_id, user_id, answer) VALUES(%s,%s,%s)',\n    values)\n", (5372, 5459), True, 'import datab.social_database as db\n'), ((5462, 5499), 'json.dumps', 'json.dumps', (["{'info': 'Data received'}"], {}), "({'info': 'Data received'})\n", (5472, 5499), False, 'import json\n'), ((5675, 5722), 'datab.social_database.get_non_answered_questions', 'db.get_non_answered_questions', (['room_id', 'user_id'], {}), '(room_id, user_id)\n', (5704, 5722), True, 'import datab.social_database as db\n'), ((5751, 5795), 'datab.social_database.get_non_answered_people', 'db.get_non_answered_people', (['room_id', 'user_id'], {}), '(room_id, user_id)\n', (5777, 5795), True, 'import datab.social_database as db\n'), ((8731, 8767), 'flask.request.args.get', 'request.args.get', (['"""quiz_question_id"""'], {}), "('quiz_question_id')\n", (8747, 8767), False, 'from flask import request, abort\n'), ((8789, 8823), 'flask.request.args.get', 'request.args.get', (['"""quiz_answer_id"""'], {}), "('quiz_answer_id')\n", (8805, 8823), False, 'from flask import request, abort\n'), ((8829, 8942), 'datab.social_database.exec_query', 'db.exec_query', (['"""UPDATE quiz_question SET answered_id = %s WHERE id = %s"""', '[quiz_answer_id, quiz_question_id]'], {}), "('UPDATE quiz_question SET answered_id = %s WHERE id = %s', [\n    quiz_answer_id, quiz_question_id])\n", (8842, 8942), True, 'import datab.social_database as db\n'), ((8951, 9097), 'datab.social_database.exec_query', '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]'], {}), "(\n    'SELECT qq.answered_id, qq.correct_answer_id, qq.question_id  FROM quiz_question qq WHERE qq.id = %s'\n    , [quiz_question_id])\n", (8964, 9097), True, 'import datab.social_database as db\n'), ((9256, 9345), 'datab.social_database.exec_query', 'db.exec_query', (['"""SELECT a.answer FROM answer a WHERE a.id = %s """', '[correct_answer_id]'], {}), "('SELECT a.answer FROM answer a WHERE a.id = %s ', [\n    correct_answer_id])\n", (9269, 9345), True, 'import datab.social_database as db\n'), ((9536, 9681), 'json.dumps', 'json.dumps', (["{'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}})\n", (9546, 9681), False, 'import json\n'), ((9752, 9766), 'flaskrun.flaskrun.flask_run', 'flask_run', (['app'], {}), '(app)\n', (9761, 9766), False, 'from flaskrun.flaskrun import flask_run\n'), ((912, 926), 'json.dumps', 'json.dumps', (['{}'], {}), '({})\n', (922, 926), False, 'import json\n'), ((2591, 2629), 'json.dumps', 'json.dumps', (["{'error': 'no JSON found'}"], {}), "({'error': 'no JSON found'})\n", (2601, 2629), False, 'import json\n'), ((2865, 2902), 'json.dumps', 'json.dumps', (["{'info': 'Data received'}"], {}), "({'info': 'Data received'})\n", (2875, 2902), False, 'import json\n'), ((5573, 5600), 'flask.request.args.get', 'request.args.get', (['"""room_id"""'], {}), "('room_id')\n", (5589, 5600), False, 'from flask import request, abort\n'), ((5620, 5647), 'flask.request.args.get', 'request.args.get', (['"""user_id"""'], {}), "('user_id')\n", (5636, 5647), False, 'from flask import request, abort\n'), ((5901, 5937), 'random.sample', 'random.sample', (['possible_questions', '(1)'], {}), '(possible_questions, 1)\n', (5914, 5937), False, 'import random\n'), ((5977, 6006), 'datab.social_database.get_all_questions', 'db.get_all_questions', (['room_id'], {}), '(room_id)\n', (5997, 6006), True, 'import datab.social_database as db\n'), ((6174, 6213), 'random.sample', 'random.sample', (['possible_users_to_ask', '(1)'], {}), '(possible_users_to_ask, 1)\n', (6187, 6213), False, 'import random\n'), ((6256, 6301), 'datab.social_database.get_all_different_people', 'db.get_all_different_people', (['room_id', 'user_id'], {}), '(room_id, user_id)\n', (6283, 6301), True, 'import datab.social_database as db\n'), ((6496, 6563), 'datab.social_database.insert_quiz_question', 'db.insert_quiz_question', (['user_id', 'asked_about_id[0]', 'question_id[0]'], {}), '(user_id, asked_about_id[0], question_id[0])\n', (6519, 6563), True, 'import datab.social_database as db\n'), ((6587, 6642), 'datab.social_database.get_all_different_people', 'db.get_all_different_people', (['room_id', 'asked_about_id[0]'], {}), '(room_id, asked_about_id[0])\n', (6614, 6642), True, 'import datab.social_database as db\n'), ((6651, 6678), 'random.shuffle', 'random.shuffle', (['other_users'], {}), '(other_users)\n', (6665, 6678), False, 'import random\n'), ((6732, 6780), 'datab.social_database.get_answer', 'db.get_answer', (['question_id[0]', 'asked_about_id[0]'], {}), '(question_id[0], asked_about_id[0])\n', (6745, 6780), True, 'import datab.social_database as db\n'), ((6790, 6901), 'datab.social_database.exec_query', 'db.exec_query', (['"""UPDATE quiz_question SET correct_answer_id=%s WHERE id = %s"""', '[answer_id, quiz_question_id]'], {}), "('UPDATE quiz_question SET correct_answer_id=%s WHERE id = %s',\n    [answer_id, quiz_question_id])\n", (6803, 6901), True, 'import datab.social_database as db\n'), ((7286, 7309), 'random.shuffle', 'random.shuffle', (['answers'], {}), '(answers)\n', (7300, 7309), False, 'import random\n'), ((7556, 7726), 'datab.social_database.exec_query', '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]]'], {}), "(\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]])\n", (7569, 7726), True, 'import datab.social_database as db\n'), ((7869, 7958), 'datab.social_database.exec_query', 'db.exec_query', (['"""SELECT q.question FROM question q WHERE q.id = %s"""', '[question_id[0]]'], {}), "('SELECT q.question FROM question q WHERE q.id = %s', [\n    question_id[0]])\n", (7882, 7958), True, 'import datab.social_database as db\n'), ((8104, 8183), 'datab.social_database.exec_query', 'db.exec_query', (['"""SELECT u.email FROM users u WHERE u.id=%s"""', '[asked_about_id[0]]'], {}), "('SELECT u.email FROM users u WHERE u.id=%s', [asked_about_id[0]])\n", (8117, 8183), True, 'import datab.social_database as db\n'), ((8413, 8504), 'json.dumps', 'json.dumps', (["{'id': quiz_question_id, 'question': question_text, 'answers': answer_json}"], {}), "({'id': quiz_question_id, 'question': question_text, 'answers':\n    answer_json})\n", (8423, 8504), False, 'import json\n'), ((8579, 8654), 'json.dumps', 'json.dumps', (["{'error': 'Not available questions for this user in this room'}"], {}), "({'error': 'Not available questions for this user in this room'})\n", (8589, 8654), False, 'import json\n'), ((9478, 9524), 'json.dumps', 'json.dumps', (["{'error': 'Internal server error'}"], {}), "({'error': 'Internal server error'})\n", (9488, 9524), False, 'import json\n'), ((2761, 2853), 'datab.social_database.exec_query', 'db.exec_query', (['"""INSERT INTO question (room_id, question) VALUES (%s, %s)"""', '[room_id, q]'], {}), "('INSERT INTO question (room_id, question) VALUES (%s, %s)', [\n    room_id, q])\n", (2774, 2853), True, 'import datab.social_database as db\n'), ((5128, 5166), 'json.dumps', 'json.dumps', (["{'error': 'no JSON found'}"], {}), "({'error': 'no JSON found'})\n", (5138, 5166), False, 'import json\n'), ((6073, 6109), 'random.sample', 'random.sample', (['possible_questions', '(1)'], {}), '(possible_questions, 1)\n', (6086, 6109), False, 'import random\n'), ((6371, 6410), 'random.sample', 'random.sample', (['possible_users_to_ask', '(1)'], {}), '(possible_users_to_ask, 1)\n', (6384, 6410), False, 'import random\n'), ((7114, 7159), 'datab.social_database.get_answer', 'db.get_answer', (['question_id[0]', 'other_users[i]'], {}), '(question_id[0], other_users[i])\n', (7127, 7159), True, 'import datab.social_database as db\n')]