def user_answer(request): """ POST: Set the user's answer for the current question. """ question = None try: question = Question.objects.get(id=request.POST.get('question_id')) raw_answer = request.POST.get('answer') if raw_answer == 'up': answer = True else: answer = False ua = UserAnswer( user=request.user, question=question, answer=answer ) ua.save() result = ua.is_correct except Question.DoesNotExist: result = 'Failed - Invalid Question' response = dict(result=result, feedback=question.feedback) return HttpResponse(json.dumps(response))
def submit_answer(req, quiz_id): quiz = get_object_or_404(Quiz, pk=quiz_id) try: uid = req.session['uid'] user = Participant.objects.get(pk=uid) except (KeyError, django.core.exceptions.ObjectDoesNotExist): return HttpResponse('Unauthorized', status=401) ans_selection = req.REQUEST['answer'] ans = UserAnswer(quiz=quiz, selection=ans_selection, user=user) ans.save() return utils.JsonStatuses.OK