From 9cab7d150937db5f85384329f470fcac17502506 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=BChler=20Ma=C3=ABlys?= Date: Tue, 19 Mar 2024 21:37:27 +0100 Subject: [PATCH] use serializer to save data given by user --- .../serializers/OptionSerializer.py | 14 +++++++++ .../serializers/QuestionSerializer.py | 4 +-- api/masteriqapp/views/QuestionView.py | 30 +++++++++++++------ 3 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 api/masteriqapp/serializers/OptionSerializer.py diff --git a/api/masteriqapp/serializers/OptionSerializer.py b/api/masteriqapp/serializers/OptionSerializer.py new file mode 100644 index 0000000..d555649 --- /dev/null +++ b/api/masteriqapp/serializers/OptionSerializer.py @@ -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'] diff --git a/api/masteriqapp/serializers/QuestionSerializer.py b/api/masteriqapp/serializers/QuestionSerializer.py index 400389a..ae00f5e 100644 --- a/api/masteriqapp/serializers/QuestionSerializer.py +++ b/api/masteriqapp/serializers/QuestionSerializer.py @@ -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'] \ No newline at end of file + fields = ['text', 'category'] \ No newline at end of file diff --git a/api/masteriqapp/views/QuestionView.py b/api/masteriqapp/views/QuestionView.py index 1dfce87..db0103a 100644 --- a/api/masteriqapp/views/QuestionView.py +++ b/api/masteriqapp/views/QuestionView.py @@ -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") @@ -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)