Updated User management admin table

This commit is contained in:
Elijah 2023-11-29 14:41:15 +01:00 committed by Elahi
parent 8ee3f561a4
commit 5d934469c2
3 changed files with 66 additions and 6 deletions

View File

@ -1,14 +1,64 @@
import django_tables2 as tables
from django.utils.translation import gettext_lazy as _
from django.utils.html import format_html
from idhub.models import Rol, Event
from idhub_auth.models import User
class ButtonColumn(tables.Column):
attrs = {
"a": {
"type": "button",
"class": "text-primary",
"title": "'View'",
}
}
# django_tables will only call the render function if it doesn't find
# any empty values in the data, so we stop it from matching the data
# to any value considered empty
empty_values = ()
def render(self):
return format_html('<i class="bi bi-eye"></i>')
class UserTable(tables.Table):
view_user = ButtonColumn(
linkify={
"viewname": "idhub:admin_people",
"args": [tables.A("pk")]
},
orderable=False,
)
membership = tables.Column(empty_values=())
role = tables.Column(empty_values=())
def render_membership(self, record):
return record.get_memberships()
def order_membership(self, queryset, is_descending):
# TODO: Test that this doesn't return more rows than it should
queryset = queryset.order_by(
("-" if is_descending else "") + "memberships__type"
)
return (queryset, True)
def render_role(self, record):
return record.get_roles()
def order_role(self, queryset, is_descending):
queryset = queryset.order_by(
("-" if is_descending else "") + "roles"
)
return (queryset, True)
class Meta:
model = User
template_name = "idhub/custom_table.html"
fields = ("first_name", "last_name", "email", "is_active", "is_admin")
fields = ("last_name", "first_name", "email", "membership", "role",
"view_user")
class RolesTable(tables.Table):

View File

@ -32,7 +32,8 @@ from idhub.admin.forms import (
UserRolForm,
)
from idhub.admin.tables import (
DashboardTable
DashboardTable,
UserTable
)
from idhub.models import (
DID,
@ -82,10 +83,12 @@ class ImportExport(AdminView):
section = "ImportExport"
class PeopleListView(People, TemplateView):
class PeopleListView(People, SingleTableView):
template_name = "idhub/admin/people.html"
subtitle = _('View users')
icon = 'bi bi-person'
table_class = UserTable
model = User
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
@ -94,6 +97,11 @@ class PeopleListView(People, TemplateView):
})
return context
def get_queryset(self, **kwargs):
queryset = super().get_queryset(**kwargs)
return queryset
class PeopleView(People, TemplateView):
template_name = "idhub/admin/user.html"

View File

@ -1,12 +1,14 @@
{% extends "idhub/base_admin.html" %}
{% load i18n %}
{% load render_table from django_tables2 %}
{% block content %}
<h3>
<i class="{{ icon }}"></i>
{{ subtitle }}
</h3>
<div class="table-responsive">
{% render_table table %}
{% comment %}<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
@ -35,5 +37,5 @@
{% endfor %}
</tbody>
</table>
</div>
</div>{% endcomment %}
{% endblock %}