Ejemplo n.º 1
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

    form = CommentForm()
    from_admin = False
    reviewed = False #注意,如果为False则不会在页面中显示,因为32行规定只有为True才会显示,所以提交评论后应该在评论批准里批准为True

    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, reviewed=reviewed, post=post)

        replied_comment_id = request.args.get('reply_to')
        if replied_comment_id:
            replied_comment = Comment.query.get_or_404(replied_comment_id)
            comment.replied = replied_comment

        db.session.add(comment)
        db.session.commit()
        send_comment_mail(post) #发邮件提醒评论
        flash('Comment success', 'info')

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

    return render_template('blog/post.html', post=post, pagination=pagination,
        comments=comments, form=form)
Ejemplo n.º 2
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
    admin =  Admin.query.first()
    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
    elif admin.post_before_view:
        form = CommentForm()
        from_admin = False
        reviewed = False
    else:
        form = CommentForm()
        from_admin = False
        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 if 'site' in form else ''
        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 or not admin.post_before_view:  # 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))
    return render_template('blog/post.html', post=post, pagination=pagination, form=form, comments=comments)
Ejemplo n.º 3
0
def edit_comment(comment_id):
    form = CommentForm()
    comment = Comment.query.get_or_404(comment_id)
    if form.validate_on_submit():
        comment.author = form.author.data
        comment.email = form.email.data
        comment.site = form.site.data
        comment.body = form.body.data
        db.session.commit()
        flash('Edit successed.', 'success')
        return redirect(url_for('.manage_comment'))
    form.author.data = comment.author
    form.email.data = comment.email
    form.site.data = comment.site
    form.body.data = comment.body
    return render_template('admin/edit_comment.html', form=form)
Ejemplo n.º 4
0
def show_post(slug):
    post = Post.query.filter_by(slug=slug).first_or_404()
    # 显示评论
    page = request.args.get('page', 1, type=int)
    per_page = current_app.config['BLUELOG_POST_PER_PAGE']
    pagination = Comment.query.with_parent(post).filter_by(
        reviewed=True).order_by(Comment.timestamp.desc()).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['BLUELOG_EMAIL']
        form.site.data = url_for('blog.index')
        from_admin = True
        reviewed = True
    else:
        form = CommentForm()
        from_admin = False
        # TODO: 已经检查
        reviewed = True

    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
        # 存入数据库
        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')
            # 发送提示邮件给管理员
        return redirect(
            url_for('blog.show_post', slug=post.slug) + '#comments')

    return render_template('blog/post.html',
                           post=post,
                           pagination=pagination,
                           comments=comments,
                           form=form)
Ejemplo n.º 5
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=per_page)
    comments = pagination.items

    # 发表评论
    # 如果当前用户通过验证
    if current_user.is_authenticated:
        # 实例化AdminCommentForm类,并对三个隐藏字段赋予相应的值
        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')
        # 如果请求URL的查询字符串中是否包含replied_id的值,如果包含,则表示提交的评论时一条回复
        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,
                           form=form,
                           comments=comments)
Ejemplo n.º 6
0
def show_post(post_id):
    '''显示博客条目详情'''
    post = Post.query.get_or_404(post_id)
    page = request.args.get('page', 1, int)
    per_page = current_app.config['BLUELOG_COMMENT_PER_PAGE']

    # 知道post,得到post对应所有的comment,这里有好几种方式,可以试一下
    pagination = Comment.query.with_parent(post).filter_by(reviewed=True).order_by(Comment.timestamp.desc()).paginate(
        page, per_page)

    # 这种写法无法排序
    # pagination = post.comments.filter_by(reviewed=True).order_by(Comment.timestamp.asc()).paginate(page, per_page)
    comments = pagination.items

    if current_user.is_authenticated:
        # 如果当前用户是管理员admin
        # 隐藏字段赋值
        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)
        # 根据reply_comment 这个endpoint的参数
        # replied_id就是对Comment进行reply的Comment的id
        # 这个字段是Comment表中的外键,关联的是主键id
        replied_id = request.args.get('reply')
        if replied_id:
            # 如果这个不为空,则是对comment评论的reply
            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('.show_post', post_id=post_id))
    # else:
    #     flash('form validate not passed!')
    #     return render_template('blog/post.html', post=post, pagination=pagination, comments=comments, form=form)
    return render_template('blog/post.html', post=post, pagination=pagination, comments=comments, form=form)
Ejemplo n.º 7
0
def show_post(post_id):
    post = Post.query.get_or_404(post_id)
    comment_page = request.args.get('page', 1, type=int)
    comment_per_page = current_app.config['BLUELOG_COMMENT_PER_PAGE']
    pagination = Comment.query.with_parent(post).filter_by(
        reviewed=True).order_by(Comment.timestamp.desc()).paginate(
            page=comment_page, per_page=comment_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', type=int)
        if replied_id:
            replied_comment = Comment.query.filter_by(
                id=replied_id).first_or_404()
            comment.replied = replied_comment
            send_new_reply_email(replied_comment)  # 发送邮件给被恢复的用户
        db.session.add(comment)
        db.session.commit()

        if current_user.is_authenticated:  # 根据登录状态显示不同的提示信息
            flash('评论已发布。', 'success')
        else:
            flash('感谢你的评论,审核通过后将会显示。', '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)
Ejemplo n.º 8
0
def show_post(post_id):
    post = Post.query.get_or_404(post_id)
    # 获取?page=xxx,默认为1
    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

    #如果管理员账户登录,在每一篇文章的评论部分会隐藏掉name、email、site字段的输入,使用当前账户信息自动赋值
    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:  # 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))
    return render_template('blog/post.html',
                           post=post,
                           pagination=pagination,
                           form=form,
                           comments=comments)
Ejemplo n.º 9
0
def show_post(post_id):
    """显示文章"""
    post = Post.query.get_or_404(post_id)
    page = request.args.get('page', 1, type=int)
    perpage = current_app.config['BLUELOG_COMMENT_PER_PAGE']
    pagination = Comment.query.with_parent(post).filter_by(
        reviewed=True).order_by(Comment.timestamp.asc()).paginate(
            page, perpage)
    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:  # 如果 URL 中 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:  # 根据登录状态现实不同的提示信息
            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)
Ejemplo n.º 10
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.desc()).paginate(
            page, per_page)
    comments = pagination.items
    # 通过是否登陆,登陆给出管理员评论表单,没登陆给出普通匿名用户的登陆表单
    if current_user.is_authenticated:
        # logger.debug("是管理员登陆的!")
        form = AdminComment()
    else:
        # logger.debug("不是管理员登陆的!")
        form = CommentForm()

    if form.validate_on_submit():
        # 是否为回复评论
        reply_id = request.args.get('reply')
        if current_user.is_authenticated:
            # 是管理员的评论
            admin = Admin.query.first()
            comment = Comment(
                author=admin.username,
                # email=url_for('blog.index'),
                site=url_for('blog.index'),
                body=form.body.data,
                post_id=post_id,
                from_admin=True,
                reviewed=True,
                replied_id=reply_id if reply_id else None)
            flash('评论发布成功', 'success')
        else:
            # 不是管理员的评论
            comment = Comment(author=form.author.data,
                              email=form.email.data,
                              site=form.site.data,
                              body=form.body.data,
                              post_id=post_id,
                              from_admin=False,
                              reviewed=False,
                              replied_id=reply_id if reply_id else None)
        db.session.add(comment)
        db.session.commit()
        flash('你的评论需要审核', 'info')
        return redirect(url_for('.show_post', post_id=post_id))
    return render_template('blog/post.html',
                           post=post,
                           pagination=pagination,
                           form=form,
                           comments=comments)
Ejemplo n.º 11
0
def show_post(post_id, page):
    post = Post.query.get_or_404(post_id)
    per_page = current_app.config['BLUELOG_COMMENT_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_username
        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

        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')
        return redirect(url_for('.show_post', post_id=post_id))
    return render_template('blog/post.html',
                           post=post,
                           pagination=pagination,
                           comments=comments,
                           form=form)
Ejemplo n.º 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["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,
                          reviewed=reviewed)
        replied_id = request.args.get('reply')
        if replied_id:
            replied_comment = Comment.query.get_or_404(replied_id)
            comment.replied_id = replied_comment
            send_new_reply_email(replied_comment)
        db.session.add(comment)
        db.session.commit()
        if current_user.is_authenticated:
            flash('评论发布', '成功')
        else:
            flash('评论已加入审核队列,审核通过后将显示在评论列表中', '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)
Ejemplo n.º 13
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['BLOG_COMMENT_PER_PAGE']
    pagination = Comment.query.with_parent(post).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_user.email
        from_admin = True
        read = True
    else:
        form = CommentForm()
        from_admin = False
        read = 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,
                          read=read)
        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()
        flash('评论发表成功', 'success')
        if not current_user.is_authenticated:  # 访客发表评论,通知管理员
            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)