Last active
November 22, 2019 00:48
-
-
Save sdbeng/b39870d2c868b32063e091fec860a727 to your computer and use it in GitHub Desktop.
trivia api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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