Ejemplo n.º 1
0
def add_comment_to_post(request, post_id):
    if request.method == 'POST':
        form = CommentForm(
            request.POST, initial={'comment_blog': post_id, 'reply': post_id})
        if request.user.is_authenticated():
            form = CommentForm(
                request.POST,
                initial={'comment_blog': post_id, 'reply': post_id,
                         'comment_mail': request.user.email,
                         'comment_name': request.user})
        if form.is_valid():
            form.save()
            messages.success(request, 'Your comment is added!')
            return HttpResponseRedirect("/blog/" + post_id)

        messages.warning(request, 'Something went wrong!')
        return render(request, "BlogApp/blog.html", {'form': form})
    else:
        # if user, there is no need for mail and name.
        if request.user.is_authenticated():
            form = CommentForm(
                initial={'comment_blog': post_id,
                         'comment_mail': request.user.email,
                         'comment_name': request.user})
        else:
            form = CommentForm(initial={'comment_blog': post_id})

    return form
Ejemplo n.º 2
0
def add_comment_to_post(request, post_id):
    if request.method == 'POST':
        form = CommentForm(request.POST,
                           initial={
                               'comment_blog': post_id,
                               'reply': post_id
                           })
        if request.user.is_authenticated():
            form = CommentForm(request.POST,
                               initial={
                                   'comment_blog': post_id,
                                   'reply': post_id,
                                   'comment_mail': request.user.email,
                                   'comment_name': request.user
                               })
        if form.is_valid():
            form.save()
            messages.success(request, 'Your comment is added!')
            return HttpResponseRedirect("/blog/" + post_id)

        messages.warning(request, 'Something went wrong!')
        return render(request, "BlogApp/blog.html", {'form': form})
    else:
        # if user, there is no need for mail and name.
        if request.user.is_authenticated():
            form = CommentForm(
                initial={
                    'comment_blog': post_id,
                    'comment_mail': request.user.email,
                    'comment_name': request.user
                })
        else:
            form = CommentForm(initial={'comment_blog': post_id})

    return form
Ejemplo n.º 3
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()
            csubmit = True
    else:
        form = CommentForm()

    # return render (request,'BlogApp/post_detail.html',{'post':post,})
    return render(request, 'BlogApp/post_detail.html', {
        'post': post,
        "form": form,
        "csubmit": csubmit,
        'comments': comments
    })
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)
        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, 'BlogApp/comment_form.html', {'form': form})
Ejemplo n.º 5
0
def dashboard(request, year, month, day, post, lk=0, dis=0):
    post = get_object_or_404(
        BlogPost1,
        slug=post,
        status='published',
        publish__year=year,
        publish__month=month,
        publish__day=day,
    )
    total_view = post.TotalView
    if str(lk) == "0" and str(dis) == "0":
        total_view = post.TotalView + 1
        post.TotalView = total_view

    if str(lk) == "0" and str(dis) == "0":
        if request.user in post.Likes.all():
            lk = "1"
        elif request.user in post.Dis_Likes.all():
            dis = "1"

    if request.user in post.Likes.all() and str(dis) == "1":
        post.Likes.remove(request.user)
        post.Dis_Likes.add(request.user)
    elif str(lk) == "1":
        post.Dis_Likes.add(request.user)

    if request.user in post.Dis_Likes.all() and str(lk) == "1":
        post.Dis_Likes.remove(request.user)
        post.Likes.add(request.user)
    elif str(dis) == "1":
        post.Dis_Likes.add(request.user)

    total_like = post.total_likes()
    total_dislike = post.total_dislikes()
    lst = ["0", "1"]

    post.save()
    if request.user.is_authenticated:
        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.name = request.user.username
                new_comment.email = request.user.email
                new_comment.save()
                csubmit = True
        else:
            form = CommentForm()
        return render(
            request, 'Blog/dashboard.html', {
                'post': post,
                'form': form,
                'csubmit': csubmit,
                'comments': comments,
                'total_like': total_like,
                "total_dislike": total_dislike,
                'total_view': total_view,
                'dis': dis,
                'lk': lk,
                'lst': lst,
            })
    else:
        messages.success(request, 'Login Required')
        return HttpResponseRedirect('/login/')