From 1d3d121831100d2b33d63899eeaa408cd16fb060 Mon Sep 17 00:00:00 2001 From: CruiseDevice Date: Fri, 3 Apr 2020 17:59:20 +0530 Subject: Get user_answer based on question type --- api/views.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'api') diff --git a/api/views.py b/api/views.py index 550ee6d..cd2965c 100644 --- a/api/views.py +++ b/api/views.py @@ -184,13 +184,23 @@ class AnswerValidator(APIView): raise Http404 def post(self, request, answerpaper_id, question_id, format=None): - try: - user_answer = str(request.data['answer']) - except KeyError: - return Response(status=status.HTTP_400_BAD_REQUEST) user = request.user answerpaper = self.get_answerpaper(answerpaper_id, user) question = self.get_question(question_id, answerpaper) + try: + if question.type == 'mcq' or question.type == 'mcc': + user_answer = request.data['answer'] + elif question.type == 'integer': + user_answer = int(request.data['answer'][0]) + elif question.type == 'float': + user_answer = float(request.data['answer'][0]) + elif question.type == 'string': + user_answer = request.data['answer'] + print(user_answer) + else: + user_answer = request.data['answer'] + except KeyError: + return Response(status=status.HTTP_400_BAD_REQUEST) # save answer uid answer = Answer.objects.create(question=question, answer=user_answer) answerpaper.answers.add(answer) -- cgit