创建博客-评论部分(管理)

来源:互联网 发布:saa7130 tv card软件 编辑:程序博客网 时间:2024/06/16 07:34

之前我们定义了几个角色用户,它们分别具有不同的权限,其中一个权限是Permission.MODERATE_COMMENTS,拥有此权限的用户可以管理其他用户的评论

为了方便管理评论,我们要在导航条中添加一个链接,具有权限的用户才能看到,这个链接在base.html模板中使用条件语句添加,如下:

# app/templates/base.html    #...    {if current_user.can(Permission.MODERATE_COMMENTS) %}    <li><a href="{{ url_for('main.moderate') }}">Moderate Comments</a></li>    {% endif %}

管理页面在同一个列表中显示全部文章的评论,最新发布的评论会显示在前面,每篇评论的下方都会显示一个按钮,用来切换disabled属性的值,/moderate路由的定义如下:

# app/main/views.py#...@main.route('/moderate')@login_required@permission_required(Permission.MODERATE_COMMENTS)def moderate():    page = request.args.get('page', 1, type=int)    pagination = Comment.query.order_by(Comment.timestamp.desc()).paginate(        page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'],        error_out=False)    comments = pagination.items    return render_template('moderate.html', comments=comments,                            pagination=pagination, page=page)

这个函数很简单,它从数据库中读取一页评论,将其传入模板进行渲染,除了评论列表之外,还能把分页对象和当前页数传入了模板

moderate.html模板也不难,因为它依靠之前创建的子模板_comments.html渲染评论

# app/templates/moderate.html{% extends "base.html" %}{% import "_macros.html" as macros %}{% block title %}Flasky - Comment Moderation{% endblock %}{% block page_content %}<div class="page-header"><h1>Comment Moderation</h1></div>{% set moderate = True %}{% include '_comments.html' %}{% if pagination %}<div class="pagination">    {{ macros.pagination_widget(pagination, '.moderate') }}</div>{% endif %}{% endblock %}

这个模板将渲染评论的工作交给_comments.html模板完成,但把控制权交给从属模板之前,会使用Jinja2提供的set指令定义一个模板变量moderate,并将其设值为True,这个变量用在_comments.html,决定是否渲染评论管理功能

_comments.html模板中显示评论正文的部分要做两方面修改,对于普通用户,(没设定moderate变量),不显示标记为有问题的评论,对于协管员(moderate设为True),不管评论是否被标记为有问题,都要显示,而且在正文下方还要显示一个用来切换状态的按钮,具体代码如下:

# app/templates/_comments.html    #...    <div class="comment-body">        {% if comment.disabled %}            <p><i>This comment has been disabled by a moderator</i></p>        {% endif %}        {% if moderate or not comment.disbled %}            {% if comment.body_html %}                {{ comment.body_html | safe }}            {% else %}                {{ comment.body }}            {% endif %}        {% endif %}            </div>        {% if moderate %}        <br>        {% if comment.disabled %}            <a class="btn btn-default btn-xs" href="{{url_for('.moderate_enable', id=comment.id, page=page) }}">Enable</a>        {% else %}            <a class="btn btn-danger btn-xs" href="{{url_for('.moderate_disbled', id=comment.id, page=page) }}">            Disable</a>        {% endif %}        {% endif %}    </div>

做了上述改动之后,用户将看到一个关于有问题评论的简短提示,协管员既能看到这个提示,也能看到评论的正文,在每篇评论的下方,协管员还能看到一个按钮,用来切换评论的状态,点击按钮后会触发两个新路由中的一个,但具体触发哪一个取决于协管员要把评论设为什么状态,两个新路由如下:

# app/main/views.py@main.route('/moderate/enable/<int:id>')@login_required@permission_required(Psermission.MODERATE_COMMENTS)def moderate_enable(id):    comment = Comment.query.get_or_404(id)    comment.disabled = False    db.session.add(comment)    return redirect(url_for('.moderate', poage=request.args.get('page', 1, type=int)))@main.route('/moderate/disable/<int:id>')@login_required@permission_required(Permission.MODERATE_COMMENTS)def moderate_disabled(id):    comment = Comment.query.get_or_404(id)    comment.disabled = True    db.session.add(comment)    return redirect(url_for('.moderate', page=request.args.get('page', 1, type=int)))

上述启用路由和禁用路由先加载评论对象,把disabled字段设为正确的值,再把评论对象写入数据库,最后,重定向到评论管理页面,如果查询字符串中指定了page参数,会将其传入重定向操作,_comments.html模板中的按钮指定了page参数,重定向后会返回之前的页面

0 0