Пример #1
0
def comment_add(request,id):
    article = get_object_or_404(Article,pk=id)
    #add comment
    if request.method=='POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            #comment_parent=Comment(id=cd["comment_parent_id"]),
            post = Comment(
                article=Article(id=id),
                user = User(id=cd["user_id"]),
                email = cd["email"],
                author = cd["author"],
                content = cd["content"]
            )
            if cd["comment_parent_id"] != None:
                post.comment_parent = Comment(id=cd["comment_parent_id"])

            post.save()
            article.comment_count+=1
            article.save()

        print("%r" % form.errors)

    return HttpResponseRedirect("/articles/%d#comment" % int(id))
Пример #2
0
def fake_comments(count=200):
    #一级评论
    for i in range(count):
        comment = Comment(author=fake.name(),
                          body=fake.sentence(),
                          email=fake.email(),
                          show=True,
                          like=random.randint(1, 30),
                          timestamp=fake.date_time_this_year(),
                          post=Post.query.get(
                              random.randint(1, Post.query.count())))
        db.session.add(comment)
    db.session.commit()

    #二级评论
    salt = count * 2
    for i in range(salt):
        # replied = Comment.query.get(random.randint(1, Comment.query.count()))
        replied = random.choice(Comment.query.filter_by(show=True).all())
        comment = Comment(
            author=fake.name(),
            email=fake.email(),
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            show=False,
            like=random.randint(1, 30),
            # replied=Comment.query.get(random.randint(1, Comment.query.count())),
            replied=replied,
            to=random.choice(replied.replies + [replied]).author,
            # post=Post.query.get(random.randint(1, Post.query.count()))
            post=replied.post)
        db.session.add(comment)
    db.session.commit()
Пример #3
0
def fake_comments(count=500):
    for i in range(count):
        comment = Comment(
            author=fake.name(),
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            post=Post.query.get(random.randint(1, Post.query.count()))
        )
        db.session.add(comment)

        # from admin
        comment = Comment(
            author='Whale Liu',
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            from_admin=True,
            post=Post.query.get(random.randint(1, Post.query.count()))
        )
        db.session.add(comment)
    db.session.commit()

    salt = int(count/10)
    # replies
    for i in range(salt):
        comment = Comment(
            author=fake.name(),
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            replied=Comment.query.get(random.randint(1, Comment.query.count())),
            post=Post.query.get(random.randint(1, Post.query.count()))
        )
        db.session.add(comment)
    db.session.commit()
Пример #4
0
Файл: blog.py Проект: zpaul/blog
def post(post_id):
    """View function for post page"""

    #Form object:'Comment'
    form = CommentForm()
    #form.validator_on_submit() will be true and return the
    #data object to form instance form user enter.
    #when the HTTP request is POST
    if form.validate_on_submit():
        new_comment = Comment(id=str(uuid4()), name=form.name.data)
        new_comment.text = form.text.data
        new_comment.date = datetime.datetime.now()
        new_comment.post_id = post_id
        db.session.add(new_comment)
        db.session.commit()

    post = db.session.query(Post).get_or_404(post_id)
    tags = post.tags
    comments = post.comments.order_by(Comment.date.desc()).all()
    recent, top_tags = sidebar_data()

    return render_template('post.html',
                           post=post,
                           tags=tags,
                           comments=comments,
                           form=form,
                           recent=recent,
                           top_tags=top_tags)
Пример #5
0
def show_post(post_id):
    post = Post.query.get_or_404(post_id)
    if post.private and not current_user.is_authenticated:
        flash("你没有权限访问该文章!", "warning")
        return redirect(url_for(".index"))
    page = request.args.get("page", 1, type=int)
    per_page = current_app.config.get("BLOG_COMMENT_PER_PAGE", 15)
    pagination = (
        Comment.query.with_parent(post)
        .filter_by(reviewed=True)
        .order_by(Comment.timestamp.asc())
        .paginate(page, per_page=per_page)
    )
    comments = pagination.items

    if current_user.is_authenticated:
        form = AdminCommentForm()
        form.author.data = current_user.name
        form.email.data = current_app.config["BLOG_EMAIL"]
        form.site.data = url_for(".index")
        from_admin = True
        reviewed = True
    else:
        form = CommentForm()
        from_admin = False
        reviewed = False

    if form.validate_on_submit():
        author = form.author.data
        email = form.email.data
        site = form.site.data
        body = form.body.data
        comment = Comment(
            author=author,
            email=email,
            site=site,
            body=body,
            from_admin=from_admin,
            post=post,
            reviewed=reviewed,
        )
        replied_id = request.args.get("reply")
        if replied_id:
            replied_comment = Comment.query.get_or_404(replied_id)
            comment.replied = replied_comment
            send_new_reply_email(replied_comment)

        db.session.add(comment)
        db.session.commit()
        if current_user.is_authenticated:
            flash("Comment published.", "success")
        else:
            flash("Thanks, your comment will be published after reviewed.", "info")
            send_new_comment_email(post)
        return redirect(url_for(".show_post", post_id=post_id))
    return render_template(
        "blog/post.html", post=post, pagination=pagination, comments=comments, form=form
    )
Пример #6
0
def fake_comments(count=500):
    for i in range(count):
        comment = Comment(
            author=fake.name(),
            email=fake.email(),
            site=fake.url(),
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            reviewed=True,
            post=Post.query.get(random.randint(1, Post.query.count()))
        )
        db.session.add(comment)

    salt = int(count * 0.1)
    for i in range(salt):
        # unreviewed comments
        comment = Comment(
            author=fake.name(),
            email=fake.email(),
            site=fake.url(),
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            reviewed=False,
            post=Post.query.get(random.randint(1, Post.query.count()))
        )
        db.session.add(comment)

        # from admin
        comment = Comment(
            author='Mima Kirigoe',
            email='*****@*****.**',
            site='example.com',
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            from_admin=True,
            reviewed=True,
            post=Post.query.get(random.randint(1, Post.query.count()))
        )
        db.session.add(comment)
    db.session.commit()

    # replies
    for i in range(salt):
        comment = Comment(
            author=fake.name(),
            email=fake.email(),
            site=fake.url(),
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            reviewed=True,
            replied=Comment.query.get(random.randint(1, Comment.query.count())),
            post=Post.query.get(random.randint(1, Post.query.count()))
        )
        db.session.add(comment)
    db.session.commit()
Пример #7
0
def fake_comments(count=500):
    for i in range(count):
        comment = Comment(
            author=fake.name(),
            email=fake.email(),
            site=fake.url(),
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            reviewed=True,
            post=Post.query.get(random.randint(1, Post.query.count())),
        )
        db.session.add(comment)

    salt = int(count * 0.1)
    for i in range(salt):
        comment = Comment(
            author=fake.name(),
            email=fake.email(),
            site=fake.url(),
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            reviewed=False,
            post=Post.query.get(random.randint(1, Post.query.count())),
        )
        db.session.add(comment)

        # 管理员发表的评论
        comment = Comment(
            author="Mima Kirigoe",
            email="*****@*****.**",
            site="example.com",
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            from_admin=True,
            reviewed=True,
            post=Post.query.get(random.randint(1, Post.query.count())),
        )
        db.session.add(comment)
    db.session.commit()

    # 回复
    for i in range(salt):
        comment = Comment(
            author=fake.name(),
            email=fake.email(),
            site=fake.url(),
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            reviewed=True,
            replied=Comment.query.get(random.randint(1,
                                                     Comment.query.count())),
            post=Post.query.get(random.randint(1, Post.query.count())),
        )
        db.session.add(comment)
    db.session.commit()
Пример #8
0
def fake_comments(count=500):
    """生成虚拟评论"""
    for i in range(count):
        comment = Comment(author=fake.name(),
                          email=fake.email(),
                          site=fake.url(),
                          body=fake.sentence(),
                          timestamp=fake.date_time_this_year(),
                          reviewed=True,
                          post=Post.query.get(
                              random.randint(1, Post.query.count())))
        db.session.add(comment)

    salt = int(count * 0.1)  # 未审核评论、管理员评论和回复都是50条

    # 未审核评论
    for i in range(salt):
        comment = Comment(author=fake.name(),
                          email=fake.email(),
                          site=fake.url(),
                          body=fake.sentence(),
                          timestamp=fake.date_time_this_year(),
                          reviewed=False,
                          post=Post.query.get(
                              random.randint(1, Post.query.count())))
        db.session.add(comment)

        # 管理员评论
        comment = Comment(author="Gweid",
                          email="*****@*****.**",
                          site="example.com",
                          body=fake.sentence(),
                          timestamp=fake.date_time_this_year(),
                          from_admin=True,
                          reviewed=True,
                          post=Post.query.get(
                              random.randint(1, Post.query.count())))
        db.session.add(comment)
    db.session.commit()

    # 回复
    for i in range(salt):
        comment = Comment(
            author=fake.name(),
            email=fake.email(),
            site=fake.url(),
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            reviewed=True,
            replied=Comment.query.get(random.randint(1,
                                                     Comment.query.count())),
            post=Post.query.get(random.randint(1, Post.query.count())))
        db.session.add(comment)
    db.session.commit()
Пример #9
0
def show_post(post_id):
    post = Post.query.get_or_404(post_id)
    page = request.args.get('page', 1, type=int)
    per_page = current_app.config['BLUELOG_COMMENT_PER_PAGE']
    pagination = Comment.query.with_parent(post).filter_by(
        reviewed=True).order_by(Comment.timestamp.asc()).paginate(
            page, per_page)
    comments = pagination.items

    if current_user.is_authenticated:
        form = AdminCommentForm()
        form.author.data = current_user.name
        form.email.data = current_app.config['BLUELOG_EMAIL']
        form.site.data = url_for('.index')
        from_admin = True
        reviewed = True
    else:
        form = CommentForm()
        from_admin = False
        reviewed = False

    if form.validate_on_submit():
        author = form.author.data
        email = form.email.data
        site = form.site.data
        body = form.body.data
        comment = Comment(author=author,
                          email=email,
                          site=site,
                          body=body,
                          from_admin=from_admin,
                          post=post,
                          reviewed=reviewed)
        replied_id = request.args.get('reply')
        if replied_id:
            replied_comment = Comment.query.get_or_404(replied_id)
            comment.replied = replied_comment
            send_new_reply_email(replied_comment)
        db.session.add(comment)
        db.session.commit()

        if current_user.is_authenticated:
            flash('Comment pulished', 'success')
        else:
            flash('thanks,you comment will be published after reviewed',
                  'info')
            send_new_comment_email(post)
        return redirect(url_for('.show_post', post_id=post_id))
    return render_template('blog/post.html',
                           post=post,
                           pagination=pagination,
                           form=form,
                           comments=comments)
Пример #10
0
def fake_comments(count=500):
    salt = int(count * 0.1)
    for i in range(count):
        reviewed_comment = Comment(
            author=fake.name(),
            email=fake.email(),
            site=fake.url(),
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            reviewed=True,
            post=Post.query.get(random.randint(1, Post.query.count()))
        )
        db.session.add(reviewed_comment)
    for i in range(salt):
        # comments which are not reviewed
        not_reviewed_comment = Comment(
            author=fake.name(),
            email=fake.email(),
            site=fake.url(),
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            reviewed=False,
            post=Post.query.get(random.randint(1, Post.query.count()))
        )
        db.session.add(not_reviewed_comment)
    db.session.commit()
    # comments from admin
    comment = Comment(
        author='Alan Wang',
        email='*****@*****.**',
        site='example.com',
        body=fake.sentence(),
        timestamp=fake.date_time_this_year(),
        reviewed=True,
        post=Post.query.get(random.randint(1, Post.query.count()))
    )
    db.session.add(comment)
    db.session.commit()
    # replies
    for i in range(salt):
        replied_comment=Comment(
            author=fake.name(),
            email=fake.email(),
            site=fake.url(),
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            reviewed=True,
            post=Post.query.get(random.randint(1, Post.query.count())),
            replied=Comment.query.get(random.randint(1, Comment.query.count())),
        )
        db.session.add(replied_comment)
    db.session.commit()
Пример #11
0
def fake_comments(count=500):
    for i in range(count):
        comment = Comment(author=fake.name(),
                          email=fake.email(),
                          site=fake.url(),
                          body=fake.sentence(),
                          timestamp=fake.date_time_this_year(),
                          reviewed=True,
                          post=Post.query.get(
                              random.randint(1, Post.query.count())))
        db.session.add(comment)

    salt = int(count * 0.1)
    for i in range(salt):
        #未读评论
        comment = Comment(author=fake.name(),
                          email=fake.email(),
                          site=fake.url(),
                          body=fake.sentence(),
                          timestamp=fake.date_time_this_year(),
                          reviewed=False,
                          post=Post.query.get(
                              random.randint(1, Post.query.count())))
        db.session.add(comment)

        #作者评论
        comment = Comment(author='Lige',
                          email='*****@*****.**',
                          site='ligewudi.com',
                          body=fake.sentence(),
                          timestamp=fake.date_time_this_year(),
                          from_admin=True,
                          reviewed=True,
                          post=Post.query.get(
                              random.randint(1, Post.query.count())))
        db.session.add(comment)
    db.session.commit()

    #replies
    for i in range(salt):
        comment = Comment(
            author=fake.name(),
            email=fake.email(),
            site=fake.url(),
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            reviewed=True,
            replied=Comment.query.get(random.randint(1,
                                                     Comment.query.count())),
            post=Post.query.get(random.randint(1, Post.query.count())))
        db.session.add(comment)
    db.session.commit()
Пример #12
0
def show_post(post_id):
    post = Post.query.get_or_404(post_id)
    page = request.args.get('page', 1, type=int)
    per_page = current_app.config['MYBLOG_COMMENT_PER_PAGE']
    pagination = Comment.query.with_parent(post).filter_by(
        reviewed=True).order_by(Comment.timestamp.asc()).paginate(
            page, per_page)
    comments = pagination.items

    if current_user.is_authenticated:
        form = AdminCommentForm()
        form.author.data = current_user.username
        form.email.data = current_app.config['MYBLOG_EMAIL']
        from_admin = True
        reviewed = True
    else:
        form = CommentForm()
        from_admin = False
        reviewed = False

    if form.validate_on_submit():
        author = form.author.data
        email = form.email.data
        body = form.body.data
        comment = Comment(author=author,
                          email=email,
                          body=body,
                          from_admin=from_admin,
                          post=post,
                          reviewed=reviewed)
        replied_id = request.args.get('reply')
        if replied_id:
            replied_comment = Comment.query.get_or_404(replied_id)
            comment.replied = replied_comment
        db.session.add(comment)
        db.session.commit()
        if current_user.is_authenticated:
            flash('评论发布成功', 'success')
        else:
            flash('您的评论将在管理员审核后发布', 'info')
        return redirect(url_for('main.show_post', post_id=post_id))
    if request.args.get('from_index') == 'True':
        post.read_count += 1
        db.session.add(post)
        db.session.commit()
    return render_template('posts/post.html',
                           post=post,
                           pagination=pagination,
                           form=form,
                           comments=comments)
Пример #13
0
def fake_comments(count=500):
    for i in range(count):                    # 添加已审核的评论
        comment=Comment(author=fake.name(),
                        email=fake.email(),
                        site=fake.url(),
                        body=fake.sentence(),
                        timestamp=fake.date_time_this_year(),
                        reviewed=True,
                        post=Post.query.get(random.randint(1,Post.query.count()))
                        )
        db.session.add(comment)

    salt=int(count*0.1)
    for i in range(salt):                 # 添加未审核通过的评论
        comment = Comment(author=fake.name(),
                          email=fake.email(),
                          site=fake.url(),
                          body=fake.sentence(),
                          timestamp=fake.date_time_this_year(),
                          reviewed=False,
                          post=Post.query.get(random.randint(1, Post.query.count()))
                          )
        db.session.add(comment)

        comment = Comment(author='Mima Kirigoe',    # 添加管理员发表的评论
                          email='*****@*****.**',
                          site='163.com',
                          body=fake.sentence(),
                          timestamp=fake.date_time_this_year(),
                          from_admin=True,
                          reviewed=True,
                          post=Post.query.get(random.randint(1, Post.query.count()))
                          )
        db.session.add(comment)
    db.session.commit()

    for i in range(salt):                   # 添加回复
        comment=Comment(author=fake.name(),
                        email=fake.email(),
                        site=fake.url(),
                        body=fake.sentence(),
                        timestamp=fake.date_time_this_year(),
                        reviewed=True,
                        replied=Comment.query.get(random.randint(1,Comment.query.count())),
                        post=Post.query.get(random.randint(1,Post.query.count()))
                        )
        db.session.add(comment)
    db.session.commit()
Пример #14
0
def show_post(post_id):
    post = Post.query.get_or_404(post_id)
    page = request.args.get('page', 1, type=int)
    per_page = current_app.config['POST_PER_PAGE']
    pagination = Comment.query.with_parent(post).order_by(Comment.timestamp.asc()).paginate(
        page, per_page=per_page)
    comments = pagination.items

    if current_user.is_authenticated:
        form = AdminCommentForm()
        form.author.data = current_user.name
        from_admin = True
    else:
        form = CommentForm()
        from_admin = False

    if form.validate_on_submit():
        author = form.author.data
        body = form.body.data
        comment = Comment(
            author=author, body=body,
            from_admin=from_admin, post=post
        )
        db.session.add(comment)
        db.session.commit()
        if current_user.is_authenticated:
            flash('Comment publish.', 'success')
        else:
            flash('Thanks, your comment will be published after reviewed.')
        return redirect(url_for('.show_post', post_id=post_id))
    return render_template('blog/post.html', post=post, pagination=pagination, comments=comments, form=form)
Пример #15
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)
Пример #16
0
def comment(post_id):
    if request.method == 'POST':
        content = request.form.get('content').strip()
        comment = Comment(body=content,
                          post_id=post_id,
                          user_id=current_user.id)
        db.session.add(comment)
        db.session.commit()
    return redirect(url_for('posts.detail', post_id=post_id))
Пример #17
0
def save_comment(request):
    comment_author=request.POST.get("name",None)
    comment_email=request.POST.get("email",None)
    comment_website=request.POST.get("homepage",None)
    comment_body=request.POST.get("content",None)
    comment_post_id=request.POST.get("postid",None)
    if not comment_author or not len(comment_author):
        if comment_post_id:
            return HttpResponseRedirect("/blog/post/"+comment_post_id+"/")
        else:
            return HttpResponseRedirect("/blog/")
    try:
        onepost=Entry.objects.get(id=comment_post_id)
        comment=Comment(author=comment_author, email=comment_email, website=comment_website, body=comment_body, date=datetime.datetime.now(),post=onepost)
        comment.save()
        return HttpResponseRedirect("/blog/post/"+comment_post_id+"#cmt_form")
        #return HttpResponseRedirect("/blog/")
    except Exception,e:
        return HttpResponse("Write Error")
Пример #18
0
def show_post(post_id):
    post = Post.query.get_or_404(post_id)
    page = request.args.get('page', 1, type=int)
    per_page = current_app.config['MYBLOG_MANAGE_POST_PER_PAGE']
    # with_parent(post)表示当前文章下的所有评论,filter_by(reviewed=True)表示通过审核的评论
    pagination = Comment.query.with_parent(post).filter_by(reviewed=True).order_by(Comment.timestamp.desc()).paginate(
        page, per_page)
    comments = pagination.items

    if current_user.is_authenticated:  # 如果当前用户已登陆,使用管理员表单
        form = AdminCommentForm()
        form.author.data = current_user.name
        form.email.data = current_app.config['MYBLOG_EMAIL']
        form.site.data = url_for('.index')
        from_admin = True
        reviewed = True
    else:  # 否则使用普通表单
        form = CommentForm()
        from_admin = False
        reviewed = False

    if form.validate_on_submit():
        author = form.author.data
        email = form.email.data
        site = form.site.data
        body = form.body.data
        comment = Comment(
            author=author, email=email, site=site, body=body,
            from_admin=from_admin, post=post, reviewed=reviewed)
        replied_id = request.args.get('reply')
        if replied_id:  # 若果存在参数reply,表示是回复
            replied_comment = Comment.query.get_or_404(replied_id)
            comment.replied = replied_comment
            send_new_reply_email(replied_comment)
        db.session.add(comment)
        db.session.commit()
        if current_user.is_authenticated:  # send message based on authentication status
            flash('回复成功', 'success')
        else:
            flash('谢谢,您的评论将在审核后发表', 'info')
            send_new_comment_email(post)  # send notification email to admin
        return redirect(url_for('.show_post', post_id=post_id))
    return render_template('blog/post.html', post=post, pagination=pagination, form=form, comments=comments)
Пример #19
0
    def setUp(self):
        super(AdminTestCase, self).setUp()
        self.login()

        category = Category(name='Default')
        post = Post(title='Hello', category=category, body='Blah...')
        comment = Comment(body='A comment', post=post, from_admin=True)
        link = Link(name='GitHub', url='https://github.com/greyli')
        db.session.add_all([category, post, comment, link])
        db.session.commit()
Пример #20
0
    def setUp(self) -> None:
        super().setUp()
        self.login()

        category = Category(name="Default")
        post = Post(title="Hello", category=category, body="Blah...")
        comment = Comment(body="A comment", post=post, from_admin=True)
        link = Link(name="GitHub", url="https://github.com/greyli")
        db.session.add_all([category, post, comment, link])
        db.session.commit()
Пример #21
0
def show_post(post_id):
    post = Post.query.get_or_404(post_id)
    
    page = request.args.get('page', 1, type=int)
    per_page = current_app.config['MYBLOG_COMMENT_PER_PAGE']
    pagination = Comment.query.with_parent(post).filter_by(reviewed=True).order_by(Comment.timestamp.desc()).paginate(
        page, per_page)
    comments = pagination.items

    if current_user.is_authenticated:
        form = AdminCommentForm()
        form.author.data = current_user.name
        form.email.data = current_app.config['MAIL_DEFAULT_SENDER']
        from_admin = True
        reviewed = True
    else:
        form = CommentForm()
        from_admin = False
        reviewed = False

    if form.validate_on_submit():
        author = form.author.data
        email = form.email.data
        # site = form.site.data
        body = form.body.data
        comment = Comment(author=author,body=body,post=post,email=email, reviewed=reviewed)
        replied_id = request.args.get('reply')
        if replied_id:
            replied_comment = Comment.query.get_or_404(replied_id)
            comment.replied = replied_comment
            send_new_reply_email(replied_comment)
        db.session.add(comment)
        db.session.commit()
        if current_user.is_authenticated:  # send message based on authentication status
            flash('Comment published.', 'success')
        else:
            flash('Thanks, your comment will be published after reviewed.', 'info')
            send_new_comment_email(post)  # send notification email to admin
        return redirect(url_for('.show_post', post_id=post_id) + '#comment-form')
    return render_template('blog/post.html', post=post, pagination=pagination, form=form, comments=comments)
Пример #22
0
def create_comment(post_id):
    """Add a comment under a post. Also sends an email notification to original poster."""
    form = CreateCommentForm()
    post = get_post(post_id)
    post.body = format_markdown(post.body)
    if form.validate_on_submit():
        comment = Comment(body=form.body.data,
                          author=current_user,
                          original_post=post)
        current_app.logger.info("New comment created")
        db.session.add(comment)
        db.session.commit()
        current_app.logger.info(f"Comment {comment.id} pushed to database")
        current_app.logger.info(
            f"Comment {comment.id} has been created by user {current_user.id}")
        flash("Comment created.")
        # Send mail notification to OP
        op = get_user(post.user_id)
        try:
            current_app.logger.info("Sending notifications")
            if comment.user_id != op.id:
                current_app.logger.info("Sending notification to OP")
                OpCommentNotificationEmailSender.build_message(
                    url_for("post.show_post", post_id=post.id, _external=True),
                    comment.id,
                    op,
                    current_user,
                ).send_mail()
            # Send emails to tagged users
            current_app.logger.info("Sending notifications to tagged users")
            for user in get_mentioned_users(comment.body):
                TagNotificationEmailSender.build_message(
                    url_for("post.show_post", post_id=post.id, _external=True),
                    comment.id,
                    user,
                    current_user,
                ).send_mail()
        except SMTPAuthenticationError as e:
            current_app.logger.error(
                f"Not able to send notifications.\nError: {e}")
        return redirect(url_for("post.show_post", post_id=post_id))
    comments = get_all_comments(post_id)
    return render_template(
        "blog/create_comment.html",
        form=form,
        post=post,
        comments=comments,
        comment=None,
    )
Пример #23
0
    def setUp(self):
        super(BlogTestCase, self).setUp()
        self.login()

        category = Category(name='Default')
        topic = Topic(name="test")
        post = Post(title='Hello Post',
                    category=category,
                    topic=topic,
                    body='Blah...')
        comment = Comment(body='A comment',
                          post=post,
                          from_admin=True,
                          reviewed=True)
        link = Link(name='GitHub', url='https://github.com/syntomic')

        db.session.add_all([category, post, comment, link])
        db.session.commit()
Пример #24
0
def create_comment(post_id):
    """Add a comment under a post. Also sends an email notification to original poster."""
    form = CreateCommentForm()
    post = get_post(post_id, check_author=False)
    post.body = format_markdown(post.body)
    if form.validate_on_submit():
        comment = Comment(body=form.body.data,
                          author=current_user,
                          original_post=post)
        db.session.add(comment)
        db.session.commit()
        flash('Comment created.')
        # Send mail notification to OP
        op = get_user(post.user_id)
        if comment.user_id != op.id:
            OpCommentNotificationEmailSender\
                .build_message(
                    url_for('post.show_post', post_id=post.id, _external=True),
                    comment.id,
                    op,
                    current_user)\
                .send_mail()
        # Send emails to tagged users
        for user in get_mentioned_users(comment.body):
            TagNotificationEmailSender\
                .build_message(
                    url_for('post.show_post', post_id=post.id, _external=True),
                    comment.id,
                    user,
                    current_user)\
                .send_mail()
        return redirect(url_for('post.show_post', post_id=post_id))
    comments = get_all_comments(post_id)
    return render_template('blog/create_comment.html',
                           form=form,
                           post=post,
                           comments=comments,
                           comment=None)
Пример #25
0
def add_comment():
    author = request.json.get('author')
    body = request.json.get('body')
    email = request.json.get('email')
    timestamp = datetime.fromtimestamp(request.json.get('timestamp') / 1000)
    post_id = request.json.get('post_id')
    replied_id = request.json.get('replied_id')
    to = request.json.get('to')
    show = request.json.get('show')
    like = request.json.get('like')
    comment = Comment(
        author=author,
        body=body,
        timestamp=timestamp,
        post_id=post_id,
        replied_id=replied_id,
        to=to,
        show=show,
        like=like,
    )
    db.session.add(comment)
    db.session.commit()
    return jsonify({'type': 'success'})
Пример #26
0
def comment_add(request, id):
    article = get_object_or_404(Article, pk=id)
    #add comment
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            #comment_parent=Comment(id=cd["comment_parent_id"]),
            post = Comment(article=Article(id=id),
                           user=User(id=cd["user_id"]),
                           email=cd["email"],
                           author=cd["author"],
                           content=cd["content"])
            if cd["comment_parent_id"] != None:
                post.comment_parent = Comment(id=cd["comment_parent_id"])

            post.save()
            article.comment_count += 1
            article.save()

        print("%r" % form.errors)

    return HttpResponseRedirect("/articles/%d#comment" % int(id))
Пример #27
0
def show_post(post_id):
    """
    文章详情页:文章内容+评论+评论表单
    :param post_id:
    :return:
    """
    # 获取文章详情
    post = Post.query.get_or_404(post_id)
    # 评论信息
    page = request.args.get('page', 1, type=int)
    per_page = current_app.config['BLOG_COMMENT_PER_PAGE']
    pagination = Comment.query.with_parent(post).filter_by(
        reviewed=True).order_by(Comment.timestamp.desc()).paginate(
            page, per_page)
    comments = pagination.items

    # 验证用户是否登录,
    # 如果登录则将相关数据填入表单
    if current_user.is_authenticated:
        form = AdminCommentForm()
        form.author.data = current_user.name
        form.email.data = current_app.config['BLOG_EMAIL']
        form.site.data = url_for('.index')
        from_admin = True
        reviewed = True  # 直接通过审核
    else:
        form = CommentForm()
        from_admin = False
        reviewed = False

    # 表单验证通过后写入数据库
    if form.validate_on_submit():
        author = form.author.data
        email = form.email.data
        site = form.site.data
        body = form.body.data
        comment = Comment(author=author,
                          email=email,
                          site=site,
                          body=body,
                          from_admin=from_admin,
                          post=post,
                          reviewed=reviewed)

        # 对于评论的回复
        # 试图获取是否为回复:如果是回复,则添加对应的字段
        replied_id = request.args.get('reply')
        if replied_id:
            replied_comment = Comment.query.get_or_404(replied_id)
            comment.replied = replied_comment  # 为回复建立与评论的关系
            # todo 邮件发送功能
            # send_new_reply_email(replied_comment)

        db.session.add(comment)
        db.session.commit()

        # 如果管理员登录则显示发布成功,如果不是则发送邮件通知管理员
        if current_user.is_authenticated:
            flash('Comment published.', 'success')
        else:
            flash('Thanks, your comment will be published after reviewed.',
                  'info')
            # todo 邮件发送功能
            # send_new_comment_email(post)

        return redirect(url_for('.show_post', post_id=post_id))

    return render_template('blog/post.html',
                           post=post,
                           pagination=pagination,
                           form=form,
                           comments=comments)