示例#1
0
    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
            },
        )
示例#2
0
文件: views.py 项目: ercanbasa/blog
def post_detail(request, template="post_detail.html", pk=None):
    post = get_object_or_404(Post, pk=pk)
    ctx = {'post': post}

    if not request.method == "POST":
        if request.user.is_authenticated():
            form = CommentForm()
        else:
            form = AnonymousCommentForm()

    else:
        if request.user.is_authenticated():
            form = CommentForm(request.POST)
        else:
            form = AnonymousCommentForm(request.POST)

        if form.is_valid():
            obj = form.instance
            obj.content_object = post
            if request.user.is_authenticated():
                obj.writer = Profile.objects.from_request(request)
                messages.add_message(
                    request, messages.SUCCESS,
                    _("Your comment has been saved successfully."))
            else:
                obj.activation_key = hashlib.sha1(str(
                    random.random())).hexdigest()
                obj.is_verified = False

                subject = 'Email confirmation'
                message = """
                    Hello %s,\nConfirm your comment via the link below\n
                    http://%s%s""" % (obj.anonymous_name,
                                      request.META['HTTP_HOST'],
                                      reverse('comment_activation',
                                              kwargs={
                                                  'key': obj.activation_key,
                                                  'post_id': post.id
                                              }))
                sender = '*****@*****.**'
                recipients = [obj.anonymous_mail]
                mail_sender.delay(subject, message, sender, recipients)

                messages.add_message(
                    request, messages.SUCCESS,
                    _("Your comment has been saved but it will not be "
                      "shown before your mail confirmation."))
            obj.save()

        if request.user.is_authenticated():
            form = CommentForm()
        else:
            form = AnonymousCommentForm()

    ctx.update({'form': form})
    return render(request, template, ctx)
示例#3
0
文件: views.py 项目: ercanbasa/blog
def comment_to_comment(request,
                       template="comment_to_comment.html",
                       pk=None):
    comment = get_object_or_404(PostComment, pk=pk)
    post_id = comment.object_id

    if not request.method == "POST":
        if request.user.is_authenticated():
            form = CommentForm()
        else:
            form = AnonymousCommentForm()

    else:
        if request.user.is_authenticated():
            form = CommentForm(request.POST)
        else:
            form = AnonymousCommentForm(request.POST)

        if form.is_valid():
            obj = form.instance
            obj.content_object = comment
            if request.user.is_authenticated():
                obj.writer = Profile.objects.from_request(request)
                messages.add_message(
                    request,
                    messages.SUCCESS,
                    _("Your comment has been saved successfully."))
            else:
                obj.activation_key = hashlib.sha1(
                    str(random.random())).hexdigest()
                obj.is_verified = False

                subject = 'Email confirmation'
                message = """
                    Hello %s,\nConfirm your comment via the link below\n
                    http://%s%s""" % (
                    obj.anonymous_name,
                    request.META['HTTP_HOST'],
                    reverse('comment_activation',
                            kwargs={'key': obj.activation_key,
                                    'post_id': post_id}))
                sender = '*****@*****.**'
                recipients = [obj.anonymous_mail]
                mail_sender.delay(subject, message, sender, recipients)

                messages.add_message(
                    request,
                    messages.SUCCESS,
                    _("Your comment has been saved but it will not be "
                      "shown before your mail confirmation."))
            obj.save()
            return redirect('post_detail', pk=post_id)

    ctx = {'form': form}
    return render(request, template, ctx)
示例#4
0
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})
示例#5
0
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})
示例#6
0
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")
示例#7
0
 def get_context_data(self, **kwargs):
     if self.request.user.is_authenticated:
         form = UserCommentForm()
     else:
         form = CommentForm()
     context = {
         'page_template':
         self.page_template,
         'comment_form':
         form,
         'comments':
         Comment.objects.filter(post=self.object.id).select_related()
     }
     return super(BlogDetailView, self).get_context_data(**context)
示例#8
0
 def post(self, request, *args, **kwargs):
     self.object = post = self.get_object()
     if request.user.is_authenticated:
         form = UserCommentForm(request.POST)
     else:
         form = CommentForm(request.POST)
     if form.is_valid():
         comment = form.save(commit=False)
         comment.post = post
         if request.user.is_authenticated:
             comment.user = request.user
             comment.user_name = request.user
             comment.user_email = request.user.email
         comment.ip = '0.0.0.0'
         comment.save()
         return redirect(post.get_absolute_url())
     context = self.get_context_data(object=post)
     context['comment_form'] = form
     return self.render_to_response(context)
示例#9
0
 def get(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'],
     )
     comment_form = CommentForm()
     comments = post.comment_set.filter(active=True)
     new_comment = None
     return render(
         request,
         'blog/post/detail.html',
         {
             'post': post,
             'comments': comments,
             'new_comment': new_comment,
             'comment_form': comment_form
         },
     )
示例#10
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)

    # 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
        })
示例#11
0
 def test_valid_form(self):
     data = {'body': 'lorem ipsum'}
     form = CommentForm(data=data)
     self.assertTrue(form.is_valid())
示例#12
0
 def test_invalid_form(self):
     data = {'body': ''}
     form = CommentForm(data=data)
     self.assertFalse(form.is_valid())