Example #1
0
def api_update_blog_view(request, slug):

    try:
        blog_post = BlogPost.objects.get(slug=slug)
    except BlogPost.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    user = request.user
    if blog_post.author != user:
        return Response(
            {'response': "You don't have permission to edit that."})

    if request.method == 'PUT':
        serializer = BlogPostSerializer(blog_post,
                                        data=request.data,
                                        partial=True)
        data = {}
        if serializer.is_valid():
            serializer.save()
            data['response'] = UPDATE_SUCCESS
            data['pk'] = blog_post.pk
            data['title'] = blog_post.title
            data['body'] = blog_post.body
            data['slug'] = blog_post.slug
            data['date_updated'] = blog_post.date_updated
            #remove signature its not imp
            image_url = str(request.build_absolute_uri(blog_post.image.url))
            if "?" in image_url:
                image_url = image_url[:image_url.rfind("?")]
            data['image'] = image_url
            data['username'] = blog_post.author.username
            return Response(data=data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #2
0
def api_update_blog_view(request, id):

    try:
        blog_post = Post.objects.get(id=id)
    except Post.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    # user = request.user
    # if blog_post.author != user:
    #	return Response({'response':"You don't have permission to edit that."})

    if request.method == 'PUT':
        # serializer = BlogPostUpdateSerializer(blog_post, data=request.data, partial=True)
        serializer = BlogPostSerializer(
            blog_post, data=request.data)
        data = {}
        if serializer.is_valid():
            serializer.save()
            data['response'] = UPDATE_SUCCESS
            # data['pk'] = blog_post.pk
            # data['title'] = blog_post.title
            # data['body'] = blog_post.body
            # data['slug'] = blog_post.slug
            # data['date_updated'] = blog_post.date_updated
            # image_url = str(request.build_absolute_uri(blog_post.image.url))
            # if "?" in image_url:
            #	image_url = image_url[:image_url.rfind("?")]
            # data['image'] = image_url
            # data['username'] = blog_post.author.username
            return Response(data=data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #3
0
def api_create_blog_view(request):

    account = Author.objects.get(pk=1)
    blog_post = Post(author=account)
    if request.method == 'POST':

        # data = request.data
        # data['author'] = request.user.pk
        # serializer = BlogPostCreateSerializer(data=data)
        serializer = BlogPostSerializer(blog_post, data=request.data)
        #data = {}
        if serializer.is_valid():

            serializer.save()
            # blog_post = serializer.save()
            #data['response'] = CREATE_SUCCESS
            # data['pk'] = blog_post.pk
            # data['title'] = blog_post.title
            # data['body'] = blog_post.body
            # data['slug'] = blog_post.slug
            # data['date_updated'] = blog_post.date_updated
            # image_url = str(request.build_absolute_uri(blog_post.image.url))
            # if "?" in image_url:
            #	image_url = image_url[:image_url.rfind("?")]
            # data['image'] = image_url
            # data['username'] = blog_post.author.username
            # return Response(data=data)
            return Response(serializer.data)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #4
0
def api_post_blog_view(request, slug):
    account = request.user
    blog_post = BlogPost(author=account)
    if request.method == "POST":
        serializer = BlogPostSerializer(blog_post, data=request.data)
        data = {}
        if (serializer.is_valid()):
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.erros, status=status.HTTP_400_BAD_REQUEST)
Example #5
0
def api_blog_create_view(request):
    account = request.user

    blogpost = BlogPost(author=account)
    serializer = BlogPostSerializer(blogpost, data=request.data)
    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)
Example #6
0
def api_create_blog_view(request):
    account = Account.objects.get(pk=1)
    blogpost=BlogPost(author=account)
    if request.method == "POST":
        serializer = BlogPostSerializer(blogpost, data=request.data)
        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)
Example #7
0
def api_create_blog_view(request):

    blog_post = BlogPost(author=request.user)

    if request.method == 'POST':
        serializer = BlogPostSerializer(blog_post, data=request.data)
        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)
Example #8
0
def api_create_blog_view(request):
    account = request.user
    blog_post = BlogPost(author=account)

    if request.method == "POST":
        serializer = BlogPostSerializer(blog_post, data=request.data)
        if serializer.is_valid():
            print("VALID SERIALIZER")
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #9
0
def api_blog_create_view(request):
    account = Account.objects.get(pk=1)#query about a random user
    print(account)
    blog_post = BlogPost(author=account)#pass the author to BlogPost since it is a required field

    if request.method =='POST':
        serializer = BlogPostSerializer(blog_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)
Example #10
0
def api_blog_update_view(request,slug):
    try:
        blog_post = BlogPost.objects.get(slug=slug)
    except BlogPost.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    serializer = BlogPostSerializer(blog_post,data=request.data)
    data ={}
    if serializer.is_valid():
        serializer.save()
        data['success'] ='update successful'
        return Response(data=data)
    return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)
Example #11
0
def api_update_blog_view(request, id):
    try:
        blog_post = BlogPost.objects.get(id=id)
    except BlogPost.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'PUT':
        serializer = BlogPostSerializer(blog_post, data=request.data)
        if serializer.is_valid():
            serializer.save()
            data = {'success': "Updated successfully"}
            return Response(data=data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #12
0
def api_update_blog_view(request,slug):
    try:
        blogpost=BlogPost.objects.get(slug=slug)
    except BlogPost.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method =="PUT":
        serializer=BlogPostSerializer(blogpost,data=request.data)
        data={}
        if serializer.is_valid():
            serializer.save()
            data["sucess"] = "update sucessfully"
            return Response(data=data)
        return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)
Example #13
0
def api_update_blog_view(request, slug):

	try:
		blog_post = BlogPost.objects.get(slug=slug)
	except BlogPost.DoesNotExist:
		return Response(status=status.HTTP_404_NOT_FOUND)

	if request.method == 'PUT':
		serializer = BlogPostSerializer(blog_post, data=request.data)
		data = {}
		if serializer.is_valid():
			serializer.save()
			data[SUCCESS] = UPDATE_SUCCESS
			return Response(data=data)
		return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #14
0
def api_blog_detail_view(request, slug):
    try:
        blogpost = BlogPost.objects.get(slug=slug)
    except:
        return Response(status=status.HTTP_404_NOT_FOUND)
    serializer = BlogPostSerializer(blogpost)
    return Response(serializer.data)
Example #15
0
def api_create_blog_view(request):
    #get the one of account user who as the primary key = 1
    # account = Account.objects.get(pk=3)

    # get the account for the author ,with out author we don't create blogpost
    account = request.user
    blog_post = BlogPost(author=account)

    if request.method == 'POST':
        # passing blog post that already has author attach to it to the serializer
        serializer = BlogPostSerializer(blog_post, data=request.data)
        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)
Example #16
0
def api_put_blog_view(request, slug):
    try:
        blog_post = BlogPost.objects.get(slug=slug)
    except BlogPost.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)
    user = request.user
    if blog_post.author != user:
        return Response({'response': 'No permission to you'})
    if request.method == 'PUT':
        serializer = BlogPostSerializer(blog_post, data=request.data)
        data = {}
        if serializer.is_valid():
            serializer.save()
            data['success'] = "updated"
            return Response(data=data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #17
0
def api_detail_blog_view(request, slug):
    try:
        blog_post = BlogPost.objects.get(slug=slug)
    except BlogPost.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)
    if request.method == 'GET':
        serializer = BlogPostSerializer(blog_post)
        return Response(serializer.data)
def api_update_blog_view(request, slug):
    try:
        blog_post = BlogPost.objects.get(slug=slug)
    except BlogPost.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)
    user = request.user
    if blog_post.author != user:
        return Response(
            {'response': "Your are not authorized for this operation."})
    if request.method == "PUT":
        serializer = BlogPostSerializer(blog_post, data=request.data)
        data = {}
        if serializer.is_valid():
            serializer.save()
            data['success'] = "update successful"
            return Response(data=data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #19
0
def api_blog_detail_view(request,slug):
    try:
        blog_post = BlogPost.objects.get(slug=slug) #fetch the blog post
    except BlogPost.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    serializer = BlogPostSerializer(blog_post) #serialize it,now our data is ready in json,xml format
    return Response(serializer.data)    #return the response
Example #20
0
def api_blog_update_view(request, slug):
    try:
        blogpost = BlogPost.objects.get(slug=slug)
    except:
        return Response(status=status.HTTP_404_NOT_FOUND)
    user = request.user
    if blogpost.author != user:
        return Response(
            {'response': 'you donot have permission to edit this post'})
    data = {}
    serializer = BlogPostSerializer(blogpost)
    if request.method == "PUT":
        if serializer.is_valid():
            serializer.save()
            data["success"] = "your update was successful:)"
            return Response(data=data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #21
0
    def get(self, request):
        try:
            blog_list = Post.objects.all()
            serializer = BlogPostSerializer(
                blog_list, context={'request': request}, many=True)
            return Response(serializer.data)

        except ValueError as e:
            return Response(e.args[0], status.HTTP_400_BAD_REQUEST)
Example #22
0
def api_update_blog_view(request, slug):

    try:
        blog_post = BlogPost.objects.get(slug=slug)
    except BlogPost.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    user = request.user
    if blog_post.author != user:
        return Response({'response': "You dont have permision to update the post"})

    if request.method == "PUT":
        serializer = BlogPostSerializer(blog_post, data=request.data)
        data = {}
        if serializer.is_valid():
            serializer.save()
            data["success"] = "Update Successful"
            return Response(data=data)
        return Response(serializer.error, status=status.HTTP_400_BAD_REQUEST)
Example #23
0
def api_update_blog_view(request, slug):

    try:
        blog_post = BlogPost.objects.get(slug=slug)
    except BlogPost.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    user = request.user
    if blog_post.author != user:
        return Response(
            {'response': "No tienes permisos para editar este blog."})

    if request.method == 'PUT':
        serializer = BlogPostSerializer(blog_post, data=request.data)
        data = {}
        if serializer.is_valid():
            serializer.save()
            data["success"] = "Se ha actualizado correctamente"
            return Response(data=data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #24
0
def api_update_blog_view(request, slug):

    try:
        blog_post = BlogPost.objects.get(slug=slug)
    except BlogPost.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    user = request.user
    if blog_post.author != user:
        return Response(
            {'response': "You don't have permission to edit that."})

    if request.method == 'PUT':
        serializer = BlogPostSerializer(blog_post, data=request.data)
        data = {}
        if serializer.is_valid():
            serializer.save()
            data['response'] = UPDATE_SUCCESS
            return Response(data=data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #25
0
def api_detail_blog_view(request, slug):

    try:
        # BlogPost is a model
        blog_post = BlogPost.objects.get(slug=slug)

    except BlogPost.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        #passing blog_post object got from database
        serializer = BlogPostSerializer(blog_post)
        # Returning serilized data from that serilizer
        return Response(serializer.data)
Example #26
0
def blog_list(request):
    blog_post_list = BlogPost.objects.all().order_by('-created_on')
    if request.method == "GET":
        serializer = BlogPostSerializer(blog_post_list, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = BlogPostSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save(author=request.user)
            data = {}
            data['success'] = "Create successful"
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #27
0
def blog_detail_view(request, slug):
    try:
        blog_post = BlogPost.objects.get(slug=slug)
        data = {}
    except BlogPost.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == "GET":
        blog_post.view += 1
        blog_post.save()
        serializer = BlogPostSerializer(blog_post, many=False)
        return Response(serializer.data)

    elif blog_post.author != request.user:
        return Response(
            {'Response': "You are not the author of this blog post!"})

    elif request.method == "PUT":
        serializer = BlogPostSerializer(blog_post, data=request.data)
        if serializer.is_valid():
            serializer.save(user=request.user)
            data['update'] = "Update successful"
            return Response(data=data)
        # else:
        #     data['failure'] = "Update failure"
        #     return Response(data= data)
        #print(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == "DELETE":
        operation = blog_post.delete()
        if operation:
            data['delete'] = "Delete successful"
        else:
            data['failure'] = "delete failed"
            #return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        return Response(data=data)
Example #28
0
def api_list_blog_view(request):
    blog_post = BlogPost.objects.all()[::-1]
    if request.method == 'GET':
        serializer = BlogPostSerializer(blog_post, many=True)
        return Response(serializer.data)