Skip to content

Commit

Permalink
Refactoring and started working on moderation tools
Browse files Browse the repository at this point in the history
  • Loading branch information
sh4nks committed Jun 24, 2015
1 parent 7ddb884 commit 69b7175
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 39 deletions.
87 changes: 72 additions & 15 deletions flaskbb/forum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import datetime

from flask import (Blueprint, redirect, url_for, current_app,
request, flash)
request, flash, jsonify)
from flask_login import login_required, current_user
from flask_babelex import gettext as _

Expand Down Expand Up @@ -170,22 +170,22 @@ def new_topic(forum_id, slug=None):
)
if "submit" in request.form and form.validate():
topic = form.save(current_user, forum_instance)

# redirect to the new topic
return redirect(url_for('forum.view_topic', topic_id=topic.id))

return render_template(
"forum/new_topic.html", forum=forum_instance, form=form
)


@forum.route("/topic/delete", methods=["POST"])
@forum.route("/topic/<int:topic_id>/delete", methods=["POST"])
@forum.route("/topic/<int:topic_id>-<slug>/delete", methods=["POST"])
@login_required
def delete_topic(topic_id, slug=None):
def delete_topic(topic_id=None, slug=None):
topic = Topic.query.filter_by(id=topic_id).first_or_404()

if not can_delete_topic(user=current_user, topic=topic):

flash(_("You do not have the permissions to delete this topic."),
"danger")
return redirect(topic.forum.url)
Expand All @@ -196,14 +196,42 @@ def delete_topic(topic_id, slug=None):
return redirect(url_for("forum.view_forum", forum_id=topic.forum_id))


@forum.route("/topic/lock", methods=["POST"])
@forum.route("/topic/<int:topic_id>/lock", methods=["POST"])
@forum.route("/topic/<int:topic_id>-<slug>/lock", methods=["POST"])
@login_required
def lock_topic(topic_id, slug=None):
topic = Topic.query.filter_by(id=topic_id).first_or_404()

# TODO: Bulk lock
def lock_topic(topic_id=None, slug=None):
if request.is_xhr:
ids = request.get_json()["ids"]
data = []

for topic in Topic.query.filter(Topic.id.in_(ids)).all():
# skip already locked topics
if topic.locked:
continue

# check if the user has the right permissions
if not can_moderate(user=current_user, forum=topic.forum):
return jsonify(status=550,
message=_("You do not have the permissions to "
"lock this topic."),
category="danger")

topic.locked = True
topic.save()

data.append({
"id": topic.id,
"type": "lock",
"reverse": "unlock",
"reverse_name": _("Unlock"),
"reverse_url": url_for("forum.unlock_topic")
})

return jsonify(message="{} topics locked.".format(len(data)),
category="success", data=data, status=200)

topic = Topic.query.filter_by(id=topic_id).first_or_404()
if not can_moderate(user=current_user, forum=topic.forum):
flash(_("You do not have the permissions to lock this topic."),
"danger")
Expand All @@ -214,15 +242,42 @@ def lock_topic(topic_id, slug=None):
return redirect(topic.url)


@forum.route("/topic/unlock", methods=["POST"])
@forum.route("/topic/<int:topic_id>/unlock", methods=["POST"])
@forum.route("/topic/<int:topic_id>-<slug>/unlock", methods=["POST"])
@login_required
def unlock_topic(topic_id, slug=None):
topic = Topic.query.filter_by(id=topic_id).first_or_404()

# TODO: Bulk unlock
def unlock_topic(topic_id=None, slug=None):
if request.is_xhr:
ids = request.get_json()["ids"]
data = []

for topic in Topic.query.filter(Topic.id.in_(ids)).all():
# skip already locked topics
if not topic.locked:
continue

# check if the user has the right permissions
if not can_moderate(user=current_user, forum=topic.forum):
return jsonify(status=550,
message=_("You do not have the permissions to "
"unlock this topic."),
category="danger")

topic.locked = False
topic.save()

data.append({
"id": topic.id,
"type": "unlock",
"reverse": "lock",
"reverse_name": _("Lock"),
"reverse_url": url_for("forum.lock_topic")
})

return jsonify(message="{} topics unlocked.".format(len(data)),
category="success", data=data, status=200)

# Unlock is basically the same as lock
topic = Topic.query.filter_by(id=topic_id).first_or_404()
if not can_moderate(user=current_user, forum=topic.forum):
flash(_("You do not have the permissions to unlock this topic."),
"danger")
Expand All @@ -233,10 +288,11 @@ def unlock_topic(topic_id, slug=None):
return redirect(topic.url)


@forum.route("/topic/highlight", methods=["POST"])
@forum.route("/topic/<int:topic_id>/highlight", methods=["POST"])
@forum.route("/topic/<int:topic_id>-<slug>/highlight", methods=["POST"])
@login_required
def highlight_topic(topic_id, slug=None):
def highlight_topic(topic_id=None, slug=None):
topic = Topic.query.filter_by(id=topic_id).first_or_404()

if not can_moderate(user=current_user, forum=topic.forum):
Expand All @@ -249,10 +305,11 @@ def highlight_topic(topic_id, slug=None):
return redirect(topic.url)


@forum.route("/topic/trivialize", methods=["POST"])
@forum.route("/topic/<int:topic_id>/trivialize", methods=["POST"])
@forum.route("/topic/<int:topic_id>-<slug>/trivialize", methods=["POST"])
@login_required
def trivialize_topic(topic_id, slug=None):
def trivialize_topic(topic_id=None, slug=None):
topic = Topic.query.filter_by(id=topic_id).first_or_404()

# Unlock is basically the same as lock
Expand Down
4 changes: 4 additions & 0 deletions flaskbb/static/css/flaskbb.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ body {
margin-bottom: 60px;
}

.forum-moderation, .forum-selectall, .forum-select {
display: none;
}

.footer {
position: absolute;
bottom: 0;
Expand Down
28 changes: 18 additions & 10 deletions flaskbb/static/js/flaskbb.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ var show_management_search = function() {
});
};

var change_link = function(data, link, text) {
$.each(data, function(k, v) {
});
var show_moderation_tools = function() {


};

var flash_message = function(message) {
Expand Down Expand Up @@ -67,17 +67,11 @@ var BulkActions = function() {

return false;
};

$(function() {
$('.action-checkall').change(function() {
$('input.action-checkbox').prop('checked', this.checked);
});
});
};

var send_data = function(endpoint_url, data) {
$.ajax({
url: BASE_URL + endpoint_url,
url: endpoint_url,
method: "POST",
data: JSON.stringify(data),
dataType: "json",
Expand Down Expand Up @@ -116,6 +110,20 @@ var send_data = function(endpoint_url, data) {

$(document).ready(function () {
// TODO: Refactor

$('#toggle-moderation-tools').click(function (event) {
event.preventDefault();

$('.forum-moderation').toggle();
$('.forum-selectall').toggle();
$('.forum-select').toggle();
});

// listen on the action-checkall checkbox to un/check all
$('.action-checkall').change(function() {
$('input.action-checkbox').prop('checked', this.checked);
});

// Reply conversation
$('.reply-btn').click(function (event) {
event.preventDefault();
Expand Down
52 changes: 50 additions & 2 deletions flaskbb/templates/forum/forum.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<table class="table table-bordered">
<thead>
<tr>
<th colspan="5">
<th colspan="{% if current_user|can_moderate(forum) %}6{% else %}5{% endif %}">
{{ forum.title }}
</th>
</tr>
Expand All @@ -54,11 +54,15 @@
<td>{% trans %}Views{% endtrans %}</td>

<td>{% trans %}Last Post{% endtrans %}</td>

{% if current_user|can_moderate(forum) %}
<td class="forum-selectall"><input type="checkbox" name="rowtoggle" class="action-checkall" title="Select All"/></td>
{% endif %}
</tr>

{% for topic, topicread in topics.items %}
<tr>
<td width="4%" style="vertical-align: middle; text-align: center;">
<td class="topic-status" width="4%" style="vertical-align: middle; text-align: center;">
{% if topic.locked %}
<span class="fa fa-lock" style="font-size: 2em"></span>
{% elif topic.important %}
Expand Down Expand Up @@ -110,6 +114,9 @@
{% endif %}
</small>
</td>
{% if current_user|can_moderate(forum) %}
<td class="forum-select"><input type="checkbox" name="rowid" class="action-checkbox" value="{{ topic.id }}" title="Select Topic"/></td>
{% endif %}
</tr>
{% else %}
<tr>
Expand All @@ -122,4 +129,45 @@
</tbody>
</table>

{% if current_user|can_moderate(forum) %}
<button id="toggle-moderation-tools" class="btn btn-default">Toggle Moderation Tools</button>

<div class="forum-moderation pull-right">
<div class="btn-group" role="group" aria-label="...">
<a class="btn btn-warning" href="javascript:void(0)" onclick="return bulk_actions.execute('{{ url_for('forum.lock_topic') }}', '{% trans %}Are you sure you want to lock these Topics?{% endtrans %}')">
<span class="fa fa-lock"></span> {% trans %}Lock{% endtrans %}
</a>
<a class="btn btn-warning" href="javascript:void(0)" onclick="return bulk_actions.execute('{{ url_for('forum.unlock_topic') }}', '{% trans %}Are you sure you want to lock these Topics?{% endtrans %}')">
<span class="fa fa-unlock"></span> {% trans %}Unlock{% endtrans %}
</a>
</div>
<div class="btn-group" role="group" aria-label="...">
<a class="btn btn-success" href="javascript:void(0)" onclick="return bulk_actions.execute('{{ url_for('forum.highlight_topic') }}', '{% trans %}Are you sure you want to highlight these Topics?{% endtrans %}')">
<span class="fa fa-star"></span> {% trans %}Highlight{% endtrans %}
</a>
<a class="btn btn-success" href="javascript:void(0)" onclick="return bulk_actions.execute('{{ url_for('forum.trivialize_topic') }}', '{% trans %}Are you sure you want to trivialize these Topics?{% endtrans %}')">
<span class="fa fa-star-o"></span> {% trans %}Trivialize{% endtrans %}
</a>
</div>

<a class="btn btn-danger" href="javascript:void(0)" onclick="return bulk_actions.execute('{{ url_for('forum.delete_topic') }}', '{% trans %}Are you sure you want to trivialize these Topics?{% endtrans %}')">
<span class="fa fa-trash-o"></span> {% trans %}Delete{% endtrans %}
</a>

<button class="btn btn-info">
<span class="fa fa-plane"></span> {% trans %}Move{% endtrans %}
</button>
</div>
{% endif %}

{% endblock %}

{% block scripts %}
<script>
var bulk_actions = new BulkActions();

$(function () {
$('[data-toggle="tooltip"]').tooltip()
})
</script>
{% endblock %}
3 changes: 0 additions & 3 deletions flaskbb/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,6 @@

{# standard javascript libs #}
{% block javascript %}
<script>
var BASE_URL = "{{ request.path.split('/')[0] }}";
</script>
<script src="{{ url_for('static', filename='js/jquery.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/flaskbb.js') }}"></script>
Expand Down
2 changes: 1 addition & 1 deletion flaskbb/templates/management/banned_users.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
</button>
<ul class="dropdown-menu slidedown">
<li>
<a href="javascript:void(0)" onclick="return bulk_actions.execute('/users/unban', '{% trans %}Are you sure you want to unban these Users?{% endtrans %}')">
<a href="javascript:void(0)" onclick="return bulk_actions.execute('{{ url_for('management.unban_user') }}', '{% trans %}Are you sure you want to unban these Users?{% endtrans %}')">
<span class="fa fa-flag text-success"></span> {% trans %}Unban selected Users{% endtrans %}
</a>
</li>
Expand Down
2 changes: 1 addition & 1 deletion flaskbb/templates/management/groups.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</button>
<ul class="dropdown-menu slidedown">
<li>
<a href="javascript:void(0)" onclick="return bulk_actions.execute('/groups/delete', '{% trans %}Are you sure you want to delete these Groups?{% endtrans %}')">
<a href="javascript:void(0)" onclick="return bulk_actions.execute('{{ url_for('management.delete_group') }}', '{% trans %}Are you sure you want to delete these Groups?{% endtrans %}')">
<span class="fa fa-trash text-danger"></span> {% trans %}Delete selected Groups{% endtrans %}
</a>
</li>
Expand Down
2 changes: 1 addition & 1 deletion flaskbb/templates/management/unread_reports.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</button>
<ul class="dropdown-menu slidedown">
<li>
<a href="javascript:void(0)" onclick="return bulk_actions.execute('/reports/markread', '{% trans %}Are you sure you want to mark these Reports as read?{% endtrans %}')">
<a href="javascript:void(0)" onclick="return bulk_actions.execute('{{ url_for('management.report_markread') }}', '{% trans %}Are you sure you want to mark these Reports as read?{% endtrans %}')">
<span class="fa fa-flag-o text-primary"></span> {% trans %}Mark as Read{% endtrans %}
</a>
</li>
Expand Down
6 changes: 3 additions & 3 deletions flaskbb/templates/management/users.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@
</button>
<ul class="dropdown-menu slidedown">
<li>
<a href="javascript:void(0)" onclick="return bulk_actions.execute('/users/ban', '{% trans %}Are you sure you want to ban these Users?{% endtrans %}')">
<a href="javascript:void(0)" onclick="return bulk_actions.execute('{{ url_for('management.ban_user') }}', '{% trans %}Are you sure you want to ban these Users?{% endtrans %}')">
<span class="fa fa-flag text-warning"></span> {% trans %}Ban selected Users{% endtrans %}
</a>
</li>

<li>
<a href="javascript:void(0)" onclick="return bulk_actions.execute('/users/unban', '{% trans %}Are you sure you want to unban these Users?{% endtrans %}')">
<a href="javascript:void(0)" onclick="return bulk_actions.execute('{{ url_for('management.unban_user') }}', '{% trans %}Are you sure you want to unban these Users?{% endtrans %}')">
<span class="fa fa-flag text-success"></span> {% trans %}Unban selected Users{% endtrans %}
</a>
</li>

<li>
<a href="javascript:void(0)" onclick="return bulk_actions.execute('/users/delete', '{% trans %}Are you sure you want to delete these Users?{% endtrans %}')">
<a href="javascript:void(0)" onclick="return bulk_actions.execute('{{ url_for('management.delete_user') }}', '{% trans %}Are you sure you want to delete these Users?{% endtrans %}')">
<span class="fa fa-trash text-danger"></span> {% trans %}Delete selected Users{% endtrans %}
</a>
</li>
Expand Down
3 changes: 0 additions & 3 deletions flaskbb/themes/bootstrap3/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,6 @@

{# standard javascript libs #}
{% block javascript %}
<script>
var BASE_URL = "/{{ request.path.split('/')[1] }}";
</script>
<script src="{{ url_for('static', filename='js/jquery.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script>
<script src="{{ url_for('static', filename='js/flaskbb.js') }}"></script>
Expand Down

0 comments on commit 69b7175

Please sign in to comment.