Beispiel #1
0
    def play_quiz():
        """
        Play quiz route to get questions for quizzes.

        :return:
        """
        try:
            request_data = request.get_json()
            previous_questions = request_data.get('previous_questions', [])
            quiz_category = request_data.get('quiz_category')

            if not quiz_category:
                abort(STATUS_BAD_REQUEST)

            category_id = quiz_category.get('id', 0)
            if category_id == 0:
                questions = get_all_questions()
            else:
                questions = get_all_questions(category_id=category_id)

            filtered_questions = list(
                filter(
                    lambda question: question.get('id') not in
                    previous_questions, questions))

            random_question = random.choice(filtered_questions) \
                if filtered_questions else None

            return jsonify({'question': random_question, 'success': True})

        except Exception as exp:
            abort(exp.code)
def get_questions():
    """
    Get questions by given page number, raise 404 if questions not found.

    :return: raise error in case of error otherwise
    json with questions, categories, current category,
    total questions and success status
    """
    try:
        page = request.args.get('page', 1, type=int)
        questions = get_questions_by_page(page)

        if len(questions) == 0:
            abort(STATUS_NOT_FOUND)

        return jsonify({
            'success': True,
            'current_category': None,
            'categories': get_all_categories(),
            'questions': questions,
            'total_questions': len(get_all_questions())
        })

    except Exception as exp:
        abort(exp.code)
def play_quiz(token):
    """
    Play quiz route to get questions for quizzes.

    or raise bad request if quiz category not found.

    :param token: string
    :return: raise error in case of error otherwise
    json with question and success status
    """
    try:
        request_data = request.get_json()
        previous_questions = request_data.get('previous_questions', [])
        quiz_category = request_data.get('quiz_category')

        if not quiz_category:
            abort(STATUS_BAD_REQUEST)

        category_id = quiz_category.get('id', 0)
        if category_id == 0:
            questions = get_all_questions()
        else:
            questions = get_all_questions(category_id=category_id)

        filtered_questions = list(
            filter(
                lambda question:
                question.get('id') not in previous_questions, questions
            )
        )

        random_question = random.choice(filtered_questions) \
            if filtered_questions else None

        return jsonify({
            'question': random_question,
            'success': True
        })

    except Exception as exp:
        abort(exp.code)
Beispiel #4
0
    def search_questions():
        """
        Return the list of questions filtered by given search.

        :return:
        """
        try:
            request_data = request.get_json()
            questions = get_all_questions(query=request_data.get('searchTerm'))
            return jsonify({
                'success': True,
                'questions': questions,
                'total_questions': len(questions),
            })

        except Exception as exp:
            abort(exp.code)
def search_questions():
    """
    Return the list of questions filtered by given search.

    :return: raise error in case of error otherwise
    json with questions, total questions and success status
    """
    try:
        request_data = request.get_json()
        questions = get_all_questions(
            query=request_data.get('searchTerm')
        )
        return jsonify({
            'success': True,
            'questions': questions,
            'total_questions': len(questions),
        })

    except Exception as exp:
        abort(exp.code)
Beispiel #6
0
    def get_questions():
        """
        Get questions by given page number.

        :return:
        """
        try:
            page = request.args.get('page', 1, type=int)
            questions = get_questions_by_page(page)

            if len(questions) == 0:
                abort(STATUS_NOT_FOUND)

            return jsonify({
                'success': True,
                'current_category': None,
                'categories': get_all_categories(),
                'questions': questions,
                'total_questions': len(get_all_questions())
            })

        except Exception as exp:
            abort(exp.code)
Beispiel #7
0
    def get_questions_by_category(category_id):
        """
        Get questions by category.

        :param category_id:
        :return:
        """
        try:
            category = get_category_by_id(category_id)

            if category is None:
                abort(STATUS_NOT_FOUND)

            questions = get_all_questions(category_id=category_id)
            return jsonify({
                "success": True,
                "questions": questions,
                "total_questions": len(questions),
                "current_category": category.format(),
            })

        except Exception as exp:
            abort(exp.code)
def get_questions_by_category(category_id):
    """
    Get questions by category id raise 404 if category not found.

    :param category_id: integer containing category id
    :return: raise error in case of error otherwise
    json with questions, total/current categories and success status
    """
    try:
        category = get_category_by_id(category_id)

        if category is None:
            abort(STATUS_NOT_FOUND)

        questions = get_all_questions(category_id=category_id)
        return jsonify({
            "success": True,
            "questions": questions,
            "total_questions": len(questions),
            "current_category": category.format(),
        })

    except Exception as exp:
        abort(exp.code)