diff --git a/admin_board_view/static/AdminBoardView/script.js b/admin_board_view/static/AdminBoardView/script.js index ce11b22..9aae789 100644 --- a/admin_board_view/static/AdminBoardView/script.js +++ b/admin_board_view/static/AdminBoardView/script.js @@ -33,13 +33,30 @@ function showConfirmation(title, body, id) { modal.show(); } +// Filter user page +document.getElementById("user").addEventListener("keypress", e => { + if (e.key === "Enter") { + const name = document.getElementById("user").value; + const escapedName = name.replace("'", "\\'"); + const user_options = document.getElementById("userOptions"); + const selected_user = user_options.querySelector(`[value='${escapedName}']`); + if (!selected_user) { + window.location = `/users?name=${name}`; + } else { + const userId = selected_user.id; + window.location = `/users/${userId}`; + } + } +}); + // Show user page const showUser = document.getElementById("show-user"); if (showUser) { document.getElementById("show-user").addEventListener("click", e => { const name = document.getElementById("user").value; + const escapedName = name.replace("'", "\\'"); const user_options = document.getElementById("userOptions"); - const selected_user = user_options.querySelector(`[value='${name}']`); + const selected_user = user_options.querySelector(`[value='${escapedName}']`); if (!selected_user) { showToast("Show user - Failed", "User not found"); } else { diff --git a/admin_board_view/templates/user.html b/admin_board_view/templates/user.html index d9aeeb6..6df9245 100644 --- a/admin_board_view/templates/user.html +++ b/admin_board_view/templates/user.html @@ -138,6 +138,29 @@
Find user
Show user +
+ + + + + + + + + + + {% for user in user_page %} + + + + + + + {% endfor %} + +
UserEmailBirthdateBalance
{{ user.name }}{{ user.email }}{{ user.birthday }}€{{ user.balance }}
+ {% include "pagination_footer.html" with page=user_page page_name='users' %} +
{% endif %} {% endblock %} diff --git a/admin_board_view/utils.py b/admin_board_view/utils.py index 3f8fa83..7474e1b 100644 --- a/admin_board_view/utils.py +++ b/admin_board_view/utils.py @@ -11,8 +11,13 @@ def create_paginator(data, page_n, p_len=5): p_len: Length of the page, defaults to 5 """ if isinstance(data, QuerySet): - # Order data, most recent date first - data = data.order_by('date', 'id').reverse() + if hasattr(data.model, 'date'): + # Order data, most recent date first + data = data.order_by('date', 'id').reverse() + elif hasattr(data.model, 'name'): + data = data.order_by('name', 'id') + else: + data = data.order_by('id') page = None paginator = Paginator(data, p_len) diff --git a/admin_board_view/views.py b/admin_board_view/views.py index 248a200..e9b8bd4 100644 --- a/admin_board_view/views.py +++ b/admin_board_view/views.py @@ -110,7 +110,13 @@ def users(request, user_id=None): return render(request, "user.html", { "user_info": user, "cards": cards, "top_ups": top_up_page, "sales": sales_page, "top_types": top_up_types }) else: users = User.objects.all() - return render(request, "user.html", { "users": users }) + + # Only filter on user name if a name is given + if request.GET.get('name'): + users = users.filter(name__icontains=request.GET.get('name')) + + user_page = create_paginator(users, request.GET.get('users'), p_len=15) + return render(request, "user.html", { "users": users, "user_page": user_page }) @dashboard_admin