diff --git a/idhub/admin/tables.py b/idhub/admin/tables.py index 79c22ba..252f149 100644 --- a/idhub/admin/tables.py +++ b/idhub/admin/tables.py @@ -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('') + + 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): diff --git a/idhub/admin/views.py b/idhub/admin/views.py index b6dcbc8..6ea5acd 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -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" diff --git a/idhub/templates/idhub/admin/people.html b/idhub/templates/idhub/admin/people.html index 25b7c61..a40b318 100644 --- a/idhub/templates/idhub/admin/people.html +++ b/idhub/templates/idhub/admin/people.html @@ -1,12 +1,14 @@ {% extends "idhub/base_admin.html" %} {% load i18n %} +{% load render_table from django_tables2 %} {% block content %}