From d32b2ca524371e377ca19deabbab6f3553ddc8b6 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Mon, 12 Apr 2021 19:04:27 +0200 Subject: [PATCH] Prevent traceback after delete user flash message in case last admin role is removed #1938 --- cps/admin.py | 8 +++++--- cps/static/js/table.js | 35 ++++++++++++++++++++--------------- cps/web.py | 2 +- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/cps/admin.py b/cps/admin.py index 10f9a5b4..5cd31f18 100644 --- a/cps/admin.py +++ b/cps/admin.py @@ -249,7 +249,7 @@ def list_users(): order = request.args.get("order", "").lower() state = None if sort == "state": - state = json.loads(request.args.get("state")) + state = json.loads(request.args.get("state", "[]")) if sort != "state" and order: order = text(sort + " " + order) @@ -356,7 +356,7 @@ def edit_list_user(param): user.email = check_email(vals['value']) elif param == 'kindle_mail': user.kindle_mail = valid_email(vals['value']) if vals['value'] else "" - elif param == 'role': + elif param.endswith('role'): if user.name == "Guest" and int(vals['field_index']) in \ [constants.ROLE_ADMIN, constants.ROLE_PASSWD, constants.ROLE_EDIT_SHELFS]: raise Exception(_("Guest can't have this role")) @@ -367,7 +367,9 @@ def edit_list_user(param): if not ub.session.query(ub.User).\ filter(ub.User.role.op('&')(constants.ROLE_ADMIN) == constants.ROLE_ADMIN, ub.User.id != user.id).count(): - return _(u"No admin user remaining, can't remove admin role", nick=user.name), 400 + return Response(json.dumps({'type': "danger", + 'message':_(u"No admin user remaining, can't remove admin role", + nick=user.name)}), mimetype='application/json') user.role &= ~int(vals['field_index']) elif param == 'sidebar_view': if user.name == "Guest" and int(vals['field_index']) == constants.SIDEBAR_READ_AND_UNREAD: diff --git a/cps/static/js/table.js b/cps/static/js/table.js index 7a07d82d..7c4b04a7 100644 --- a/cps/static/js/table.js +++ b/cps/static/js/table.js @@ -612,25 +612,30 @@ function checkboxFormatter(value, row, index){ function checkboxChange(checkbox, userId, field, field_index) { $.ajax({ - method:"post", + method: "post", url: window.location.pathname + "/../../ajax/editlistusers/" + field, - data: {"pk":userId, "field_index":field_index, "value": checkbox.checked} - /*
- -
*/ - /*
Text to show
*/ - }); - $.ajax({ - method:"get", - url: window.location.pathname + "/../../ajax/listusers", - async: true, - timeout: 900, - success:function(data) { - $("#user-table").bootstrapTable("load", data); + data: {"pk": userId, "field_index": field_index, "value": checkbox.checked}, + success: function (data) { + if (!jQuery.isEmptyObject(data)) { + $("#flash_success").remove(); + $("#flash_danger").remove(); + $( ".navbar" ).after( '
' + + '
'+data.message+'
' + + '
'); + } + $.ajax({ + method: "get", + url: window.location.pathname + "/../../ajax/listusers", + async: true, + timeout: 900, + success: function (data) { + $("#user-table").bootstrapTable("load", data); + } + }); } }); } + function deactivateHeaderButtons(e) { $("#user_delete_selection").addClass("disabled"); $("#user_delete_selection").attr("aria-disabled", true); diff --git a/cps/web.py b/cps/web.py index 528e285a..cf488986 100644 --- a/cps/web.py +++ b/cps/web.py @@ -761,7 +761,7 @@ def list_books(): state = None if sort == "state": - state = json.loads(request.args.get("state")) + state = json.loads(request.args.get("state", "[]")) if sort != "state" and order: order = [text(sort + " " + order)]