Example #1
0
def add_comment_to_post(request, pk):
    # either get post object with corresponding pk or show 404 page
    post = get_object_or_404(Post, pk=pk)
    #if someone filled out the form and hit the submit button (method == 'POST')
    if request.method == 'POST':
        #pass the request to the Comment form and make it form
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            # post object is in the comment form as a foreign key from Post model
            # So here we say because the form is valid make the comment.post equal to the post itself
            comment.post = post
            comment.save()
            return redirect('post_detail', pk=post.pk)

    else:
        #if not hit submit yet show the CommentForm (connected in the comment_form.html)
        form = CommentForm()
    return render(request, 'blog/comment_form.html', {'form': form})
Example #2
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()
            notification = Notification()
            notification.person = comment.author
            notification.action = 'đã thêm một bình luận ở bài viết'
            notification.post_title = '\"' + post.title + '\"'
            notification.target = 'post'
            notification.id_target = post.pk
            notification.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = CommentForm()
    return render(request, 'blog/comment_form.html', {'form': form})
Example #3
0
def add_comment_to_post(request, pk):
    # getting the post
    post = get_object_or_404(Post, pk=pk)
    # if it is a post object
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            # setting the relavent post for the comment using COmments models foriegn key
            comment.post = post
            comment.save()
            return redirect('post_detail', pk=post.pk)
    else:
        # if it is a get request
        form = CommentForm()
    #rendering objets got by GET or POST requests
    return render(request,
                  template_name='blog/comment_form.html',
                  context={'form': form})
Example #4
0
def blogPostDetails(request, pk):
    '''Method for rendering the page of blog Posts in detail '''
    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()
            return redirect('blogPostDetails', pk=pk)
    comments = Comment.objects.filter(post=post)
    context = {
        'comments': comments,
        'post': post,
        'form': form,
    }
    return render(request, "blogPostDetails.html", context)
Example #5
0
def get_details(request, blog_id):

    blog = get_object_or_404(Blog, id=blog_id)

    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            cleaned_data = form.cleaned_data
            cleaned_data['blog'] = blog
            Comment.objects.create(**cleaned_data)
    else:
        form = CommentForm()

    content = {
        'blog': blog,
        'comments': blog.comment_set.all().order_by('-created'),
        'form': form
    }
    return render(request, 'blogs/detail.html', content)
Example #6
0
def addcomment(request, article_id):
    if request.method == "POST":
        form = CommentForm(request.POST)

        if form.is_valid():
            comment = form.save(commit=False)
            comment.author = request.user
            comment.recipe_id = article_id
            form.save()
    return HttpResponseRedirect('/post/%s/' % article_id)
Example #7
0
def get_details(request, blog_id):
    try:
        blog = Blog.objects.get(id=blog_id)
    except Blog.DoesNotExist:
        raise Http404
    if request.method == 'GET':
        form = CommentForm()
    else:
        form = CommentForm(request.POST)
        if form.is_valid():
            cleaned_data = form.cleaned_data
            cleaned_data['blog'] = blog
            Comment.objects.create(**cleaned_data)
    ctx = {
        'blog': blog,
        'comments': blog.comment_set.all().order_by('-created'),
        'form': form
    }
    return render(request, 'blog_details.html', ctx)
Example #8
0
def add_comment_to_post(request, pk):

    post = get_object_or_404(
        Post,
        pk=pk)  #gets post of primary key 'pk' object or gives a 404 error

    if request.method == 'POST':
        form = CommentForm(request.POST)  #grabs the data posted
        if form.is_valid():
            comment = form.save(
                commit=False
            )  #creates a form called comment but does not commit data
            comment.post = post  #Comment model has an attribute called post that is a foreign key. This makes that foreign key equal to the post
            comment.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = CommentForm()

    return render(request, 'blog/comment_form.html', {'form': form})
Example #9
0
def add_comment_to_post(request, pk):
    form = CommentForm()
    post = get_object_or_404(Post, pk=pk)

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

        if form.is_valid():
            text = form.cleaned_data['comment']
            comment = Comment()
            comment.comment = text
            comment.post = post
            comment.author = request.user
            comment.save()

            return redirect('post_detail', pk=pk)
        else:
            CommentForm()
    return render(request, 'blog/comment_add.html', {'form': form})
def edit_comment(request, slug, pk):
    post = Post.objects.get(slug=slug)
    comment = Comment.objects.get(pk=pk)
    if comment.user != request.user:
        raise Http404
    if request.method == 'POST':
        form = CommentForm(data=request.POST, instance=comment)
        if form.is_valid():
            form.save()
            message = f"Your comment has been edited!"
            messages.add_message(request, messages.SUCCESS, message)
            return redirect('post_detail', slug)
    else:
        form = CommentForm(instance=comment)

    return render(request, 'posts/edit_comment.html', {
        'comment': comment,
        'form': form,
    })
Example #11
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)
Example #12
0
def create_comment(request):
    blog_post = Article.objects.get(pk=request.POST['blog_post_id'])
    form = CommentForm(request.POST)
    if form.is_valid():
        comment = form.instance
        comment.blog_post = blog_post
        comment.save()
        return HttpResponseRedirect('/post/' + request.POST['blog_post_id'])
    else:
        return render(request, 'blog_post.html', {'form': form})
Example #13
0
def blog(request, blog_slug):
    blog = Blog.objects.get(slug=blog_slug)

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

        if form.is_valid():
            comment = form.save(commit=False)
            comment.blog = blog
            comment.save()
            form = CommentForm()
    else:
        form = CommentForm()
    context = {
        'categories': Category.objects.order_by('title'),
        'blog': Blog.objects.get(slug=blog_slug),
        'form': form
    }
    return render(request, 'blog.html', context)
Example #14
0
def post_detail_view(request,year,month,day,post):
    post=get_object_or_404(Post,slug=post,
                                status='published',
                                publish__year=year,
                                publish__month=month,
                                publish__day=day)

    comments = post.comments.filter(active=True)
    csubmit = False
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            new_comment = form.save(commit=False)
            new_comment.post = post
            new_comment.save()
    else:
        form = CommentForm()

    return render(request,'blog/post_detail.html',{'post':post,'form':form,'csubmit':csubmit,'comments':comments})
Example #15
0
 def post(self, request, *args, **kwargs):
     if not request.user.is_authenticated:
         return HttpResponseRedirect(reverse('login'))
     else:
         comment_form = CommentForm(request.POST)
         if comment_form.is_valid():
             PostComment.objects.create(comment_text=comment_form.cleaned_data['comment_text'],
                                        post=super().get_object(),
                                        author=request.user)
         return super().get(self, request, *args, **kwargs)
Example #16
0
 def test_comment_form_with_valid_data(self):
     """
     Test comment form with valid data
     """
     form = CommentForm({
         'name': 'John Doe',
         'email': '*****@*****.**',
         'body': 'Some Comment'
     })
     self.assertTrue(form.is_valid())
Example #17
0
def add_comment_to_post(request,pk):
    '''
    Adding a comment to the post 
    '''

    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,'blog/comment_form.html',{'form':form})
Example #18
0
def article_detail(request,pk):
    article = Article.objects.get(pk=pk)
    comments = Comment.objects.filter(article__pk=pk)
    return render( request,'article_detail.html',
                   {
                    'article':article,
                    'comments':comments,
                    'commentform':CommentForm({'article':pk})
                   } 
                 )
Example #19
0
 def get_context_data(self, *, object_list=None, **kwargs):
     context = super().get_context_data()
     post = context['post']
     context['form'] = CommentForm()
     context['comments'] = post.comments.filter(is_confirmed=True)
     context['settings'] = post.post_setting
     context['category'] = post.category
     context['author'] = 'post.author'
     context['categories'] = Category.objects.all()
     return context
Example #20
0
def detail_blog_view(request, slug):

    context = {}

    blog_post = get_object_or_404(BlogPost, slug=slug)
    form = CommentForm()
    context['blog_post'] = blog_post
    context['form'] = form

    return render(request, 'blog/detail_blog.html', context)
Example #21
0
def add_comment_to_post(request, pk):
    current_user = request.user
    currentUser = current_user.first_name
    print(currentUser)
    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.author = currentUser
            comment.approve()
            comment.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = CommentForm()

        return render(request, 'blog/comment_form.html', {'form': form})
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():
            # Save the form
            comment = form.save(commit=False)
            # Model Comment has an attribute 'post' which is a ForeignKey
            # To the actual Post. This means literally make comment.post
            # equal to Post
            comment.post = post
            # Save all the information
            comment.save()
            return redirect('post_detail', pk=post.pk)
    else:
        # Create the form and just open the comment_form.html section
        # with the form to be filled up again
        form = CommentForm()
    return render(request, 'blog/comment_form.html', {'form': form})
Example #23
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)
Example #24
0
def post(request, post_id):
    post = get_object_or_404(Post, pk=post_id)
    comments = post.comment_set.all()

    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.author = request.user
            comment.date_published = timezone.now()
            comment.save()
            return HttpResponseRedirect(
                reverse('blog:post', args=(comment.post.id, )))

    else:
        form = CommentForm()
        context = {'post': post, 'comments': comments, 'form': form}
        return render(request, 'blog/post_detail.html', context)
Example #25
0
def blog_detail(request, pk):
    post = Post.objects.get(pk=pk)

    form = CommentForm()

    context = {
        "post": post,
        "form": form,
    }
    return render(request, "blog_detail.html", context)
Example #26
0
    def post(self, request, *args, **kwargs):
        form = CommentForm(request.POST or None)
        if form.is_valid():

            post = get_object_or_404(Post, pk=kwargs['pk'])
            reply_id = request.POST.get('comment_id')
            comment_parent = None
            if reply_id:
                comment_parent = Comment.objects.get(id=reply_id)

            post.comment_set.create(Name=form.cleaned_data['Name'],
                                    email=form.cleaned_data['email'],
                                    body=form.cleaned_data['body'],
                                    reply=comment_parent)

            messages.success(request, 'Comment successful')
            images = Images.objects.filter(post=post)
            form = CommentForm()
            comments = Comment.objects.filter(post=post,
                                              reply=None).order_by('-id')
            context = {
                'post': post,
                'images': images,
                'form': form,
                'comments': comments
            }
            return render(request, 'blog/post_detail.html', context)
        form = CommentForm()
        post = get_object_or_404(Post, pk=kwargs['pk'])

        messages.warning(request, 'Some error occured')
        comments = Comment.objects.filter(post=post,
                                          reply=None).order_by('-id')
        images = Images.objects.filter(post=post)

        context = {
            'post': post,
            'images': images,
            'form': form,
            'comments': comments
        }
        return render(request, 'blog/post_detail.html', context)
Example #27
0
def post_detail(request, pk):
    post = get_object_or_404(Blog, pk=pk)
    comments = comment.objects.filter(post=post).order_by('-id')[:5]
    comment_full = comment.objects.filter(post=post).order_by('-id')
    comment_count = comment.objects.filter(post=post)
    comment_actual_count = comment_count.count()
    there_are_comments = False
    if comment_actual_count > 0:
        there_are_comments = True
    likes = post.likes.count()
    dislikes = post.dislikes.count()
    score = likes - dislikes
    if request.method == "POST":
        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():
            content = request.POST.get('content')
            new_comment = comment.objects.create(post=post,
                                                 user=request.user,
                                                 content=content)
            new_comment.save()
            return HttpResponseRedirect(Blog.redirect_route(blog_id=pk))
    else:
        comment_form = CommentForm()

    is_commented = False
    if comment.objects.filter(user=request.user.id, post=post).exists():
        is_commented = True

    context = {
        'score': score,
        'there_are_comments': there_are_comments,
        'post': post,
        'is_liked': post.likes.filter(id=request.user.id).exists(),
        'is_disliked': post.dislikes.filter(id=request.user.id).exists(),
        'likes': post.likes,
        'dislikes': post.dislikes,
        'comments': comments,
        'comment_form': comment_form,
        'comment_full': comment_full,
        'is_commented': is_commented
    }
    return render(request, 'blog/post_detail.html', context)
Example #28
0
def view_post(request, pk):
    template_name = 'blog/post_detail.html'
    post = get_object_or_404(Post, pk=pk)
    form = CommentForm(request.POST or None)
    if form.is_valid():
        form.instance.post = post
        form.instance.commented_by = request.user
        form.save()
        return redirect('view_post', pk=pk)
    comments = Comment.objects.filter(post=post)
    return render(request, template_name, {'post': post, 'form': form, 'comments': comments})
Example #29
0
def create_comment(request):
    article = Article.objects.get(pk=request.POST['post_id'])
    form = CommentForm(request.POST)
    path = '/posts/' + str(article.pk)
    if form.is_valid():
        new_comment = form.save(commit=False)
        new_comment.article = article
        new_comment.save()
        return HttpResponseRedirect(path)
    else:
        print(form.errors)
Example #30
0
 def post(self, request):
     a=request.POST.get('post')
     print a
     if request.GET:
         print "get"
     if request.POST:
         print "post"
     create_comment = CommentForm(request.POST)
     if create_comment.is_valid():
         create_comment.save()
     return HttpResponseRedirect("/blog/"+str(a)+"/mainblog2/")