示例#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)
示例#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)
示例#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)
示例#6
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)
示例#7
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)
示例#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)
示例#9
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)
示例#10
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)
示例#11
0
def get_posts(request):
    posts = Post.objects.all()
    serializer_context = {
        'request': request,
    }
    serializer = PostSerializer(posts, many=True, context=serializer_context)
    return Response({"success": True, "data": serializer.data})
示例#12
0
文件: views.py 项目: ugant2/readjoy
def post_detail_api(request, pk):
    try:
        one_post = Post.published.get(pk=pk)
    except Post.DoesNotExist:
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)
    if request.method == 'GET':
        post_detail_serializer = PostSerializer(one_post)
        return JSONResponse(post_detail_serializer.data)
示例#13
0
def api_detail_post_view(request, pk):
    try:
        post = Post.objects.get(pk=pk)
    except Post.PostNotFound:
        return Response(status=status.HTTP_404_NOT_FOUND)

    serializer = PostSerializer(post)
    return Response(serializer.data)
示例#14
0
    def get(self, request):
        try:
            post = Post.objects.all()
        except Post.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)

        if request.method == 'GET':
            serializer = PostSerializer(post, many=True)
            return Response(serializer.data)
示例#15
0
def search_post(request):
    search_val = request.query_params['search']
    posts = Post.objects.filter(title__contains=search_val)
    serializer = PostSerializer(posts, many=True)
    return Response({
        "sucess": True,
        "count": len(posts),
        "data": serializer.data
    })
示例#16
0
def api_post_detail_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)

    if request.method == 'GET':
        serializer = PostSerializer(post)
        return Response(serializer.data)
示例#17
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)
示例#18
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)
示例#19
0
def api_user_post_list_view(request, username):
    try:
        user = User.objects.get(username=username)
    except User.DoesNotExist:
        return Response({'error': 'User not found.'},
                        status=status.HTTP_404_NOT_FOUND)

    try:
        posts = Post.objects.filter(author=user)
    except Post.DoesNotExist:
        return Response({'error': 'Blog posts not found.'},
                        status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = PostSerializer(posts, many=True)
        return Response(serializer.data)
示例#20
0
def api_delete_post_view(request, pk):
    try:
        post = Post.objects.get(pk=pk)
    except Post.PostNotFount:
        return Response(status=status.HTTP_404_NOT_FOUND)

    user = request.user
    if post.author != user:
        return Response(
            {'response': 'You dont have permission to delete that'})

    serializer = PostSerializer(post, request.data)
    operation = post.delete()
    data = {}
    if operation:
        data['success'] = 'delete successful'
    else:
        data['failure'] = 'delete failed'
    return Response(data=data)
示例#21
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)
示例#22
0
    def get(self, request, pk):
        posts = Mypost.objects.filter(id=pk)

        serializer = PostSerializer(posts, many=True)
        return Response(serializer.data)
示例#23
0
 def get(self, request):
     # выводим все посты  апи
     items = Mypost.objects.all()
     serializer = PostSerializer(items, many=True)
     return Response(serializer.data)
示例#24
0
def get_single_post(request, pk):
    serializer = PostSerializer(request.post)
    return Response({"success": True, "data": serializer.data})
示例#25
0
文件: views.py 项目: ugant2/readjoy
def post_list_api(request):
    if request.method == 'GET':
        posts = Post.published.all()
        post_serializer = PostSerializer(posts, many=True)
        return JSONResponse(post_serializer.data)