summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCruiseDevice2020-04-03 17:59:20 +0530
committerCruiseDevice2020-04-03 17:59:20 +0530
commit1d3d121831100d2b33d63899eeaa408cd16fb060 (patch)
tree8d3c422f8d97d9f67f5fb914694ed6b8c701c212
parentedf774b171444e2a671a838bf030931a1800ae12 (diff)
downloadonline_test-1d3d121831100d2b33d63899eeaa408cd16fb060.tar.gz
online_test-1d3d121831100d2b33d63899eeaa408cd16fb060.tar.bz2
online_test-1d3d121831100d2b33d63899eeaa408cd16fb060.zip
Get user_answer based on question type
-rw-r--r--api/views.py18
1 files changed, 14 insertions, 4 deletions
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)