def getQuiz(): question = Question('', '', 0, 0) previousQuestions = request.get_json().get('previous_questions') quizCategory = request.get_json().get('quiz_category') print(quizCategory) if(quizCategory['id'] == 0): questions = Question.getQuestionsAndCategories(question).all() else: questions = Question.getQuestionsAndCategories(question).filter(Category.id == quizCategory['id']).all() formated_questions = [] for q in questions: formated_questions.append({ 'id': q[0], 'question': q[1], 'answer': q[2], 'category': q[3], 'difficulty': q[4] }) question = random.choice(formated_questions) canSendit = True print(previousQuestions) print(question) count = len(formated_questions) while count > 0: print(count) count -= 1 if(question['id'] in previousQuestions): canSendit = False question = random.choice(formated_questions) print(question) else: canSendit = True break if(len(previousQuestions) == len(formated_questions)): canSendit = False break if(canSendit): return jsonify({ 'success':True, 'question':question }) else: return jsonify({ 'success':False, 'question':'', 'message':'You answered all questions' })
def getQuestionsOfCategory(id): question = Question('', '', 0, 0) questions = Question.getQuestionsAndCategories(question).filter(Category.id == id).all() currentCategory = Category.query.get(id) # print(questions) formated_questions = [] for q in questions: formated_questions.append({ 'id': q[0], 'question': q[1], 'answer': q[2], 'category': q[3], 'difficulty': q[4] }) # formated_questions = [q.format() for q in questions] # print(formated_questions) return jsonify({ 'success':True, 'questions':formated_questions, 'total_questions':len(formated_questions), 'currentCategory':currentCategory.format() })