Beispiel #1
0
 def put(self, request, *args, **kwargs):
     user = request.user
     data = request.data
     pk = kwargs['pk']
     course = get_object_or_404(Course, pk=pk)
     if not course.is_authorized(request.user):
         return Response(status=401)
     content = course.description
     content_data = {
         'text': data['text'],
         "last_edited_at": datetime.datetime.now()
     }
     if "file" in request.FILES:
         file = request.FILES['file']
         content_data['file'] = file
     serializer = ContentSerializer(content, data=content_data)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     course_data = {
         "title": data['title'],
         "owner": course.owner.pk,
         "description": course.description.pk,
         "last_edited_at": datetime.datetime.now()
     }
     serializer = CourseSerializer(course, data=course_data)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     return Response(serializer.data, status=HTTP_200_OK)
Beispiel #2
0
    def create(self, request, *args, **kwargs):
        context = {'request': request}

        serializer = ContentSerializer(data=request.data, context=context)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        return Response(serializer.data, status=status.HTTP_201_CREATED)
Beispiel #3
0
    def update(self, request, *args, **kwargs):
        context = {'request': request}

        instance = self.get_object()
        serializer = ContentSerializer(instance,
                                       data=request.data,
                                       partial=False,
                                       context=context)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        return Response(serializer.data)
Beispiel #4
0
    def post(self, request, pk, format=None):
        try:
            igroup = InterestGroup.objects.get(pk=pk)
            serializer = ContentSerializer(igroup.contents,
                                           many=False,
                                           data=request.data,
                                           context={"request": request})
            if not serializer.is_valid():
                return Response(serializer.errors,
                                status=status.HTTP_400_BAD_REQUEST)
            content = serializer.create(serializer.validated_data)
            igroup.contents.add(content)

            for tag in content.tags.all():
                igroup.content_tags.add(tag)

            igroup.save()

            return Response(
                ContentSerializer(content, context={
                    'request': request
                }).data)
        except Exception as e:
            return Response({"error": str(e)},
                            status=status.HTTP_400_BAD_REQUEST)
Beispiel #5
0
 def post(self, request, *args, **kwargs):
     user = request.user
     data = request.data
     content = {'text': data['text']}
     if "file" in request.FILES:
         file = request.FILES['file']
         content['file'] = file
     serializer = ContentSerializer(data=content)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     course = {
         "title": data['title'],
         "owner": user.pk,
         "description": serializer.instance.pk
     }
     serializer = CourseSerializer(data=course)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     return Response(serializer.data, status=HTTP_200_OK)
Beispiel #6
0
 def post(self, request, *args, **kwargs):
     user = request.user
     data = request.data
     question = get_object_or_404(Question, pk=kwargs.get("pk"))
     content = {'text': data['text'], "sub_title": data.get("title")}
     if "file" in request.FILES:
         file = request.FILES['file']
         content['file'] = file
     serializer = ContentSerializer(data=content)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     choice = {
         "owner": user.pk,
         "description": serializer.instance.pk,
         "question": kwargs.get("pk"),
         "is_answer": data.get("is_answer")
     }
     serializer = ChoiceSerializer(data=choice)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     return Response(serializer.data, status=HTTP_200_OK)
Beispiel #7
0
 def post(self, request, *args, **kwargs):
     user = request.user
     data = request.data
     lesson = get_object_or_404(Lesson, pk=kwargs.get("pk"))
     content = {'text': data['text'], "sub_title": data['title']}
     if "file" in request.FILES:
         file = request.FILES['file']
         content['file'] = file
     serializer = ContentSerializer(data=content)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     place = Contents.objects.filter(lesson=lesson.id).count() + 1
     contents = {
         "lesson": kwargs.get("pk"),
         "content": serializer.instance.pk,
         "owner": user.pk,
         "place": place,
     }
     serializer = ContentsSerializer(data=contents)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     return Response(serializer.data, status=HTTP_200_OK)
Beispiel #8
0
    def patch(self, request):
        data = request.data
        serializers = PostSerializers(data=data, partial=True)
        if serializers.is_valid():
            obj = serializers.save()
            data1 = data["content"]
            for d in data1:
                d["post"] = obj.id
                print(d)
                ser_content = ContentSerializer(d, partial=True)
                if ser_content.is_valid():
                    ser_content.save()
                else:
                    return Response({"response": "Save Error"},
                                    status=status.HTTP_400_BAD_REQUEST)

            return Response({"response": "Success"},
                            status=status.HTTP_201_CREATED)
        else:
            return Response({"response": "Serialize Error"},
                            status=status.HTTP_400_BAD_REQUEST)