Exemplo n.º 1
0
def update_post(request, pk):
    request.data["author"] = request.user.pk
    serializer = PostSerializer(request.post, data=request.data)
    if serializer.is_valid():
        serializer.save()
        return Response({"success": True, "data": serializer.data})
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 2
0
    def put(self, request, pk):
        print('put')
        print(request.POST)
        print(request.data)
        print('request.data.photo')

        photo = request.data['photo']

        print('photo')
        print(photo)
        #  photo_blog/2021/12/15/069.jpg
        snippet = get_object_or_404(Mypost, pk=pk)
        serializer = PostSerializer(snippet, data=request.data)
        print('serializer')
        print(serializer)
        if serializer.is_valid():
            print("1")
            # if request.POST['photo'] != ['undefined']:
            # print(serializer.errors)
            serializer.save()
            return Response(serializer.data)
        else:
            my_photo = Mypost.objects.filter(pk=pk).values('photo')
            # print('my_photo')
            # print(my_photo[0]['photo'])

            # if title == '' or text == '':
            #     return JsonResponse({'res': '0'})
            # else:
            Mypost.objects.filter(pk=pk).update(
                title=request.POST['title'],
                text=request.POST['text'],
                photo=my_photo[0]['photo'])  # , photo=request.POST['photo']
            return Response(serializer.data)
Exemplo n.º 3
0
def api_create_post_view(request):
    user = request.user
    post = Post(author=user)
    serializer = PostSerializer(post, data=request.data)

    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)
    def post(self, request):
        print("Request", request.data)
        serializer = PostSerializer(data=request.data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, )
        else:
            return Response(serializer.error_messages,
                            status=status.HTTP_400_BAD_REQUEST)
    def put(self, request, slug):
        post = self.get_object(slug)
        serializer = PostSerializer(post, data=request.data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, )
        else:
            return Response(serializer.error_messages,
                            status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 6
0
 def post(self, request):
     print('POST')
     print(request.data)
     # добавляем пост
     serializer = PostSerializer(data=request.data)
     if serializer.is_valid():
         print(serializer.errors)
         serializer.save()
         print('save')
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 7
0
    def post(self, request):
        post = Post(author=request.user)

        if request.method == 'POST':
            serializer = PostSerializer(post, data=request.data)
            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)
Exemplo n.º 8
0
def api_post_create_view(request):
    post = Post(
        author=request.user
    )  # Take the user from the request as user is already authenticated using Token Authentication

    if request.method == 'POST':
        serializer = PostSerializer(post, data=request.data)
        data = dict()
        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)
Exemplo n.º 9
0
def posts_list(request):
    if request.method == 'GET':
        posts = Post.objects.all()
        serializers = PostSerializer(posts, many=True)
        return Response(serializers.data)

    elif (request.method == 'POST'):
        serializers = PostSerializer(data=request.data)
        if serializers.is_valid():
            serializers.save()
            return Response(serializers.data, status=status.HTTP_201_CREATED)
        return Response(serializers.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 10
0
def add_post(request):
    request.data["author"] = request.user.pk
    serializer = PostSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save()
        return Response({
            "success": True,
            "data": serializer.data
        },
                        status=status.HTTP_201_CREATED)
    return Response({
        "success": False,
        "message": serializer.errors
    },
                    status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 11
0
    def put(self, request, id):
        try:
            post = Post.objects.get(id=id)
        except Post.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

        if request.method == 'PUT':
            serializer = PostSerializer(post, data=request.data)
            data = {}
            if serializer.is_valid():
                serializer.save()
                data["success"] = "update successfull"
                return Response(data=data)
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 12
0
def api_post_update_view(request, pk):
    try:
        post = Post.objects.get(pk=pk)
    except Post.DoesNotExist:
        return Response({'error': 'Blog post does not exist.'},
                        status=status.HTTP_404_NOT_FOUND)

    user = request.user  # If Token Authentication is in place, Django will automatically fetch user via token
    if user != post.author:
        return Response(
            {'error': "You are not authorized to edit someone else's post."},
            status=status.HTTP_401_UNAUTHORIZED)

    if request.method == 'PUT':
        serializer = PostSerializer(post, data=request.data)
        data = dict()
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 13
0
def posts_details(request, pk):
    try:
        posts = Post.objects.get(pk=pk)
    except Post.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializers = PostSerializer(posts)
        return Response(serializers.data)

    elif request.method == 'PUT':
        serializers = PostSerializer(posts, request.data)
        if serializers.is_valid():
            serializers.save()
            return Response(serializers.data)
        return Response(serializers.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        posts.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)