From 4efe97d6e4f2d70a42479de59219fe485787e99b Mon Sep 17 00:00:00 2001 From: Elijah Date: Wed, 22 Nov 2023 19:46:36 +0100 Subject: [PATCH] Added pagination and column sorting to the admin dashboard, User, and Roles tables --- idhub/admin/tables.py | 24 ++++++++++++++++ idhub/admin/views.py | 33 +++++++++++++--------- idhub/templates/idhub/admin/dashboard.html | 22 ++------------- idhub/templates/idhub/admin/people.html | 32 ++------------------- idhub/templates/idhub/admin/roles.html | 31 ++------------------ trustchain_idhub/settings.py | 1 + 6 files changed, 51 insertions(+), 92 deletions(-) create mode 100644 idhub/admin/tables.py diff --git a/idhub/admin/tables.py b/idhub/admin/tables.py new file mode 100644 index 0000000..7bd76d8 --- /dev/null +++ b/idhub/admin/tables.py @@ -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") diff --git a/idhub/admin/views.py b/idhub/admin/views.py index 1b380d2..6cf48db 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -5,6 +5,7 @@ import pandas as pd from pathlib import Path from jsonschema import validate from smtplib import SMTPException +from django_tables2 import SingleTableView from django.conf import settings from django.utils.translation import gettext_lazy as _ @@ -30,6 +31,11 @@ from idhub.admin.forms import ( SchemaForm, UserRolForm, ) +from idhub.admin.tables import ( + UserTable, + DashboardTable, + RolesTable +) from idhub.models import ( DID, Event, @@ -43,19 +49,15 @@ from idhub.models import ( ) -class DashboardView(AdminView, TemplateView): +class DashboardView(AdminView, SingleTableView): template_name = "idhub/admin/dashboard.html" + table_class = DashboardTable title = _('Dashboard') subtitle = _('Events') icon = 'bi bi-bell' 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): title = _("User management") @@ -82,10 +84,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) @@ -402,13 +406,16 @@ class RolesView(AccessControl): template_name = "idhub/admin/roles.html" subtitle = _('Manage roles') icon = '' + table_class = RolesTable + model = Rol def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) - context.update({ - 'roles': Rol.objects, - }) - return context + queryset = kwargs.pop('object_list', None) + if queryset is None: + self.object_list = self.model.objects.all() + + return super().get_context_data(**kwargs) + class RolRegisterView(AccessControl, CreateView): template_name = "idhub/admin/rol_register.html" diff --git a/idhub/templates/idhub/admin/dashboard.html b/idhub/templates/idhub/admin/dashboard.html index d1317e7..4862f82 100644 --- a/idhub/templates/idhub/admin/dashboard.html +++ b/idhub/templates/idhub/admin/dashboard.html @@ -1,29 +1,11 @@ {% extends "idhub/base_admin.html" %} {% load i18n %} +{% load render_table from django_tables2 %} {% block content %}

{{ subtitle }}

-
- - - - - - - - - - {% for ev in events %} - - - - - - {% endfor %} - -
{{ ev.get_type_name }}{{ ev.message }}{{ ev.created }}
-
+{% render_table table %} {% endblock %} diff --git a/idhub/templates/idhub/admin/people.html b/idhub/templates/idhub/admin/people.html index 25b7c61..4862f82 100644 --- a/idhub/templates/idhub/admin/people.html +++ b/idhub/templates/idhub/admin/people.html @@ -1,39 +1,11 @@ {% extends "idhub/base_admin.html" %} {% load i18n %} +{% load render_table from django_tables2 %} {% block content %}

{{ subtitle }}

-
- - - - - - - - - - - - - {% for user in users %} - - - - - - - - - {% endfor %} - -
{{ user.last_name|default:'' }}{{ user.first_name|default:'' }}{{ user.email }} - {{ user.get_memberships }} - - {{ user.get_roles }} -
-
+{% render_table table %} {% endblock %} diff --git a/idhub/templates/idhub/admin/roles.html b/idhub/templates/idhub/admin/roles.html index 25bfd93..4862f82 100644 --- a/idhub/templates/idhub/admin/roles.html +++ b/idhub/templates/idhub/admin/roles.html @@ -1,38 +1,11 @@ {% extends "idhub/base_admin.html" %} {% load i18n %} +{% load render_table from django_tables2 %} {% block content %}

{{ subtitle }}

-
-
-
- - - - - - - - - - - {% for rol in roles.all %} - - - - - - - {% endfor %} - -
{{ rol.name }}{{ rol.description|default:""}}
- -
-
-
+{% render_table table %} {% endblock %} diff --git a/trustchain_idhub/settings.py b/trustchain_idhub/settings.py index 30bbc0a..aef9742 100644 --- a/trustchain_idhub/settings.py +++ b/trustchain_idhub/settings.py @@ -70,6 +70,7 @@ INSTALLED_APPS = [ 'django.contrib.staticfiles', 'django_extensions', 'django_bootstrap5', + 'django_tables', 'idhub_auth', 'idhub' ]