def __get_number_questions(): client = db_utils.connect() db = client[skill_config.DB_NAME] res = db.questions.count_documents({}) db_utils.close(client) return res
def get_question(question_id): client = db_utils.connect() db = client[skill_config.DB_NAME] question = db.questions.find_one({'_id': ObjectId(question_id)}) db_utils.close(client) return question
def __user_exists(user_id): client = db_utils.connect() db = client[skill_config.DB_NAME] res = db.users.count_documents({'user_id': user_id}) db_utils.close(client) return res == 1
def __get_already_fetched_questions(user_id): client = db_utils.connect() db = client[skill_config.DB_NAME] res = db.users.find_one({'user_id': user_id}) db_utils.close(client) return res['fetched_questions']
def __reset_user_fetched_questions(user_id): client = db_utils.connect() db = client[skill_config.DB_NAME] db.users.find_one_and_update({'user_id': user_id}, {'$set': { 'fetched_questions': [] }}) db_utils.close(client)
def __add_user_fetched_question(user_id, question_id): client = db_utils.connect() db = client[skill_config.DB_NAME] db.users.find_one_and_update({'user_id': user_id}, {'$push': { 'fetched_questions': question_id }}) db_utils.close(client)
def __get_random_question_not_fetched(already_fetched): client = db_utils.connect() db = client[skill_config.DB_NAME] questions = db.questions.aggregate([{ '$match': { '_id': { '$nin': already_fetched } } }, { '$sample': { 'size': 1 } }]) db_utils.close(client) return list(questions)[0]
def __insert_new_user(user_id): client = db_utils.connect() db = client[skill_config.DB_NAME] db.users.insert_one({'user_id': user_id, 'fetched_questions': []})