Exemple #1
0
 def post(self, request, *args, **kwargs):
     user = request.user
     data = request.data
     lesson = get_object_or_404(Lesson, pk=kwargs.get("pk"))
     quiz = {
         "owner": user.pk,
         "lesson": kwargs.get("pk"),
     }
     serializer = QuizSerializer(data=quiz)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     return Response(serializer.data, status=HTTP_200_OK)
Exemple #2
0
 def put(self, request):
     queryset = Quiz.objects.get(id=request.data['id'])
     serializer = QuizSerializer(queryset, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_200_OK)
     return Response(serializer.errors, status=status.HTTP_404_NOT_FOUND)
    def put(self, request):
        try:
            child_id = CbcEngine.get_engine().encrypt(request.data['child_id'])
            child = Child.objects.get(id_number=child_id)
            parent = child.parent
            if parent.user.username != request.user.username:
                raise exceptions.AuthenticationFailed(
                    detail='Not authorized request.')
        except KeyError:
            return Response('One of the fields are missing.',
                            status=status.HTTP_400_BAD_REQUEST)
        except Child.DoesNotExist:
            return Response('child does not exist.',
                            status=status.HTTP_400_BAD_REQUEST)

        request.data['child_id'] = child_id
        serializer = QuizSerializer(data=request.data)
        if not serializer.is_valid():
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
        try:
            serializer.save()
        except IntegrityError:
            return Response('Quiz is already exists',
                            status=status.HTTP_400_BAD_REQUEST)
        return Response('Quiz added successfully!')
Exemple #4
0
 def put(self, request, pk, format=None):
     quiz = self.get_object(pk)
     serializer = QuizSerializer(quiz,
                                 data=request.data,
                                 context={'request': request})
     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)
Exemple #5
0
 def post(self, request):
     serializer = QuizSerializer(data=request.data)
     try:
         if serializer.is_valid():
             serializer.save()
             return Response(serializer.data,
                             status=status.HTTP_201_CREATED)
     except Exception as e:
         return Response(serializer.errors,
                         status=status.HTTP_404_NOT_FOUND)
    def update(self, request, pk, format=None):
        """
        Pass in the pk of a topic as the 'pk'. We will use this topic and return a random quiz.

        curl -X PUT -H "Authorization: Bearer <Token>" -H "Content-Type: application/json"
         --data '{"no_of_questions":"<no_of_questions>","no_of_choices":"<no_of_choices>"}'
         "127.0.0.1:8000/api/generate_quiz/<topic_id>/"
         """
        try:
            # Get the relevant topic.
            topic = self.topic_queryset().get(id=pk)
        except Exception as e:
            # Topic does not exist.
            return Response({"error_description": "Topic does not exist"},
                            status=status.HTTP_400_BAD_REQUEST)

        if not topic.questions.all():
            return Response(
                {
                    "error_description":
                    "Topic has no questions. Add some questions to the topic first."
                },
                status=status.HTTP_400_BAD_REQUEST)

        serializer = QuizSerializer(data=request.data)

        if serializer.is_valid():
            no_of_questions = serializer.validated_data['no_of_questions']
            no_of_choices = serializer.validated_data['no_of_choices']
            show_all_alternative_answers = serializer.validated_data[
                'show_all_alternative_answers']
            fixed_choices_only = serializer.validated_data[
                'fixed_choices_only']

            randomly_generated_quiz = topic.generate_quiz(
                no_of_questions=no_of_questions,
                no_of_choices=no_of_choices,
                show_all_alternative_answers=show_all_alternative_answers,
                fixed_choices_only=fixed_choices_only).quiz
            return Response(randomly_generated_quiz,
                            status=status.HTTP_201_CREATED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)