def quiz_passenger(request): """ Serve up the next question with all possible answers """ print "request.POST: ", request.POST context = {} ls = LightService() # This is an either an answer to a question or a prompt to move to the next question # Either way, retrieve the current quiz id, passed in via POST quiz_id = request.POST.get("quiz_id") quiz = get_object_or_404(Quiz, pk=quiz_id) # Also attempt to retrieve an answer_id. If it doesn't exist, this is just a prompt # to move to the next question answer_id = request.POST.get("answer") if answer_id: user_answer, created = UserAnswer.objects.get_or_create(quiz_id=quiz_id, answer_id=answer_id) is_correct = user_answer.answer.correct context["previous_question"] = user_answer.answer.question context["previous_answer"] = user_answer.answer context["previous_answer_was_correct"] = is_correct context["previous_possible_answers"] = QuizAnswer.objects.filter(question_id=user_answer.answer.question.id) if is_correct: ls.correct() else: ls.incorrect() # Retrieve a question the user hasn't answered yet already_answered = UserAnswer.objects.filter(quiz_id=quiz.id) print "already_answered: ", already_answered already_answered_ids = [a.answer.question.id for a in already_answered] question = QuizQuestion.objects.exclude(id__in=already_answered_ids).first() if question: possible_answers = QuizAnswer.objects.filter(question_id=question.id) context["next_question"] = question context["next_possible_answers"] = possible_answers context["quiz_id"] = quiz_id return render_to_response("quiz_passenger.html", context, RequestContext(request))
def lights_game_end(request): ls = LightService() ls.game_end() context = {"lights": "game_end"} return render_to_response("test_lights.html", context, RequestContext(request))
def lights_correct(request): ls = LightService() ls.correct() context = {"lights": "correct"} return render_to_response("test_lights.html", context, RequestContext(request))