Skip to content

Commit

Permalink
Merge pull request #52 from HE-Arc/mbu-49-routes-user-iq-by-category
Browse files Browse the repository at this point in the history
Mbu 49 routes user iq by category
  • Loading branch information
maelys-buhler authored Mar 30, 2024
2 parents 1ea2fec + c9591d9 commit 75bea4a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
6 changes: 3 additions & 3 deletions api/masteriqapp/tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ def test_route(self):
assert response.json()['user_rank'] is not None
assert response.json()['user_iq'] is not None



response = c.get("/api/category/2/user_iq/")
assert response.status_code == 200
assert response.json()['user_iq'] is not None
7 changes: 6 additions & 1 deletion api/masteriqapp/views/IQView.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import random

from django.http import HttpResponse
from django.http import JsonResponse
from rest_framework import viewsets
Expand Down Expand Up @@ -33,5 +35,8 @@ def category_with_iq(self, request):
"user_iq": 100 #TODO: REPLACE WITH USER IQ
}
answer_dict[category.id] = cat_dict
return JsonResponse(answer_dict)
return Response(status=status.HTTP_200_OK, data=answer_dict)

@action(detail=True, methods=["GET"])
def user_iq(self, request, pk):
return Response(status=status.HTTP_200_OK, data={"user_iq":random.randint(1,200)})
4 changes: 0 additions & 4 deletions api/masteriqapp/views/QuestionView.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def new(self, request, pk):
new_question = random.choice(questions)
request.session['question'] = new_question.id
request.session['options_asked'] = False
print(request.session['question'])
serializer = QuestionSerializer(new_question)
return Response(serializer.data, status=status.HTTP_200_OK)

Expand All @@ -60,7 +59,6 @@ def new_community(self, request):
if option_serializer.is_valid():
option_serializer.save()
else:
print(option_serializer.data)
return Response(data={"field": "Option", "error": option_serializer.errors},
status=status.HTTP_400_BAD_REQUEST)

Expand All @@ -81,15 +79,13 @@ def options(self, request):
question_id = request.session['question']
request.session['options_asked'] = True
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)

@action(detail=False, methods=["POST"], url_path="answer_text")
def answer_text(self, request):
print(request.session['options_asked'])
if not 'answer' in request.data:
return Response(status=status.HTTP_400_BAD_REQUEST, data={"error": "No answer given"})
if not 'question' in request.session or not 'options_asked' in request.session:
Expand Down

0 comments on commit 75bea4a

Please sign in to comment.