Skip to content

Commit

Permalink
Improved un/banning
Browse files Browse the repository at this point in the history
  • Loading branch information
sh4nks committed May 22, 2015
1 parent 5ac1522 commit 5b24e62
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
51 changes: 48 additions & 3 deletions flaskbb/management/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,24 @@ def ban_user(user_id=None):
# synchronize_session=False
# )

data = []
for user in User.query.filter(User.id.in_(ids)).all():
user.ban()
if user.ban():
data.append({
"id": user.id,
"type": "ban",
"reverse": "unban",
"reverse_name": _("Unban"),
"reverse_url": url_for("management.unban_user",
user_id=user.id)
})

print data

return jsonify(
message="{} Users banned.".format(users),
message="{} Users banned.".format(len(data)),
category="success",
data=data,
status=200
)

Expand All @@ -238,14 +250,47 @@ def ban_user(user_id=None):
return redirect(url_for("management.banned_users"))


@management.route("/users/unban", methods=["POST"])
@management.route("/users/<int:user_id>/unban", methods=["POST"])
@moderator_required
def unban_user(user_id):
def unban_user(user_id=None):
if not can_ban_user(current_user):
flash(_("You do not have the permissions to unban this user."),
"danger")
return redirect(url_for("management.overview"))

if request.is_xhr:
ids = request.get_json()["ids"]

#banned_group = Group.query.filter_by(banned=True).first()
#users = User.query.\
# filter(User.id.in_(ids)).\
# update(
# {"primary_group_id": banned_group.id},
# synchronize_session=False
# )

data = []
for user in User.query.filter(User.id.in_(ids)).all():
if user.unban():
data.append({
"id": user.id,
"type": "unban",
"reverse": "ban",
"reverse_name": _("Ban"),
"reverse_url": url_for("management.ban_user",
user_id=user.id)
})

print data

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

user = User.query.filter_by(id=user_id).first_or_404()

if user.unban():
Expand Down
14 changes: 13 additions & 1 deletion flaskbb/static/js/flaskbb.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
// get the csrf token from the header
var csrftoken = $('meta[name=csrf-token]').attr('content');

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

var flash_message = function(message) {
var container = $('#flashed-messages');
Expand Down Expand Up @@ -39,7 +43,7 @@ var BulkActions = function() {
data.ids.push($(v).val());
});

send_data(url, data);
send_data(url, data)

return false;
};
Expand All @@ -66,6 +70,14 @@ var send_data = function(endpoint_url, data) {
})
.done(function(response) {
flash_message(response);
console.log(response.data);
$.each(response.data, function(k, v) {
// get the form
console.log(v.reverse_url);
var form = $('#' + v.type + '-' + v.id);
form.attr('action', v.reverse_url);
form.find('button').html(v.reverse_name);
});
})
.fail(function(error) {
flash_message(error);
Expand Down
4 changes: 2 additions & 2 deletions flaskbb/templates/management/users.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@
{% endif %}

{% if current_user|can_ban_user and not user.permissions['banned'] %}
<form class="inline-form" method="post" action="{{ url_for('management.ban_user', user_id = user.id) }}">
<form class="inline-form" id="ban-{{user.id}}" method="post" action="{{ url_for('management.ban_user', user_id = user.id) }}">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
<button class="btn btn-link">{% trans %}Ban{% endtrans %}</button> |
</form>
{% endif %}

{% if current_user|can_ban_user and user.permissions['banned'] %}
<form class="inline-form" method="post" action="{{ url_for('management.unban_user', user_id = user.id) }}">
<form class="inline-form" id="unban-{{user.id}}" method="post" action="{{ url_for('management.unban_user', user_id = user.id) }}">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
<button class="btn btn-link">{% trans %}Unban{% endtrans %}</button> |
</form>
Expand Down

0 comments on commit 5b24e62

Please sign in to comment.