Skip to content

Commit

Permalink
use if to check for yes/right/true and no/wrong/false
Browse files Browse the repository at this point in the history
  • Loading branch information
maelys-buhler committed Apr 9, 2024
1 parent dfa0af3 commit f743582
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions api/masteriqapp/views/QuestionView.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
masteriq = apps.get_app_config("masteriqapp")


def check_if_text_answer_is_correct(given_answer, right_answer):
if (right_answer.lower() == "yes" or right_answer.lower() == "true" or right_answer.lower() == "right") and (given_answer.lower() == "yes" or given_answer.lower() == "true" or given_answer.lower() == "right") :
return True
if (right_answer.lower() == "no" or right_answer.lower() == "wrong" or right_answer.lower() == "false") and (given_answer.lower() == "no" or given_answer.lower() == "wrong" or given_answer.lower() == "false") :
return True
if given_answer.lower() == right_answer.lower():
return True
return False


class QuestionView(viewsets.ViewSet):
category_model = masteriq.get_model("Category")
question_model = masteriq.get_model("Question")
Expand Down Expand Up @@ -96,9 +106,7 @@ def answer_text(self, request):
"answer with options"})
question = self.question_model.objects.get(pk=request.session['question'])
right_answer = question.options.get(is_correct=True)
user_is_correct = False
if request.data['answer'].lower() == right_answer.text.lower():
user_is_correct = True
user_is_correct = check_if_text_answer_is_correct(request.data['answer'], right_answer.text)
data_to_send = {"user_is_correct": user_is_correct, "right_answer": right_answer.text,
"answer_sent": request.data['answer']}
del request.session['question']
Expand Down Expand Up @@ -136,3 +144,5 @@ def options_asked(self, request):
else:
data_to_send = {"options_asked": request.session['options_asked']}
return Response(status=status.HTTP_200_OK, data=data_to_send)


0 comments on commit f743582

Please sign in to comment.