Ejemplo n.º 1
0
def get_questions():
    '''
        Handles GET requests for getting all questions.
        '''

    # get all questions and paginate
    selection = Question.query.all()
    total_questions = len(selection)
    current_questions = paginate_questions(request, selection)

    # get all categories and add to dict
    categories = Category.query.all()
    categories_dict = {}
    for category in categories:
        categories_dict[category.id] = category.type

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

    # return data to view
    return jsonify({
        'success': True,
        'questions': current_questions,
        'total_questions': total_questions,
        'categories': categories_dict
    })
Ejemplo n.º 2
0
def get_questions_by_category(id):
    '''
        Handles GET requests for getting questions based on category.
        '''

    # get the category by id
    category = Category.query.filter_by(id=id).one_or_none()

    # abort 400 for bad request if category isn't found
    if (category is None):
        abort(400)

    # get the matching questions
    selection = Question.query.filter_by(category=category.id).all()

    # paginate the selection
    paginated = paginate_questions(request, selection)

    # return the results
    return jsonify({
        'success': True,
        'questions': paginated,
        'total_questions': len(Question.query.all()),
        'current_category': category.type
    })
Ejemplo n.º 3
0
 def getQuestions():
     '''
   An endpoint to handle GET requests for questions,
   including pagination (every 10 questions).
   return a list of questions, number of total questions,
       current category, categories.
   and return 404 if not found page
   QUESTIONS_PER_PAGE = 10 global define var
   Two routes if use in traminal you can use single or collection sentence
     '''
     questions = Question.query.order_by(Question.id).all()
     total_questions = len(questions)
     category_query = Category.query.order_by(Category.id).all()
     current_questions = paginate_questions(request, questions,
                                            QUESTIONS_PER_PAGE)
     if (len(current_questions) == 0):
         abort(404)
     categories = {}
     for category in category_query:
         categories[category.id] = category.type
     return jsonify({
         'success': True,
         'questions': current_questions,
         'totalQuestions': total_questions,
         'categories': categories
     })
Ejemplo n.º 4
0
   def searchQuestion():
       '''
 A POST endpoint to get questions based on a search term.
   return any questions for whom the search term is
   a substring of the question,
       totalQuestions and current_category
   if search term return 422
   if not exit return 404 not found
       '''
       searchTerm = ''
       searchData = request.get_json()
       searchTerm = searchData.get('searchTerm', '')
       if searchTerm == '':
           print("sear")
           abort(422)
       try:
           question1 = ''
           question1 = Question.query.filter(
               Question.question.ilike(f'%{searchTerm}%')).all()
           if question1 == 0:
               abort(404)
           else:
               current_questions = paginate_questions(request, question1,
                                                      QUESTIONS_PER_PAGE)
               for category in current_questions:
                   current_category = category['category']
               return jsonify({
                   'success': True,
                   'questions': current_questions,
                   'totalQuestions': len(current_questions),
                   'current_category': current_category
               })
       except Exception:
           abort(404)
Ejemplo n.º 5
0
 def getQuestionsOnCategory(category_id):
   try:
     category_query = Category.query.filter_by(id=category_id).one_or_none()
     questions = Question.query.filter_by(category=category_id).all()
     total_questions = len(questions)
     current_questions = paginate_questions(request, questions, QUESTIONS_PER_PAGE)
     return jsonify({
         'success': True,
         'questions': current_questions,
         'totalQuestions': total_questions,
         'category': category_query.type
     })
   except Exception:
     abort(404)
Ejemplo n.º 6
0
 def get_questions():
   questions = Question.query.order_by(Question.id).all()
   total_questions = len(questions)
   category_query = Category.query.order_by(Category.id).all()
   current_questions = paginate_questions(
       request, questions,
       QUESTIONS_PER_PAGE)
   if (len(current_questions) == 0):
       abort(404)
   categories = {category.id : category.type.format() for category in category_query}
   return jsonify({
       'success': True,
       'questions': current_questions,
       'totalQuestions': total_questions,
       'categories': categories
   })
Ejemplo n.º 7
0
 def searchQuestion():
   try:
     searchData = request.get_json().get('searchTerm', '')
     searchTerm = "%{}%".format(searchData)
     
     question = Question.query.filter(Question.question.ilike(searchTerm)).all()
     if question == 0 or not question:
       abort(404)
     else:
       current_questions = paginate_questions(request, question, QUESTIONS_PER_PAGE)
       categories = [category['category'] for category in current_questions]
       return jsonify({
         'success': True,
         'questions': current_questions,
         'totalQuestions': len(current_questions),
         'current_category': categories
       })
   except Exception:
     abort(404)
Ejemplo n.º 8
0
 def getQuestionsOnCategory(category_id):
     '''
     A GET endpoint to get questions based on category.
     return: return 'success': True, get maximum 10 questions,
         totalQuestions, type of category
     if any problem like category not exit except 404
     '''
     try:
         category_query = Category.query.filter_by(
             id=category_id).one_or_none()
         questions = Question.query.filter_by(category=category_id).all()
         total_questions = len(questions)
         current_questions = paginate_questions(request, questions,
                                                QUESTIONS_PER_PAGE)
         return jsonify({
             'success': True,
             'questions': current_questions,
             'totalQuestions': total_questions,
             'categories': category_query.type
         })
     except Exception:
         abort(404)
Ejemplo n.º 9
0
def post_question():
    '''
        Handles POST requests for creating new questions and searching questions.
        '''
    # load the request body
    body = request.get_json()

    # if search term is present
    if (body.get('searchTerm')):
        search_term = body.get('searchTerm')

        # query the database using search term
        selection = Question.query.filter(
            Question.question.ilike(f'%{search_term}%')).all()

        # 404 if no results found
        if (len(selection) == 0):
            abort(404)

        # paginate the results
        paginated = paginate_questions(request, selection)

        # return results
        return jsonify({
            'success': True,
            'questions': paginated,
            'total_questions': len(Question.query.all())
        })
    # if no search term, create new question
    else:
        # load data from body
        new_question = body.get('question')
        new_answer = body.get('answer')
        new_difficulty = body.get('difficulty')
        new_category = body.get('category')

        # ensure all fields have data
        if ((new_question is None) or (new_answer is None)
                or (new_difficulty is None) or (new_category is None)):
            abort(422)

        try:
            # create and insert new question
            question = Question(question=new_question,
                                answer=new_answer,
                                difficulty=new_difficulty,
                                category=new_category)
            question.insert()

            # get all questions and paginate
            selection = Question.query.order_by(Question.id).all()
            current_questions = paginate_questions(request, selection)

            # return data to view
            return jsonify({
                'success': True,
                'created': question.id,
                'question_created': question.question,
                'questions': current_questions,
                'total_questions': len(Question.query.all())
            })

        except:
            # abort unprocessable if exception
            abort(422)