def post(self, request, *args, **kwargs): post = get_object_or_404( Post, slug=kwargs['post'], status='published', publish__year=kwargs['year'], publish__month=kwargs['month'], publish__day=kwargs['day'], ) comments = post.comment_set.filter(active=True) new_comment = None comment_form = CommentForm(data=request.POST) if comment_form.is_valid(): new_comment = comment_form.save(commit=False) new_comment.post = post new_comment.save() return render( request, 'blog/post/detail.html', { 'post': post, 'comments': comments, 'new_comment': new_comment, 'comment_form': comment_form }, )
def comment_post(request, post_id): form = CommentForm(request.POST or None) if form.is_valid(): comment = form.save(commit=False) comment.author = request.user post = get_object_or_404(Post, id=post_id) comment.post = post comment.save() return redirect(reverse("blog_posts")) return render(request, "blog/comment_post.html", {'form': form})
def view_post(request, post_id): """View a single post, commenting DOES NOT requiere validation from author >>Must do: Author CAN remove comment""" post_instance = get_object_or_404(Post, id=post_id) form = CommentForm(request.POST or None) if form.is_valid(): user = request.user comment = form.save(commit=False) comment.author = user comment.post = post_instance comment.save() return redirect(reverse("view_post", kwargs={'post_id': post_id}), {'post': post_instance, 'form': form}) return render(request, "blog/view_post.html", {'post': post_instance, 'form': form})
def submit_comment_reply(request): response = {} if not check_bibot_response(request): response['bibot_err'] = 'error' return HttpResponse(json.dumps(response), content_type="application/json") if request.POST['is_comment'] == 'True': form = CommentForm(request.POST) else: form = ReplyForm(request.POST) response.update(request.POST) if form.is_valid(): m = form.save() if request.POST['is_comment'] == 'True': response['comment'] = comment_dictionary(m) else: response['reply'] = reply_dictionary(m) return HttpResponse(json.dumps(response), content_type="application/json")
def post_detail(request, year, month, day, post): post = get_object_or_404(Post, slug=post, status='published', publish__year=year, publish__month=month, publish__day=day) # List of active comments for this post comments = post.comments.filter(active=True) new_comment = None if request.method == 'POST': # A comment was posted comment_form = CommentForm(data=request.POST) if comment_form.is_valid(): # Create Comment object but don't save to database yet new_comment = comment_form.save(commit=False) # Assign the current post to the comment new_comment.post = post # Save the comment to the database new_comment.save() else: comment_form = CommentForm() # List of similar posts post_tags_ids = post.tags.values_list('id', flat=True) similar_posts = Post.published.filter(tags__in=post_tags_ids) \ .exclude(id=post.id) similar_posts = similar_posts.annotate(same_tags=Count('tags')) \ .order_by('-same_tags', '-publish')[:4] return render( request, 'blog/post/detail.html', { 'post': post, 'comments': comments, 'new_comment': new_comment, 'comment_form': comment_form, 'similar_posts': similar_posts })