Esempio n. 1
0
def manage_category():
    """后台分类管理视图"""
    form = NewCategoryForm(request.form)

    if form.validate_on_submit():
        with db.auto_commit():
            category = Category()
            category.set_attr(form.data)
            db.session.add(category)
        return redirect_back()

    fields_names, fields_errors = get_form_error_items(form)
    return render_template('admin/manage_category.html', form=form,
                           fields_errors=fields_errors,
                           fields_names=fields_names)
Esempio n. 2
0
def manage_link():
    """后台链接管理管理视图"""
    form = NewLinkForm(request.form)

    if form.validate_on_submit():
        with db.auto_commit():
            link = Link()
            link.set_attr(form.data)
            db.session.add(link)
        return redirect_back()

    fields_names, fields_errors = get_form_error_items(form)
    return render_template('admin/manage_link.html', form=form,
                           fields_errors=fields_errors,
                           fields_names=fields_names)
Esempio n. 3
0
def post(post_id):
    """
    文章详情视图,该视图处理发表评论功能
    :param post_id: 文章 id
    """
    post = Post.query.filter_by(id=post_id, trash=False,
                                published=True).first_or_404()
    reply_id = request.args.get('reply_id')
    admin = Admin.query.first()
    per_page = admin.comment_per_page
    admin_email = admin.email
    # 该文章下不在回收站中、已审核且不是回复其它评论的评论 Pagination 对象
    comment_pagination = Comment.query.with_parent(post).filter_by(
        trash=False, reviewed=True, replied_id=None).order_by(
            Comment.create_time.desc()).paginate(per_page=per_page)

    form = CommentForm(request.form)
    # 根据用户登录状态设置不同的字段数据
    if current_user.is_authenticated:
        form.author.data = admin.nickname
        from_admin = True
        reviewed = True
        flash_message = '评论已发布。'
    else:
        from_admin = False
        reviewed = False
        flash_message = '您的评论会尽快被审核,感谢您的评论。'

    if form.validate_on_submit():
        # 如果文章不允许评论,则直接返回
        if not post.can_comment:
            flash('评论已关闭!', 'warning')
            return redirect_back()

        with db.auto_commit():
            comment = Comment()
            comment.set_attr(form.data)
            comment.from_admin = from_admin
            comment.reviewed = reviewed
            comment.post = post

            if reply_id:
                replied_comment = Comment.query.get_or_404(reply_id)
                if replied_comment.trash or not replied_comment.reviewed or replied_comment.post.id != post.id:
                    abort(404)
                comment.replied = replied_comment

            db.session.add(comment)

        flash(flash_message, 'primary')
        # 如果不是已登录用户,则发送邮件通知管理员审核
        if not current_user.is_authenticated:
            send_mail([admin_email],
                      '博客有新的评论需要审核',
                      'email/new_comment.html',
                      post=post)

        return redirect(
            url_for('web.post', post_id=post.id, _anchor="commentResponse"))

    fields_name, fields_errors = get_form_error_items(form)

    # 如果是回复评论且表单验证失败,则跳转至专门显示表单错误的页面
    if reply_id and form.errors:
        if request.referrer and is_safe_url(request.referrer):
            back_url = request.referrer
        else:
            back_url = url_for('web.post', post_id=post.id)
        return redirect(
            url_for('web.reply_error',
                    fields_errors=','.join(fields_errors),
                    back_url=back_url))

    # 如果是主评论表单填写错误,flash 一条信息
    if form.errors:
        flash('评论表单填写有误。', 'danger')

    return render_template('blog/post.html',
                           post=post,
                           comment_pagination=comment_pagination,
                           form=form,
                           fields_errors=fields_errors,
                           fields_name=fields_name)