Skip to content

Commit

Permalink
Merge pull request #47 from HE-Arc/mbu-38-get-options-route
Browse files Browse the repository at this point in the history
Mbu 38 get options route
  • Loading branch information
maelys-buhler authored Mar 30, 2024
2 parents a564329 + ac930f6 commit 2ba42e3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
17 changes: 7 additions & 10 deletions api/masteriqapp/tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def test_route(self):

response = c.get("/api/question/1/new/")
assert response.status_code == 200
#assert response.json()['id'] is not None
assert response.json()['text'] is not None
assert response.json()['category'] is not None

Expand All @@ -24,12 +23,10 @@ def test_route(self):
"options": ["11", "15", "He wasnt born"],
"answer": "1"})

print(response.status_code)
print(response.json())
#TODO: understand why it works in manual test, but not in this test
#assert response.status_code == 201
#assert response.json()['id'] is not None
#assert response.json()['text'] is not None
#assert response.json()['category'] is not None
#assert response.json()['options'] is not None
#assert len(response.json()['options']) >= 2
response = c.get("/api/question/options/")
assert response.status_code == 200
assert response.json()['question_id'] is not None
assert response.json()['number_of_question'] is not None
assert response.json()['options'] is not None
assert len(response.json()['options']) >= 2

24 changes: 20 additions & 4 deletions api/masteriqapp/views/QuestionView.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def new_community(self, request):
if question_serializer.is_valid():
question = question_serializer.save()
else:
return Response(data={"field": "Question", "errors": question_serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
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": 3})
Expand All @@ -59,12 +60,27 @@ def new_community(self, request):
option_serializer.save()
else:
print(option_serializer.data)
return Response(data={"field": "Option", "error": option_serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
return Response(data={"field": "Option", "error": option_serializer.errors},
status=status.HTTP_400_BAD_REQUEST)

option_serializer = OptionSerializer(data={"text": datas['answer'], "is_correct": True, "question": question.id})
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(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"])
def options(self, request):
if not 'question' in request.session:
return Response(status=449, data={"error": "No question being answered at the moment"})
question_id = request.session['question']
question = self.question_model.objects.get(pk=question_id)
print(question.options.all())
data_to_send = {'question_id': question.id, 'number_of_question': len(question.options.all()), 'options': {}}
for option in question.options.all():
data_to_send['options'][option.id] = option.text
return Response(status=status.HTTP_200_OK, data=data_to_send)

0 comments on commit 2ba42e3

Please sign in to comment.