Skip to content

Commit

Permalink
chg: [admin_bp] Implemented delete_user and toggle_user.
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricbonhomme committed Nov 22, 2023
1 parent fb687fc commit b9dd2cc
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions freshermeat/web/views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,34 @@ def process_user_form(user_id=None):
@admin_permission.require(http_exception=403)
def delete_user(user_id=None):
"""Let an administrator delete a user."""
pass
user = User.query.filter(User.id == user_id).first()
if user.id == current_user.id:
flash("You can not delete your own user.", "danger")
else:
db.session.delete(user)
db.session.commit()
flash("User deleted.", "success")
return redirect(url_for("admin_bp.list_users"))


@admin_bp.route("/user/toggle/<int:user_id>", methods=["GET"])
@login_required
@admin_permission.require(http_exception=403)
def toggle_user(user_id=None):
"""Let an administrator enable or disable a user."""
pass
user = User.query.filter(User.id == user_id).first()
if user.id == current_user.id:
flash("You can not do this change to your own user.", "danger")
else:
user.is_active = not user.is_active
db.session.commit()
flash(
"User {status}.".format(
status="activated" if user.is_active else "deactivated"
),
"success",
)
return redirect(url_for("admin_bp.list_users"))


# Flask-Admin views
Expand Down

0 comments on commit b9dd2cc

Please sign in to comment.