Esempio n. 1
0
 def put(self, request, quiz_pk, question_pk):
     question = self.get_object(quiz_pk, question_pk)
     serializer = QuestionSerializer(question,
                                     data=request.data,
                                     context={
                                         'request': request,
                                         'quiz_pk': quiz_pk
                                     })
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_200_OK)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 2
0
 def post(self, request, pk, format=None):
     """Create new question."""
     quiz = self.get_object(pk)
     serializer = QuestionSerializer(data=request.data,
                                     context={
                                         'request': request,
                                         'quiz_pk': quiz.pk
                                     },
                                     many=True)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 3
0
    def test_update_question_with_answers(self):
        """
        Make sure question is updated, and new answers are added,
        old ones are destroyed.
        """
        payload_data = {
            'question':
            'What is your name?',
            'explanation':
            'Lorem ipsum ...',
            'answers': [
                dict(content='answer1', is_correct=False),
                dict(content='answer2', is_correct=True)
            ]
        }

        response = self.client.put(reverse('question_detail',
                                           args=(self.quiz.id,
                                                 self.question.id)),
                                   data=payload_data)
        updated_obj = Question.objects.get(id=self.question.id)

        self.assertEqual(updated_obj.question, 'What is your name?')
        self.assertEqual(response.data, QuestionSerializer(updated_obj).data)
        self.assertEqual(QuestionAnswer.objects.count(), 2)
        self.assertEqual(updated_obj.answers.count(), 2)
Esempio n. 4
0
    def get(self, request, category_id=None):
        try:
            quiz = Quiz.objects.get(category__id=category_id)
        except Quiz.DoesNotExist:
            raise Http404

        serializer = QuestionSerializer(quiz.question.all(), many=True)
        return Response(serializer.data)
Esempio n. 5
0
    def get(self, request, **kwargs):
        quiz_filter = self.request.query_params.get('quiz_id', None)
        qs = self.get_queryset()

        if quiz_filter:
            serializers = QuizSerializer(qs, many=True)
        else:
            serializers = QuestionSerializer(qs, many=True)
        return Response(serializers.data)
Esempio n. 6
0
    def get(self, request, id):
        try:
            subject = Subject.objects.get(pk=id)
        except Subject.DoesNotExist:
            return Response({'error': 'Does Not Exist.'},
                            status=status.HTTP_404_NOT_FOUND)

        queryset = subject.questions.all()
        serializer = QuestionSerializer(instance=queryset, many=True)
        return Response(serializer.data)
Esempio n. 7
0
    def test_get_all_quiz_questions(self):
        """Receive list of questions for specific quiz."""
        serialized_data = QuestionSerializer(
            Question.objects.filter(quiz=self.quiz_published), many=True).data

        response = self.client.get(
            reverse('question_list', args=(self.quiz_published.id, )))

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, serialized_data)
Esempio n. 8
0
    def post(self, request, *args, **kwargs):
        user = request.user
        data = request.data
        quiz = get_object_or_404(Quiz, pk=kwargs.get("pk"))
        question = {
            "description": data['question'],
            "quiz": quiz.pk,
            "owner": user.pk
        }

        serializer = QuestionSerializer(data=question)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        choices = data['choices']
        question = serializer.instance
        for c in choices:
            choice = {
                "description": c['text'],
                "is_answer": c['is_answer'],
                "question": question.pk,
                "owner": user.pk
            }

            serializer = ChoiceSerializer(data=choice)
            serializer.is_valid(raise_exception=True)
            serializer.save()
        return Response(QuestionDetailSerializer(question).data,
                        status=HTTP_200_OK)
Esempio n. 9
0
    def put(self, request, *args, **kwargs):
        user = request.user
        data = request.data
        pk = kwargs['pk']
        question = get_object_or_404(Question, pk=pk)
        if not question.is_authorized(request.user):
            return Response(status=401)

        question_data = {
            "owner": question.owner.pk,
            "description": data['question'],
            "last_edited_at": datetime.datetime.now(),
            "quiz": question.quiz.pk
        }
        serializer = QuestionSerializer(question, data=question_data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        choices = data['choices']
        for c in choices:
            choice_data = {
                "description": c['text'],
                "is_answer": c['is_answer'],
                "question": question.pk,
                "owner": user.pk
            }
            if "id" in c:
                choice = Choice.objects.filter(pk=c['id']).first()
                if choice:
                    serializer = ChoiceSerializer(choice, data=choice_data)
            else:
                serializer = ChoiceSerializer(data=choice_data)
            serializer.is_valid(raise_exception=True)
            serializer.save()
        return Response(QuestionDetailSerializer(question).data,
                        status=HTTP_200_OK)
 def get(self, request, format=None, **kwargs):
     question = Question.objects.filter(quiz__title=kwargs['topic'])
     serializer = QuestionSerializer(question, many=True)
     return Response(serializer.data)
Esempio n. 11
0
 def get(self, request, quiz_pk, question_pk):
     question = self.get_object(quiz_pk, question_pk)
     serializer = QuestionSerializer(question)
     return Response(serializer.data, status=status.HTTP_200_OK)
Esempio n. 12
0
 def get(self, request, pk, format=None):
     """Return all quiz questions."""
     questions = self.get_queryset(pk)
     serializer = QuestionSerializer(questions, many=True)
     return Response(serializer.data, status=status.HTTP_200_OK)
Esempio n. 13
0
 def list(self, request, *args, **kwargs):
     quiz = Question.objects.filter(quiz__title=kwargs['topic'])
     serializer = QuestionSerializer(quiz, many=True)
     return Response(serializer.data)
Esempio n. 14
0
 def get(self, request, pid, qid):
     poll = Question.objects.filter(poll=pid).filter(pk=qid)
     serializer = QuestionSerializer(poll, many=True)
     return Response(serializer.data)