From 62447d6b89774564531032e899c26acde37222b1 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Sun, 31 Jan 2021 10:17:40 +0100 Subject: [PATCH] Basic User edit in tables --- cps/admin.py | 58 +++++++++++++++++++++ cps/static/js/table.js | 97 +++++++++++++++++++++++++++++++++++ cps/templates/admin.html | 4 ++ cps/templates/user_table.html | 46 +++++++++++++++++ 4 files changed, 205 insertions(+) create mode 100644 cps/templates/user_table.html diff --git a/cps/admin.py b/cps/admin.py index 03b306a8..c7210f3f 100644 --- a/cps/admin.py +++ b/cps/admin.py @@ -212,6 +212,64 @@ def view_configuration(): restrictColumns=restrict_columns, title=_(u"UI Configuration"), page="uiconfig") +@admi.route("/admin/usertable") +@login_required +@admin_required +def edit_user_table(): + visibility = current_user.view_settings.get('useredit', {}) + allUser = ub.session.query(ub.User).all() + return render_title_template("user_table.html", users=allUser, visiblility=visibility, + title=_(u"Edit Users"), page="usertable") + +@admi.route("/axjax/listusers") +@login_required +@admin_required +def list_users(): + off = request.args.get("offset") or 0 + limit = request.args.get("limit") or 10 + + total_count = ub.session.query(ub.User).count() + search = request.args.get("search") + if search: + users = ub.session.query(ub.User).filter().offset(off).limit(limit).all() + filtered_count = users.length() + # entries, filtered_count, pagination = calibre_db.get_search_results(search, off, order, limit) + else: + users = ub.session.query(ub.User).offset(off).limit(limit).all() + filtered_count = total_count + + table_entries = {'totalNotFiltered': total_count, 'total': filtered_count, "rows": users} + js_list = json.dumps(table_entries, cls=db.AlchemyEncoder) + + response = make_response(js_list) + response.headers["Content-Type"] = "application/json; charset=utf-8" + return response + + +@admi.route("/axjax/editlistusers/", methods=['POST']) +@login_required +@admin_required +def edit_list_user(param): + vals = request.form.to_dict() + user = ub.session.query(ub.User).filter(ub.User.id == vals['pk']).one_or_none() # ub.User.query calibre_db.get_book(vals['pk']) + if param =='nickname': + if not ub.session.query(ub.User).filter(ub.User.nickname == vals['value']).scalar(): + user.nickname = vals['value'] + else: + log.error(u"This username is already taken") + return _(u"This username is already taken"), 400 + elif param =='email': + existing_email = ub.session.query(ub.User).filter(ub.User.email == vals['value'].lower()).first() + if not existing_email: + user.email = vals['value'] + else: + log.error(u"Found an existing account for this e-mail address.") + return _(u"Found an existing account for this e-mail address."), 400 + elif param =='kindle_mail': + user.kindle_mail = vals['value'] + ub.session_commit() + return "" + @admi.route("/admin/viewconfig", methods=["POST"]) @login_required diff --git a/cps/static/js/table.js b/cps/static/js/table.js index efe0fad4..116a72d6 100644 --- a/cps/static/js/table.js +++ b/cps/static/js/table.js @@ -344,6 +344,103 @@ $(function() { $("#h3").removeClass("hidden"); }); + // User table handling + var user_column = []; + $("#user-table > thead > tr > th").each(function() { + var element = {}; + if ($(this).attr("data-edit")) { + element = { + editable: { + mode: "inline", + emptytext: "", + error: function(response) { + return response.responseText; + } + } + }; + } + var validateText = $(this).attr("data-edit-validate"); + if (validateText) { + element.editable.validate = function (value) { + if ($.trim(value) === "") return validateText; + }; + } + user_column.push(element); + }); + + $("#user-table").bootstrapTable({ + sidePagination: "server", + pagination: true, + paginationLoop: false, + paginationDetailHAlign: " hidden", + paginationHAlign: "left", + idField: "id", + uniqueId: "id", + search: true, + showColumns: true, + searchAlign: "left", + showSearchButton : false, + searchOnEnterKey: true, + checkboxHeader: false, + maintainMetaData: true, + responseHandler: responseHandler, + columns: user_column, + formatNoMatches: function () { + return ""; + }, + // eslint-disable-next-line no-unused-vars + /*onEditableSave: function (field, row, oldvalue, $el) { + if (field === "title" || field === "authors") { + $.ajax({ + method:"get", + dataType: "json", + url: window.location.pathname + "/../../ajax/sort_value/" + field + "/" + row.id, + success: function success(data) { + var key = Object.keys(data)[0]; + $("#books-table").bootstrapTable("updateCellByUniqueId", { + id: row.id, + field: key, + value: data[key] + }); + // console.log(data); + } + }); + } + },*/ + // eslint-disable-next-line no-unused-vars + onColumnSwitch: function (field, checked) { + var visible = $("#user-table").bootstrapTable("getVisibleColumns"); + var hidden = $("#user-table").bootstrapTable("getHiddenColumns"); + var st = ""; + visible.forEach(function(item) { + st += "\"" + item.field + "\":\"" + "true" + "\","; + }); + hidden.forEach(function(item) { + st += "\"" + item.field + "\":\"" + "false" + "\","; + }); + st = st.slice(0, -1); + /*$.ajax({ + method:"post", + contentType: "application/json; charset=utf-8", + dataType: "json", + url: window.location.pathname + "/../../ajax/table_settings", + data: "{" + st + "}", + });*/ + }, + }); + + $("#user-table").on("check.bs.table check-all.bs.table uncheck.bs.table uncheck-all.bs.table", + function (e, rowsAfter, rowsBefore) { + var rows = rowsAfter; + + if (e.type === "uncheck-all") { + rows = rowsBefore; + } + + var ids = $.map(!$.isArray(rows) ? [rows] : rows, function (row) { + return row.id; + }); + }); }); /* Function for deleting domain restrictions */ diff --git a/cps/templates/admin.html b/cps/templates/admin.html index 1ef64157..777a6f8a 100644 --- a/cps/templates/admin.html +++ b/cps/templates/admin.html @@ -7,6 +7,7 @@

{{_('Users')}}

+ {% if allUser.__len__() < 10 %} @@ -41,6 +42,9 @@ {% endif %} {% endfor %}
{{_('Username')}}
+ {% else %} + + {% endif %} {% if (config.config_login_type == 1) %}
{{_('Import LDAP Users')}}
diff --git a/cps/templates/user_table.html b/cps/templates/user_table.html new file mode 100644 index 00000000..40c0f731 --- /dev/null +++ b/cps/templates/user_table.html @@ -0,0 +1,46 @@ +{% extends "layout.html" %} +{% macro user_table_row(parameter, edit_text, show_text, validate) -%} +{{ show_text }} +{%- endmacro %} + +{% block header %} + + +{% endblock %} +{% block body %} +

{{_(title)}}

+ + + + + + {{ user_table_row('nickname', _('Enter Username'),_('Username'), true) }} + {{ user_table_row('email', _('Enter E-mail Address'),_('E-mail Address'), true) }} + {{ user_table_row('kindle_mail', _('Enter Kindle E-mail Address'),_('Kindle E-mail'), true) }} + + + + + + + + + +
+{% endblock %} +{% block js %} + + + + + +{% endblock %}