Skip to content

Commit

Permalink
use serializer to save data given by user
Browse files Browse the repository at this point in the history
  • Loading branch information
maelys-buhler committed Mar 19, 2024
1 parent 7c27bf3 commit 9cab7d1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
14 changes: 14 additions & 0 deletions api/masteriqapp/serializers/OptionSerializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from rest_framework import serializers

from django.apps import apps


class OptionSerializer(serializers.ModelSerializer):
question = serializers.PrimaryKeyRelatedField(many=False, queryset=apps.get_app_config("masteriqapp").get_model("Question").objects.all())
# question = serializers.SlugRelatedField(many=False, slug_field='id',
# queryset=apps.get_app_config("masteriqapp").get_model(
# "Question").objects.all())
#question_id = serializers.UUIDField()
class Meta:
model = apps.get_app_config("masteriqapp").get_model("Option")
fields = ['text', 'is_correct', 'question']
4 changes: 2 additions & 2 deletions api/masteriqapp/serializers/QuestionSerializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


class QuestionSerializer(serializers.ModelSerializer):
category = serializers.SlugRelatedField(many=False, read_only=True, slug_field='name')
category = serializers.SlugRelatedField(many=False, slug_field='name', queryset=apps.get_app_config("masteriqapp").get_model("Category").objects.all())
class Meta:
model = apps.get_app_config("masteriqapp").get_model("Question")
fields = ['id', 'text', 'category']
fields = ['text', 'category']
30 changes: 21 additions & 9 deletions api/masteriqapp/views/QuestionView.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
from django.apps import apps
from rest_framework import status
from rest_framework.generics import get_object_or_404
from rest_framework.parsers import JSONParser
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response

from masteriqapp.serializers.OptionSerializer import OptionSerializer
from masteriqapp.serializers.QuestionAndOptionsSerializer import QuestionAndOptionsSerializer
from masteriqapp.serializers.QuestionSerializer import QuestionSerializer

masteriq = apps.get_app_config("masteriqapp")


class QuestionView(viewsets.ViewSet):
category_model = masteriq.get_model("Category")
question_model = masteriq.get_model("Question")
Expand All @@ -40,19 +40,31 @@ def new(self, request, pk):
def new_community(self, request):
datas = request.data
if not ('question' in datas and 'answer' in datas and 'options' in datas):
print("bad 1")
return Response(status=status.HTTP_400_BAD_REQUEST)
if len(datas['options']) < 1:
print("bad 2")
return Response(status=status.HTTP_400_BAD_REQUEST)

category = self.category_model.objects.get(name="Community")
question = self.question_model.objects.create(text=datas['question'], category=category)
options = []
question_serializer = QuestionSerializer(data={"text": datas["question"], "category": category.name})
question = None
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)

for option in datas['options']:
options.append(masteriq.get_model('Option').objects.create(text=option, is_correct=False, question=question))
options.append(masteriq.get_model('Option').objects.create(text=datas['answer'], is_correct=True, question=question))
option_serializer = OptionSerializer(data={"text": option, "is_correct": False, "question": 3})

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)

question_serializer = QuestionAndOptionsSerializer(question)
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)

0 comments on commit 9cab7d1

Please sign in to comment.