Browse Source

Chapter 13: Comment moderation (13b)

T. Meissner 1 year ago
parent
commit
acd71f289d
4 changed files with 75 additions and 5 deletions
  1. 37
    0
      app/main/views.py
  2. 18
    5
      app/templates/_comments.html
  3. 3
    0
      app/templates/base.html
  4. 17
    0
      app/templates/moderate.html

+ 37
- 0
app/main/views.py View File

@@ -215,3 +215,40 @@ def show_followed():
215 215
     resp = make_response(redirect(url_for('.index')))
216 216
     resp.set_cookie('show_followed', '1', max_age=30*24*60*60)  # lasts 30 days
217 217
     return resp
218
+
219
+
220
+@main.route('/moderate')
221
+@login_required
222
+@permission_required(Permission.MODERATE)
223
+def moderate():
224
+    page = request.args.get('page', 1, type=int)
225
+    pagination = Comment.query.order_by(Comment.timestamp.desc()).paginate(
226
+        page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'],
227
+        error_out=False)
228
+    comments = pagination.items
229
+    return render_template('moderate.html', comments=comments,
230
+                           pagination=pagination, page=page)
231
+
232
+
233
+@main.route('/moderate/enable/<int:id>')
234
+@login_required
235
+@permission_required(Permission.MODERATE)
236
+def moderate_enable(id):
237
+    comment = Comment.query.get_or_404(id)
238
+    comment.disabled = False
239
+    db.session.add(comment)
240
+    db.session.commit()
241
+    return redirect(url_for('.moderate',
242
+                            page=request.args.get('page', 1, type=int)))
243
+
244
+
245
+@main.route('/moderate/disable/<int:id>')
246
+@login_required
247
+@permission_required(Permission.MODERATE)
248
+def moderate_disable(id):
249
+    comment = Comment.query.get_or_404(id)
250
+    comment.disabled = True
251
+    db.session.add(comment)
252
+    db.session.commit()
253
+    return redirect(url_for('.moderate',
254
+                            page=request.args.get('page', 1, type=int)))

+ 18
- 5
app/templates/_comments.html View File

@@ -9,13 +9,26 @@
9 9
         <div class="comment-content">
10 10
             <div class="comment-date">{{ moment(comment.timestamp).fromNow() }}</div>
11 11
             <div class="comment-author"><a href="{{ url_for('.user', username=comment.author.username) }}">{{ comment.author.username }}</a></div>
12
-            <div class="comemnt-body">
13
-            {% if comment.body_html %}
14
-                {{ comment.body_html | safe }}
15
-            {% else %}
16
-                {{ comment.body }}
12
+            <div class="comment-body">
13
+            {% if comment.disabled %}
14
+            <p><i>This comment has been disabled by a moderator.</i></p>
15
+            {% endif %}
16
+            {% if moderate or not comment.disabled %}
17
+                {% if comment.body_html %}
18
+                    {{ comment.body_html | safe }}
19
+                {% else %}
20
+                    {{ comment.body }}
21
+                {% endif %}
17 22
             {% endif %}
18 23
             </div>
24
+            {% if moderate %}
25
+            <br>
26
+                {% if comment.disabled %}
27
+                <a class="btn btn-default btn-xs" href="{{ url_for('.moderate_enable', id=comment.id, page=page) }}">Enable</a>
28
+                {% else %}
29
+                <a class="btn btn-danger btn-xs" href="{{ url_for('.moderate_disable', id=comment.id, page=page) }}">Disable</a>
30
+                {% endif %}
31
+            {% endif %}
19 32
         </div>
20 33
     </li>
21 34
     {% endfor %}

+ 3
- 0
app/templates/base.html View File

@@ -30,6 +30,9 @@
30 30
                 {% endif %}
31 31
             </ul>
32 32
             <ul class="nav navbar-nav navbar-right">
33
+                {% if current_user.is_authenticated and current_user.can(Permission.MODERATE) %}
34
+                <li><a href="{{ url_for('main.moderate') }}">Moderate Comments</a></li>
35
+                {% endif %}
33 36
                 {% if current_user.is_authenticated %}
34 37
                 <li class="dropdown">
35 38
                     <a href="#" class="dropdown-toggle" data-toggle="dropdown">

+ 17
- 0
app/templates/moderate.html View File

@@ -0,0 +1,17 @@
1
+{% extends "base.html" %}
2
+{% import "_macros.html" as macros %}
3
+
4
+{% block title %}Flasky - Comment Moderation{% endblock %}
5
+
6
+{% block page_content %}
7
+<div class="page-header">
8
+    <h1>Comment Moderation</h1>
9
+</div>
10
+{% set moderate = True %}
11
+{% include '_comments.html' %}
12
+{% if pagination %}
13
+<div class="pagination">
14
+    {{ macros.pagination_widget(pagination, '.moderate') }}
15
+</div>
16
+{% endif %}
17
+{% endblock %}