Skip to content

Instantly share code, notes, and snippets.

@sdbeng
Last active November 22, 2019 00:48
Show Gist options
  • Save sdbeng/b39870d2c868b32063e091fec860a727 to your computer and use it in GitHub Desktop.
Save sdbeng/b39870d2c868b32063e091fec860a727 to your computer and use it in GitHub Desktop.
trivia api
@app.route('/quizzes', methods=['POST'])
def quizzes():
"""route to quizzes, play"""
try:
data = request.get_json()
previous_questions = data['previous_questions']
quiz_category = data['quiz_category']
questions = None
# get questions for that quiz_category type 'click'
# LOGGING quiz_category: {'type': 'click', 'id': 0}
# means when `ALL` categories is selected, otherwise is by individual category
if quiz_category['type'] == "click":
questions = Question.query.all()
print(f"LOG ALL cat questions {questions}")
else:
questions=Question.query.filter_by(category =(quiz_category['id'])).all()
print(f"LOG Other cat questions {questions}")
formatted_questions = [ q.format() for q in questions]
possible_questions = []
for q in formatted_questions:
if q['id'] not in previous_questions:
possible_questions.append(q)
# get a random question from possible quest
question_sel = None
if len(possible_questions) > 0:
question_sel = random.choice(possible_questions)
return jsonify({
'success': True,
'question':question_sel,
'previous_questions': previous_questions,
'quizCategory':quiz_category
})
except Exception as ex:
abort(422)
print("***PLAY Quiz err: *** ", ex)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment