Esempio n. 1
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)
Esempio 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=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)
Esempio n. 3
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)
Esempio n. 4
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)
Esempio n. 5
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)
Esempio n. 6
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)
Esempio n. 7
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=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('.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,
                          post=post)
        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('评论提交成功', '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,
                           form=form,
                           comments=comments,
                           pagination=pagination)
Esempio n. 8
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)
Esempio n. 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,
                          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)
Esempio 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['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)