Added pagination and column sorting to the admin dashboard, User, and Roles tables

This commit is contained in:
Elijah 2023-11-22 19:46:36 +01:00
parent b279ab94d6
commit 4efe97d6e4
6 changed files with 51 additions and 92 deletions

24
idhub/admin/tables.py Normal file
View File

@ -0,0 +1,24 @@
import django_tables2 as tables
from idhub.models import Rol, Event
from idhub_auth.models import User
class UserTable(tables.Table):
class Meta:
model = User
template_name = "idhub/custom_table.html"
fields = ("first_name", "last_name", "email", "is_active", "is_admin")
class RolesTable(tables.Table):
class Meta:
model = Rol
template_name = "idhub/custom_table.html"
fields = ("name", "description")
class DashboardTable(tables.Table):
class Meta:
model = Event
template_name = "idhub/custom_table.html"
fields = ("type", "message", "created")

View File

@ -5,6 +5,7 @@ import pandas as pd
from pathlib import Path from pathlib import Path
from jsonschema import validate from jsonschema import validate
from smtplib import SMTPException from smtplib import SMTPException
from django_tables2 import SingleTableView
from django.conf import settings from django.conf import settings
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -30,6 +31,11 @@ from idhub.admin.forms import (
SchemaForm, SchemaForm,
UserRolForm, UserRolForm,
) )
from idhub.admin.tables import (
UserTable,
DashboardTable,
RolesTable
)
from idhub.models import ( from idhub.models import (
DID, DID,
Event, Event,
@ -43,19 +49,15 @@ from idhub.models import (
) )
class DashboardView(AdminView, TemplateView): class DashboardView(AdminView, SingleTableView):
template_name = "idhub/admin/dashboard.html" template_name = "idhub/admin/dashboard.html"
table_class = DashboardTable
title = _('Dashboard') title = _('Dashboard')
subtitle = _('Events') subtitle = _('Events')
icon = 'bi bi-bell' icon = 'bi bi-bell'
section = "Home" section = "Home"
model = Event
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'events': Event.objects.filter(user=None),
})
return context
class People(AdminView): class People(AdminView):
title = _("User management") title = _("User management")
@ -82,10 +84,12 @@ class ImportExport(AdminView):
section = "ImportExport" section = "ImportExport"
class PeopleListView(People, TemplateView): class PeopleListView(People, SingleTableView):
template_name = "idhub/admin/people.html" template_name = "idhub/admin/people.html"
subtitle = _('View users') subtitle = _('View users')
icon = 'bi bi-person' icon = 'bi bi-person'
table_class = UserTable
model = User
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
@ -402,13 +406,16 @@ class RolesView(AccessControl):
template_name = "idhub/admin/roles.html" template_name = "idhub/admin/roles.html"
subtitle = _('Manage roles') subtitle = _('Manage roles')
icon = '' icon = ''
table_class = RolesTable
model = Rol
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) queryset = kwargs.pop('object_list', None)
context.update({ if queryset is None:
'roles': Rol.objects, self.object_list = self.model.objects.all()
})
return context return super().get_context_data(**kwargs)
class RolRegisterView(AccessControl, CreateView): class RolRegisterView(AccessControl, CreateView):
template_name = "idhub/admin/rol_register.html" template_name = "idhub/admin/rol_register.html"

View File

@ -1,29 +1,11 @@
{% extends "idhub/base_admin.html" %} {% extends "idhub/base_admin.html" %}
{% load i18n %} {% load i18n %}
{% load render_table from django_tables2 %}
{% block content %} {% block content %}
<h3> <h3>
<i class="{{ icon }}"></i> <i class="{{ icon }}"></i>
{{ subtitle }} {{ subtitle }}
</h3> </h3>
<div class="table-responsive"> {% render_table table %}
<table class="table table-striped table-sm">
<thead>
<tr>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Event' %}</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Description' %}</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Date' %}</button></th>
</tr>
</thead>
<tbody>
{% for ev in events %}
<tr>
<td>{{ ev.get_type_name }}</td>
<td>{{ ev.message }}</td>
<td>{{ ev.created }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %} {% endblock %}

View File

@ -1,39 +1,11 @@
{% extends "idhub/base_admin.html" %} {% extends "idhub/base_admin.html" %}
{% load i18n %} {% load i18n %}
{% load render_table from django_tables2 %}
{% block content %} {% block content %}
<h3> <h3>
<i class="{{ icon }}"></i> <i class="{{ icon }}"></i>
{{ subtitle }} {{ subtitle }}
</h3> </h3>
<div class="table-responsive"> {% render_table table %}
<table class="table table-striped table-sm">
<thead>
<tr>
<th scope="col"><button type="button" class="btn btn-green-admin border border-dark">{% trans 'Last name' %}</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'First name' %}</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">Email</button></th>
<th scope="col" class="text-center"><button type="button" class="btn btn-grey border border-dark">{% trans 'Membership' %}</button></th>
<th scope="col" class="text-center"><button type="button" class="btn btn-grey border border-dark">{% trans 'Role' %}</button></th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.last_name|default:'' }}</td>
<td>{{ user.first_name|default:'' }}</td>
<td>{{ user.email }}</td>
<td class="text-center">
{{ user.get_memberships }}
</td>
<td class="text-center">
{{ user.get_roles }}
</td>
<td><a type="button" class="text-primary" href="{% url 'idhub:admin_people' user.id %}" title="{% trans 'View' %}"><i class="bi bi-eye"></i></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %} {% endblock %}

View File

@ -1,38 +1,11 @@
{% extends "idhub/base_admin.html" %} {% extends "idhub/base_admin.html" %}
{% load i18n %} {% load i18n %}
{% load render_table from django_tables2 %}
{% block content %} {% block content %}
<h3> <h3>
<i class="{{ icon }}"></i> <i class="{{ icon }}"></i>
{{ subtitle }} {{ subtitle }}
</h3> </h3>
<div class="row mt-5"> {% render_table table %}
<div class="col">
<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Role' %}</button></th>
<th scope="col"><button type="button" class="btn btn-grey border border-dark">{% trans 'Description' %}</button></th>
<th scope="col"></th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{% for rol in roles.all %}
<tr>
<td>{{ rol.name }}</td>
<td>{{ rol.description|default:""}}</td>
<td><a href="{% url 'idhub:admin_rol_edit' rol.id %}" title="{% trans 'Edit' %}"><i class="bi bi-pencil-square"></i></a></td>
<td><a href="{% url 'idhub:admin_rol_del' rol.id %}" title="{% trans 'Delete' %}"><i class="bi bi-trash"></i></a></td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="form-actions-no-box">
<a class="btn btn-green-admin" href="{% url 'idhub:admin_rol_new' %}">{% translate "Add Role" %} <i class="bi bi-plus"></i></a>
</div>
</div>
</div>
</div>
{% endblock %} {% endblock %}

View File

@ -70,6 +70,7 @@ INSTALLED_APPS = [
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'django_extensions', 'django_extensions',
'django_bootstrap5', 'django_bootstrap5',
'django_tables',
'idhub_auth', 'idhub_auth',
'idhub' 'idhub'
] ]