Beispiel #1
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.comment.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()
            csubmit = True

    else:
        form = CommentForm()
    return render(request, 'Blog/post_detail.html', {
        'post': post,
        'form': form,
        'comments': comments,
        'csumit': csubmit
    })
Beispiel #2
0
def blog_details(request, slug):
    blog = Blog.objects.get(slug=slug)
    comment_form = CommentForm()
    already_liked = Likes.objects.filter(blog=blog, user=request.user)
    if already_liked:
        liked = True
    else:
        liked = False

    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.blog = blog
            comment.save()
            return HttpResponseRedirect(
                reverse('Blog:blog_details', kwargs={'slug': slug}))
    return render(request,
                  'Blog/blog_details.html',
                  context={
                      'blog': blog,
                      'comment_form': comment_form,
                      'liked': liked
                  })
Beispiel #3
0
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)

    new_comment = None
    # List of active comments for this post
    comments = post.comments.filter(active=True)
    if request.method == 'POST':
        comment_form = CommentForm(data=request.POST)
        if comment_form.is_valid():
            #  create comment object from comment_form object with out saving to db.
            new_comment = comment_form.save(commit=False)
            new_comment.post = post
            new_comment.save()
    else:
        comment_form = CommentForm()
    return render(
        request, 'blog/post/detail.html', {
            'post': post,
            'comments': comments,
            'new_comment': new_comment,
            'comment_form': comment_form
        })
Beispiel #4
0
def add_comment(request, article_id):
    if request.POST and ('pause' not in request.session):
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.comments_article = Article.objects.get(id=article_id)
            form.save()
            request.session.set_expiry(30)
            request.session['pause'] = True
    return redirect('/articles/%s/' % article_id)
Beispiel #5
0
def add_comment_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, 'blog/comment_form.html', {'form': form})
Beispiel #6
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.author = request.user
            comment.save()
            return redirect('Blog:post_detail', pk=post.pk)
    else:
        form = CommentForm()
    return render(request, 'Blog/comment_form.html', {'form': form})
Beispiel #7
0
def create_comment(request, id):
    # getting the post object from the URL param 'id'.
    # You can only create a comment for a post right?
    post = get_object_or_404(BlogPost, id=id)
    # this is the form to submit a comment
    form = CommentForm(request.POST or None)
    if request.method == 'POST':
        # checking if form is vaild on submission
        if form.is_valid():
            comment = form.save(commit=False)
            # setting the proper post variable
            comment.post = post
            comment.save()
            print('form valid')
            # all was saved so we redirect
            return redirect(reverse('post_view'))
    # render the template
    return render(request, 'create_comment.html', {'form': form})
Beispiel #8
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/blog_details.html', ctx)
Beispiel #9
0
def blogpost_detail(request, slug):
    post = get_object_or_404(BlogPost, slug=slug)
    comments = post.comments_on_blog.filter(active=True, parent__isnull=True)
    is_liked = False
    if post.likes.filter(id=request.user.id).exists():
        is_liked = True
    if request.method == 'POST':
        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():
            parent_obj = None

            try:
                parent_id = int(request.POST.get('parent_id'))
            except:
                parent_id = None


            if parent_id:
                parent_obj = Comment.objects.get(id=parent_id)
                if parent_obj:
                    replay_comment = comment_form.save(commit=False)
                    replay_comment_name = request.user
                    replay_comment.parent = parent_obj
            new_comment = comment_form.save(commit=False)            
            new_comment.post = post
            new_comment.name = request.user
            
            new_comment.save()
            return redirect(post.get_absolute_url())
    else:
        comment_form = CommentForm()
    return render(request,
                  'blog/blogpost_detail.html',
                   {'blogpost': post,
                   'comments': comments,
                   'is_liked':is_liked,
                   'comment_form': comment_form})