Ejemplo n.º 1
0
def post_detail_view(request, post, year, month, day):
    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
            messages.success(request, f' Post Created Successfully !')
    else:
        form = CommentForm()
    is_liked = False
    if post.likes.filter(id=request.user.id).exists():
        is_liked = True
    return render(
        request, 'blog/detail_view.html', {
            'post': post,
            'form': form,
            'csubmit': csubmit,
            'comments': comments,
            'is_liked': is_liked,
            'total_likes': post.total_likes()
        })
Ejemplo n.º 2
0
def post_detail(request, pk):
  template_name = 'blog_app/post_detail.html'
  post = get_object_or_404(Post, pk=pk)
  comments = post.comments.filter(active=True)
  new_comment = None
  # Comment posted
  if request.method == 'POST':
    comment_form = CommentForm(data=request.POST)
    if comment_form.is_valid():
      comment_form.instance.name = request.user
      # 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()
      return redirect('blog-detail', pk=post.pk)
  else:
    comment_form = CommentForm()

  return render(request, 
                template_name, 
                  {
                  'post': post,
                  'comments': comments,
                  'new_comment': new_comment,
                  'comment_form': comment_form,
                  'User' : str(request.user),
                  }
                )
Ejemplo n.º 3
0
def post_detail(request, slug):
    template_name = 'post_detail.html'
    post = get_object_or_404(Post, slug=slug)
    comments = post.comments.filter(active=True)
    new_comment = None
    # Comment posted
    if request.method == 'POST':
        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.slug=slugify(new_comment.title)
            new_comment.post = post
            # Save the comment to the database
            new_comment.save()
    else:
        comment_form = CommentForm()
    print(comment_form)
    # print(post)

    return render(
        request, template_name, {
            'post': post,
            'comments': comments,
            'new_comment': new_comment,
            'comment_form': comment_form
        })
Ejemplo n.º 4
0
    def test_invalid_comment_form(self):
        self.client.login(username='******', password='******')

        data = {
            'content': '',
        }

        form = CommentForm(data=data)
        self.assertFalse(form.is_valid())
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.post = post
            comment.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = CommentForm()
    return render(request, 'blog_app/comment_form.html', {'form': form})
Ejemplo n.º 6
0
def add_comment(request, pk):
    post = get_object_or_404(Posts, 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('posts_detail', pk=post.pk)
    else:
        form = CommentForm()
    return render(request, 'blog_app/comments_form.html', {'form': form})
def PostsDetailsView(
    request,
    slug,
):
    page_title = "Post Content"
    post_details = Post.objects.get(slug=slug)
    replies = Comment.objects.filter(reply__isnull=False,
                                     )  # filter replies only not comments
    latest = Post.objects.all().order_by('-published_at')[
        0:3]  # the latest posts
    categories = Post.objects.values(
        'categories__title', 'categories__slug'
    ).annotate(qcount=Count('categories')).order_by(
        '-qcount'
    )  # To show how many posts per category and order_by the highest category in posts count (DESC)
    #↓
    #must be written like this because it is many_too_many field returns with ids not titles

    form = CommentForm(request.POST or None, request.FILES or None)
    if request.method == "POST":
        if form.is_valid():
            profile = Profile.objects.get(
                user=request.user
            )  # if i write it before form is valid it block anynomys users from entering the page
            form.instance.replier = profile  # check if the form is vaild give it the the logged in user profile
            form.instance.post = post_details  # and the post of the comment (both are model fields)
            form.save()
            return redirect(reverse('blog:post_details',
                                    args=(slug, )))  # the way to redirect slug

    def get_client_ip(request):
        x_forwarded_for = request.META.get(
            'HTTP_X_FORWARDED_FOR')  #to get IPAdress to count the post views
        if x_forwarded_for:
            ip = x_forwarded_for.split(',')[0]
        else:
            ip = request.META.get('REMOTE_ADDR')
        return ip

        # to record  one view only per user for every post using his IPAdress

    PostViews.objects.get_or_create(IPAddres=get_client_ip(request),
                                    post=post_details)

    context = {
        'latest_posts': latest,
        'post_details': post_details,
        'categories': categories,
        'comment_form': form,
        'replies': replies,
        'page_title': page_title,
    }
    return render(request, 'post/post_details.html', context)
Ejemplo n.º 8
0
def add_comment_to_post(request, pk):
    post_com = get_object_or_404(Post, pk=pk)
    if request.method == 'POST':
        form_c = CommentForm(request.POST)
        if form_c.is_valid():
            form_comments = form_c.save(commit=False)
            form_comments.post = post_com
            form_comments.save()
            return redirect('blog_app:blog_post', post_com.pk)
    else:
        form_c = CommentForm()
    return render(request, 'blog_app/comments_form.html', {'form': form_c})
Ejemplo n.º 9
0
def addcomment(request, article_id):
    if request.POST:
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.comments_article = Article.objects.get(id=article_id)
            comment.comments_user = UserProfile.objects.get(
                id=auth.get_user(request).id)
            form.save()
            art = Article.objects.get(id=article_id)
            art.article_comments += 1
            art.save()
        return redirect('/blog/articles/get/%s/' % article_id)
def comment_update(request, slug, id):
    comment = Comment.objects.get(id=id)
    form = CommentForm(request.POST or None,
                       request.FILES or None,
                       instance=comment)
    if request.method == "POST":
        if form.is_valid():
            form.save()
            return redirect(reverse('blog:post_details',
                                    args=(slug, )))  # the way to redirect slug
        else:
            form = CommentForm()

    context = {
        'comment_update_form': form,
    }
    return render(request, 'post/comment_update.html', context)
Ejemplo n.º 11
0
def add_comment(request, pk):
    post = get_object_or_404(Post, pk=pk)

    if request.method == 'POST':
        print("Adding comment to post " + str(pk))
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = Comment()
            comment.post = post
            comment.author = form.cleaned_data['author']
            comment.text = form.cleaned_data['text']
            comment.save()
            return redirect('post_detail', pk=post.pk)
    else:
        form = CommentForm()

    return render(request, 'blog_app/comment_form.html', {'form': form})
Ejemplo n.º 12
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.º 13
0
def post_detail(request, year, month, day, slug):
    post = get_object_or_404(Post,
                             slug=slug,
                             status='publish',
                             publish__year=year,
                             publish__month=month,
                             publish__day=day)
    # 列出文章的所有活动的评论
    comments = post.comments.filter(active=True)
    new_comment = None
    if request.method == 'POST':
        comment_form = CommentForm(request.POST)
        if comment_form.is_valid():
            # 通过表单直接创建新的数据对象,但是不保存在数据库中
            new_comment = comment_form.save(commit=False)
            # 设置外键为当前文章
            new_comment.post = post
            # 最后将评论写入数据库
            new_comment.save()
    else:
        comment_form = CommentForm()
    # 显示相近的tag的文章列表
    # flat=True 让结果变成一个列表
    post_tags_ids = post.tags.values_list('id', flat=True)
    similar_tags = Post.published.filter(tags__in=post_tags_ids).exclude(
        id=post.id)
    # 使用count对每个文章按照标签计数,并且生成一个新的字段same_tags用于存放计数的结果
    similar_posts = similar_tags.annotate(same_tags=Count('tags')).order_by(
        '-same_tags', '-publish')[:2]
    return render(
        request, 'blog/post/detail.html', {
            'post': post,
            'comments': comments,
            'new_comment': new_comment,
            'comment_form': comment_form,
            'similar_posts': similar_posts
        })
Ejemplo n.º 14
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_app:blog_details', kwargs={'slug': slug}))
    return render(request,
                  'blog_app/blog_details.html',
                  context={
                      'blog': blog,
                      'comment_form': comment_form,
                      'liked': liked,
                  })
Ejemplo n.º 15
0
Archivo: views.py Proyecto: JKK86/blog
def post_detail(request, year, month, day, slug):
    post = get_object_or_404(
        Post,
        slug=slug,
        status=PUBLISHED,
        publish__year=year,
        publish__month=month,
        publish__day=day,
    )
    user = request.user
    comments = post.comments.filter(active=True)
    tag_ids = post.tags.values_list('id', flat=True)
    similar_posts = Post.published.filter(tags__id__in=tag_ids).exclude(
        id=post.id)
    similar_posts = similar_posts.annotate(same_tags=Count('tags')).order_by(
        '-same_tags', '-publish')[:3]
    if request.method == 'GET':
        if user.is_authenticated:
            form = CommentForm(initial={
                'name': user.username,
                'email': user.email
            })
        else:
            form = CommentForm()
    else:
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            if user.is_authenticated:
                comment.user = user
                comment.active = True
            else:
                messages.info(
                    request, "Twój komentarz oczekuje na zatwierdzenie. "
                    "Zaloguj się jeśli chcesz, aby Twoje komentarze były publikowane bez zwłoki."
                )
                subject = 'Komentarz do zatwierdzenia'
                message = render_to_string(
                    'blog/approve_comment_email.html', {
                        'name': form.cleaned_data['name'],
                        'email': form.cleaned_data['email'],
                        'post': post
                    })
                email_from = '*****@*****.**'
                email_to = [
                    user.email for user in User.objects.filter(is_staff=True)
                ]
                send_mail(subject,
                          message,
                          email_from,
                          email_to,
                          fail_silently=False)
            comment.save()
    return render(
        request, 'blog/post_detail.html', {
            'post': post,
            'form': form,
            'similar_posts': similar_posts,
            'comments': comments
        })