예제 #1
0
    def get_questions():

        questions = Question.query.order_by(Question.id).all()
        current_questions = paginate_questions(request, questions)
        categories = Category.query.order_by(Category.id).all()

        # no questions found/left
        if len(current_questions) == 0:
            abort(404)

        Question.close()
        return jsonify({
            "success":
            True,
            "status_code":
            200,
            "status_message":
            'OK',
            "questions":
            current_questions,
            "total_questions":
            len(questions),
            "current_category":
            list(set([question['category']
                      for question in current_questions])),
            "categories":
            {category.id: category.type
             for category in categories}
        })
예제 #2
0
    def search_questions_by_categories(category_id):

        filtered_questions = Question.query.filter(
            Question.category == category_id).order_by(Question.id).all()
        current_questions = paginate_questions(request, filtered_questions)

        # no question found/left
        if len(current_questions) == 0:
            abort(404)

        Question.close()

        return jsonify({
            "success":
            True,
            "status_code":
            200,
            "status_message":
            'OK',
            "questions":
            current_questions,
            "total_questions":
            len(filtered_questions),
            "current_category":
            list(set([question['category']
                      for question in current_questions])),
        })
예제 #3
0
    def pick_random_question():
        previous_questions = request.get_json().get('previous_questions')
        quiz_category = request.get_json().get('quiz_category')

        # all or specified category
        if quiz_category.get('id') == 0:
            questions = Question.query.order_by(Question.id).all()
        else:
            questions = Question.query.filter(
                Question.category == quiz_category.get('id')).order_by(
                    Question.id).all()
        # questions left
        question_left = []
        for question in questions:
            if question.id not in previous_questions:
                question_left.append(question.format())

        # if no question left
        if len(question_left) == 0:
            abort(404)

        Question.close()

        return jsonify({
            "success": True,
            "status_code": 200,
            "status_message": 'OK',
            "question": random.choice(question_left),
        })
예제 #4
0
    def search_questions_by_searchterm():

        searchterm = request.get_json().get('searchTerm')
        filtered_questions = Question.query.filter(
            Question.question.ilike('%{}%'.format(searchterm))).order_by(
                Question.id).all()
        current_questions = paginate_questions(request, filtered_questions)

        # no question found/left
        if len(current_questions) == 0:
            abort(404)
        Question.close()
        return jsonify({
            "success":
            True,
            "status_code":
            200,
            "status_message":
            'OK',
            "questions":
            current_questions,
            "total_questions":
            len(filtered_questions),
            "current_category":
            list(set([question['category']
                      for question in current_questions])),
        })
예제 #5
0
    def delete_question(question_id):

        question = Question.query.filter(
            Question.id == question_id).one_or_none()

        # no question found/left
        if question is None:
            abort(404)
        else:
            question.delete()
            Question.close()
            return jsonify({
                "success": True,
                "status_code": 200,
                "status_message": 'OK',
                "deleted": question_id
            })
예제 #6
0
    def add_question():
        print("add question")
        data = request.get_json()
        question = Question(question=data["question"],
                            answer=data["answer"],
                            category=data["category"],
                            difficulty=data["difficulty"])
        try:
            question.insert()

        except:
            question.rollback()
            abort(404)
        finally:
            question.close()

        return jsonify({"success": True})
예제 #7
0
    def delete_questions(question_id):
        error = False

        try:
            question = Question.query.filter(
                Question.id == question_id).first()
            #print (question.format())
            if question is None:
                error = True
            else:
                question.delete()

        except:
            error = True
            Question.rollback()
        finally:
            Question.close()
            # do not want to publish a 500 message
            # return 422 here
            if error:
                abort(422)

        return jsonify({"success": True})
예제 #8
0
    def add_question_to_db():
        ''' Endpoint to POST a new question,
        which will require the question and answer text,
        category, and difficulty score. '''

        if request.method != 'POST':
            abort(405)

        error = False
        form_data = request.get_json()

        if not form_data:
            abort(400)

        try:
            question = Question(
                question=form_data['question'],
                answer=form_data['answer'],
                category=form_data['category'],
                difficulty=int(form_data['difficulty'])
            )

            question.insert()

        except:
            error = True
            question.cancel()
            print(sys.exc_info())

        finally:
            question.close()

        if error:
            abort(422)

        else:
            return jsonify({'success': True})
예제 #9
0
    def post_question():
        # retrieve form data
        question = request.get_json().get('question')
        answer = request.get_json().get('answer', '')
        difficulty = request.get_json().get('difficulty')
        category = request.get_json().get('category')

        # empty from raise 422
        if (question.strip() == "" or answer.strip() == ""
                or difficulty.strip() == "" or category.strip() == ""):
            abort(422)

        new_question = Question(question=question,
                                answer=answer,
                                difficulty=difficulty,
                                category=category)
        new_question.insert()

        Question.close()
        return jsonify({
            "success": True,
            "status_code": 200,
            "status_message": 'OK',
        })