Skip to content

Commit

Permalink
shuffle options in new questions
Browse files Browse the repository at this point in the history
  • Loading branch information
maelys-buhler committed Apr 21, 2024
1 parent d710fca commit 050a983
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions api/masteriqapp/views/QuestionView.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,25 @@ def new_community(self, request):
else:
return Response(data={"field": "Question", "errors": question_serializer.errors},
status=status.HTTP_400_BAD_REQUEST)

for option in datas['options']:
option_serializer = OptionSerializer(data={"text": option, "is_correct": False, "question": question.id})
options = []
for i in range(len(datas['options'])):
if i == int(datas['answer']):
options.append([datas['options'][i], True])
else:
options.append([datas['options'][i], False])
random.shuffle(options)
for option in options:
option_serializer = OptionSerializer(data={"text": option[0], "is_correct": option[1], "question": question.id})

if option_serializer.is_valid():
option_serializer.save()
else:
return Response(data={"field": "Option", "error": option_serializer.errors},
field = "Option"
if option[1]:
field = "Answer"
return Response(data={"field": field, "error": option_serializer.errors},
status=status.HTTP_400_BAD_REQUEST)

option_serializer = OptionSerializer(
data={"text": datas['answer'], "is_correct": True, "question": question.id})
if option_serializer.is_valid():
question = option_serializer.save()
else:
return Response(data={"field": "Answer", "error": option_serializer.errors},
status=status.HTTP_400_BAD_REQUEST)

return Response(question_serializer.data, status=status.HTTP_201_CREATED)

@action(detail=False, methods=["GET"], permission_classes=[IsAuthenticated])
Expand Down

0 comments on commit 050a983

Please sign in to comment.