Example #1
0
def newComment(request, pk):
    blog = get_object_or_404(Blog, pk=pk)

    # If this is a POST request then process the Form data
    if request.method == 'POST':

        # Create a form instance and populate it with data from the request (binding):
        form = NewCommentForm(request.POST)

        #Check if the form is valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required (here we just write it to the model due_back field)
            new_comment = Comment.objects.create(
                blog=blog,
                comment=form.cleaned_data['comment'],
                author=request.user)
            new_comment.save()

            # redirect to a new URL
            return HttpResponseRedirect(reverse('blog-detail', args=[str(pk)]))

    # If this is a GET (or any other method) create the default form.
    else:
        form = NewCommentForm()

    context = {
        'form': form,
        'blog': blog,
    }

    return render(request, 'blog/comment_form.html', context=context)
def recomment(comment):
    form = NewCommentForm()
    comment = Comment.query.filter_by(id=comment).first()
    if form.validate_on_submit():
        content = form.comment.data
        recomment = ReComment(content=content,
                              comment_id=comment.id,
                              user_id=current_user.id)
        db.session.add(recomment)
        db.session.commit()
        flash("cevap eklendi", "success")
        return redirect(url_for("show_article", article=comment.topost.id))
Example #3
0
 def get_context_data(self, **kwargs):
     data = super().get_context_data(**kwargs)
     comments_connected = Comment.objects.filter(
         post_connected=self.get_object()).order_by('data_posted')
     data['comments'] = comments_connected
     if self.request.user.is_authenticated:
         data['form'] = NewCommentForm(instance=self.request.user)
     return data
Example #4
0
 def get_context_data(self, **kwargs):
     context = super().get_context_data(**kwargs)
     comments_connected = Comments.objects.filter(
         post_connected=self.get_object()).order_by('-date_posted')
     context.update({
         'blog_nav': 'active',
         'comments': comments_connected,
         'form': NewCommentForm(instance=self.request.user),
     })
     return context
def show_article(article):
    post = Post.query.filter_by(id=article).first_or_404()
    form = NewCommentForm()
    if form.validate_on_submit():
        content = form.comment.data
        comment = Comment(content=content,
                          post_id=post.id,
                          user_id=current_user.id)
        db.session.add(comment)
        db.session.commit()
        flash('Comment added', 'success')
        return redirect(url_for('show_article', article=post.id))
    comments = Comment.query.filter_by(post_id=post.id).all()
    recomments = ReComment.query.all()
    return render_template("public/article.html",
                           post=post,
                           comments=comments,
                           form=form,
                           recomments=recomments)
Example #6
0
def new_comment(request, name, id):
    blog = Blog.objects.get(name = name)
    post = Post.objects.get(id = id)
    if request.method == 'POST':
        form = NewCommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.save()
            post.comments += 1
            post.save()
        else:
            return HttpResponse("Incorrect form!!")
    else:
        form = NewCommentForm({'post':post})
        all_comments = post.comment_set.all().order_by('-created')
        return render_to_response('blog/new_comment.html', {'form': form,
                                                            'post': post,
                                                            'blog': blog,
                                                            'comments': all_comments,
                                                            }, context_instance = RequestContext(request)
                                  )

    return HttpResponseRedirect(reverse('blog.views.blog', args = (blog.name, )))
Example #7
0
def show_post(request, post_id):
    post = get_object_or_404(Post, pk=post_id)
    add_tag_form = AddTagToPostForm()
    if request.method == 'POST':
        comment = Comment()
        comment.user = request.user
        comment.post = post
        comment.text = request.POST.get('text')
        comment.save()

        text = comment.text
        mentions = list()
        words = str(text).split()
        for word in words:
            if word[0] == '@':
                if User.objects.filter(username=word[1:]).exists():
                    user = User.objects.filter(username=word[1:]).first()
                    notif = Notification()
                    notif.text = 'شما در پست جدیدی منشن شده اید' + \
                                 '<a class="ui animated small right floated button" href="' + reverse('blog:show_post',
                                                                                                      args=[
                                                                                                          post_id]) + '">' \
                                 + '<div class="visible content">' + 'مشاهده پست' + '</div>' \
                                 + '<div class="hidden content"> \
                                    <i class="right arrow icon"></i> \
                                    </div>'                                            + '</a>'
                    notif.user = user
                    notif.seen = False
                    notif.save()

    comments = Comment.objects.filter(post=post)
    return render(
        request, 'blog/single_post.html', {
            'post': post,
            'comments': comments,
            'new_comment_form': NewCommentForm(),
            'add_tag_form': add_tag_form
        })
Example #8
0
def save_comment_task(form_data, post_id):
    comment_form = NewCommentForm(form_data)
    post = Post.objects.get(id=post_id)
    user_comment = comment_form.save(commit=False)
    user_comment.post = post
    user_comment.save()
Example #9
0
 def test_new_comment_form_comment_help_text(self):
     form = NewCommentForm()
     self.assertEqual(form.fields['comment'].help_text,
                      'Enter a comment about blog post here.')
Example #10
0
 def test_new_comment_form_max_over(self):
     sample_comment = "hello" * 1000
     form = NewCommentForm(data={'comment': sample_comment})
     self.assertTrue(form.is_valid())