Ejemplo n.º 1
0
def edit_comment(request, post_id, comment_id):
    """Edit an existing comment."""
    post = get_object_or_404(Post, id=post_id)
    comment = get_object_or_404(Comment, id=comment_id)

    # Make sure the post belongs to the current user.
    if comment.owner != request.user:
        return HttpResponseForbidden('<h1>403 Forbidden</h1>')

    # Make sure the comment belongs to the current post.
    if post.id != comment.comment_post.id:
        raise Http404

    if request.method != 'POST':
        # Initial request; pre-fill form with the current comment.
        form = CommentForm(instance=comment)
    else:
        # POST data submitted; process data.
        form = CommentForm(instance=comment, data=request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('post', args=[post.id]))

    context = {'comment': comment, 'form': form}
    return render(request, 'blogs/edit_comment.html', context)
Ejemplo n.º 2
0
def post_detail(request,_id,**kwargs):
    try:
        data =blog.objects.get(id =_id)
        liked = False
        if data.likes.filter(id=request.user.id).exists():
            liked = True
        post_is_liked = liked

        post_likes = data.likes.count()
        comments = CommentModel.objects.filter(blog = data)
    except blog.DoesNotExist:
        raise Http404('Data does not exist')

    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            #Comment = CommentModel(your_name= form.cleaned_data['your_name'],
            #comment_text=form.cleaned_data['comment_text'],
            #blog=data)
            #Comment.save()
            Comment = CommentModel(your_name= request.user,
            comment_text=form.cleaned_data['comment_text'],
            blog=data)
            Comment.save()
            return redirect(f'/postdetail/{_id}')
    else:
        form = CommentForm()
    context = {
            'post_is_liked':post_is_liked,
            'post_likes':post_likes,
            'data':data,
            'form':form,
            'comments':comments,
        }
    return render(request,'postdetail.html',context)
Ejemplo n.º 3
0
def add_comment_to_post(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == 'POST':
        form = CommentForm(request.Post)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = CommentForm()
    return render(request, 'blogs/comment_form.html', {'form': form})
Ejemplo n.º 4
0
def add_comment_to_post(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == "POST":
        form = CommentForm(request.POST)
        form.instance.created_by = request.user
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.save()
            return redirect('blogs:post_detail', pk=post.pk)
    else:
        form = CommentForm()
    return render(request, 'blogs/comment_form.html', {'form': form})
Ejemplo n.º 5
0
def add_comment_to_post(request, pk):
    post = get_object_or_404(Post, pk=pk)
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            #Here Model CommentPost has ForeignKey Known as Post. SO that Comment can be added to that particular post.
            comment.post = post
            comment.save()
            redirect('post_detail', pk=post.pk)
    else:
        form = CommentForm()
    return render(request, 'blogs/comment_form.html', {'form': form})
Ejemplo n.º 6
0
def blog_detail(request, pk):
    post = Post.objects.get(pk=pk)
    form = CommentForm()
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = Comment(author=form.cleaned_data['author'],
                              body=form.cleaned_data['body'],
                              post=post)
            comment.save()

    comments = Comment.objects.filter(post=post)
    context = {'post': post, 'comments': comments, 'form': form}
    return render(request, 'blog_detail.html', context)
Ejemplo n.º 7
0
def comment(request, blogID):
    if request.method == 'POST':
        form = CommentForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            blog = Blog.objects.get(num=blogID)
            user = blog.writer
            token = request.META.__getitem__('HTTP_X_TOKEN')

            if default_token_generator.check_token(user, token):
                x = form.save(commit=False)
                x.post = Post.objects.get(num=request.POST.get('post_id'))
                x.save()
                return JsonResponse({
                    'status': 0,
                    'post_id': request.POST.get('post_id')
                })

            else:
                return JsonResponse({
                    'status': -1,
                    'message': 'you are not logged in'
                })
        else:
            return JsonResponse({
                'status':
                -1,
                'message':
                list(form.errors.keys())[0] + ": " +
                form.errors[list(form.errors.keys())[0]][0]
            })
Ejemplo n.º 8
0
def post_detail(request, post_id):
    post = get_object_or_404(Post, id=post_id)

    if request.method == 'POST':
        form = CommentForm(request.POST)

        if form.is_valid():
            data = form.save(commit=False)
            data.post = post
            data.save()

    else:
        form = CommentForm()

    context = {'post': post, 'form': form}

    return render(request, 'blogs/post_detail.html', context=context)
Ejemplo n.º 9
0
def blog_detail(request, pk):
    post = Post.objects.get(pk=pk)
    comments = Comment.objects.filter(post=post)

    form = CommentForm()
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = Comment(
                author=form.cleaned_data["author"],
                body=form.cleaned_data["body"],
                post=post,
            )
            comment.save()

    context = {"post": post, "comments": comments, "form": form}
    return render(request, "blog_detail.html", context)
Ejemplo n.º 10
0
def new_comment(request, post_id):
    """Add a new comment."""
    post = get_object_or_404(Post, id=post_id)
    if request.method != 'POST':
        # No data submitted; create a blank form.
        form = CommentForm()
    else:
        # POST data submitted; process data.
        form = CommentForm(request.POST)
        if form.is_valid():
            new_comment = form.save(commit=False)
            new_comment.owner = request.user
            new_comment.comment_post = post
            new_comment.save()
            return HttpResponseRedirect(reverse('post', args=[post.id]))

    context = {'form': form}
    return render(request, 'blogs/new_comment.html', context)
Ejemplo n.º 11
0
def create_comment(request, post_id, slug):
    filled_form = CommentForm(request.POST)

    if filled_form.is_valid():
        temp_form = filled_form.save(commit=False)
        temp_form.post = Post.objects.get(id=post_id)
        filled_form.save()

    return redirect('blogs:post_detail', slug)
Ejemplo n.º 12
0
def showPost(request, postid):
    post = Post.objects.get(pk=postid)
    comments = post.comment_set.all()
    comment_form = CommentForm()
    tags = post.tags.all()
    context = {
        'post': post,
        'comments': comments,
        'comment_form': comment_form,
        'tags': tags
    }
    if request.method == "POST":
        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():
            comment = comment_form.save(commit=False)
            comment.user = request.user
            comment.name = request.user
            comment.post = post
            comment_form.save()
    return render(request, 'blogs/show.html', context)
Ejemplo n.º 13
0
def BlogDetailView(request,_id):
    try:
        data =blog.objects.get(id =_id)
        comments = CommentModel.objects.filter(blog = data)
    except blog.DoesNotExist:
        raise Http404('Data does not exist')
     
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            Comment = CommentModel(your_name= request.user,
            comment_text=form.cleaned_data['comment_text'],
            blog=data)
            Comment.save()
            return redirect(f'/blog/{_id}')
    else:
        form = CommentForm()
    context = {
            'data':data,
            'form':form,
            'comments':comments,
        }
    return render(request,'detailview.html',context)
Ejemplo n.º 14
0
    def post(self, request, slug):
        if request.user.is_authenticated:
            comment = request.POST.get('comment')
            blog = Blog.objects.filter(is_active=True, slug=slug).first()
            comment_json = {
                'content': comment,
                'blog': blog,
                'user': request.user
            }
            form = CommentForm(comment_json)
            if form.is_valid() and request.recaptcha_is_valid:
                form.save()
                messages.success(request,
                                 'Comment added successfully.',
                                 extra_tags='comment')
            return redirect(reverse('blogs:blog-detail', args=[slug]))

        return redirect('{}?next={}'.format(
            reverse('login'), reverse('blogs:blog-detail', args=[slug])))
Ejemplo n.º 15
0
 def get(self, request, pk) :
     x = Blog.objects.get(id=pk)
     comments = Comment.objects.filter(blog=x).order_by('-updated_at')
     comment_form = CommentForm()
     context = { 'blog' : x, 'comments': comments, 'comment_form': comment_form }
     return render(request, self.template_name, context)
Ejemplo n.º 16
0
 def get_context_data(self, **kwargs):
     context = super().get_context_data(**kwargs)
     context['commentform'] = CommentForm()
     context['recommentform'] = ReCommentForm()
     return context