From b143e5749ca2120012ecc808e105df058a728af8 Mon Sep 17 00:00:00 2001 From: Elijah Date: Fri, 1 Dec 2023 12:08:22 +0100 Subject: [PATCH] Added DID and Services tables --- idhub/admin/tables.py | 101 ++++++++++++++++++- idhub/admin/views.py | 34 +++++-- idhub/templates/idhub/admin/credentials.html | 1 + idhub/templates/idhub/admin/dids.html | 4 + idhub/templates/idhub/admin/services.html | 26 +---- 5 files changed, 131 insertions(+), 35 deletions(-) diff --git a/idhub/admin/tables.py b/idhub/admin/tables.py index f19d450..ec06042 100644 --- a/idhub/admin/tables.py +++ b/idhub/admin/tables.py @@ -1,7 +1,7 @@ import django_tables2 as tables from django.utils.html import format_html -from idhub.models import Rol, Event +from idhub.models import Rol, Event, Service, VerificableCredential, DID from idhub_auth.models import User @@ -95,8 +95,107 @@ class RolesTable(tables.Table): fields = ("name", "description") +class ServicesTable(tables.Table): + domain = tables.Column(verbose_name="Service") + role = tables.Column(empty_values=()) + edit_service = ButtonColumn( + linkify={ + "viewname": "idhub:admin_service_edit", + "args": [tables.A("pk")] + }, + orderable=False + ) + + delete_service = ButtonColumn( + linkify={ + "viewname": "idhub:admin_service_del", + "args": [tables.A("pk")] + }, + orderable=False + ) + + def render_role(self, record): + return record.get_roles() + + def render_edit_service(self): + return format_html('') + + def render_delete_service(self): + return format_html('') + + def order_role(self, queryset, is_descending): + queryset = queryset.order_by( + ("-" if is_descending else "") + "rol" + ) + + return (queryset, True) + + class Meta: + model = Service + template_name = "idhub/custom_table.html" + fields = ("domain", "description", "role", + "edit_service", "delete_service") + + class DashboardTable(tables.Table): class Meta: model = Event template_name = "idhub/custom_table.html" fields = ("type", "message", "created") + + +class CredentialTable(tables.Table): + type = tables.Column(empty_values=()) + details = tables.Column(empty_values=()) + issued_on = tables.Column(verbose_name="Issued") + view_credential = ButtonColumn( + linkify={ + "viewname": "idhub:admin_credential", + "args": [tables.A("pk")] + }, + orderable=False + ) + + def render_type(self, record): + return record.type() + + def render_details(self, record): + return record.description() + + def render_view_credential(self): + return format_html('') + + class Meta: + model = VerificableCredential + template_name = "idhub/custom_table.html" + fields = ("type", "details", "issued_on", "status", "user") + + +class DIDTable(tables.Table): + created_at = tables.Column(verbose_name="Date") + did = tables.Column(verbose_name="ID") + edit_did = ButtonColumn( + linkify={ + "viewname": "idhub:admin_dids_edit", + "args": [tables.A("pk")] + }, + orderable=False, + verbose_name="Edit DID" + ) + delete_template_code = """""" + delete_did = tables.TemplateColumn(template_code=delete_template_code, + orderable=False, + verbose_name="Delete DID") + + def render_edit_did(self): + return format_html('') + + class Meta: + model = DID + template_name = "idhub/custom_table.html" + fields = ("created_at", "label", "did", "edit_did", "delete_did") diff --git a/idhub/admin/views.py b/idhub/admin/views.py index bc93abe..1509433 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -34,7 +34,10 @@ from idhub.admin.forms import ( from idhub.admin.tables import ( DashboardTable, UserTable, - RolesTable + RolesTable, + ServicesTable, + CredentialTable, + DIDTable ) from idhub.models import ( DID, @@ -473,17 +476,20 @@ class RolDeleteView(AccessControl): return redirect('idhub:admin_roles') -class ServicesView(AccessControl): +class ServicesView(AccessControl, SingleTableView): template_name = "idhub/admin/services.html" + table_class = ServicesTable subtitle = _('Manage services') icon = '' + model = Service def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) - context.update({ - 'services': Service.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 ServiceRegisterView(AccessControl, CreateView): template_name = "idhub/admin/service_register.html" @@ -546,10 +552,12 @@ class ServiceDeleteView(AccessControl): return redirect('idhub:admin_services') -class CredentialsView(Credentials): +class CredentialsView(Credentials, SingleTableView): template_name = "idhub/admin/credentials.html" + table_class = CredentialTable subtitle = _('View credentials') icon = '' + model = VerificableCredential def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) @@ -632,19 +640,26 @@ class DeleteCredentialsView(Credentials): return redirect(self.success_url) -class DidsView(Credentials): +class DidsView(Credentials, SingleTableView): template_name = "idhub/admin/dids.html" + table_class = DIDTable subtitle = _('Manage identities (DID)') icon = 'bi bi-patch-check-fill' wallet = True + model = DID def get_context_data(self, **kwargs): + queryset = kwargs.pop('object_list', None) + if queryset is None: + self.object_list = self.model.objects.all() + context = super().get_context_data(**kwargs) context.update({ 'dids': DID.objects.filter(user=self.request.user), }) return context + class DidRegisterView(Credentials, CreateView): template_name = "idhub/admin/did_register.html" subtitle = _('Add a new organizational identity (DID)') @@ -900,4 +915,3 @@ class ImportAddView(ImportExport, FormView): else: messages.error(self.request, _("Error importing the file!")) return super().form_valid(form) - diff --git a/idhub/templates/idhub/admin/credentials.html b/idhub/templates/idhub/admin/credentials.html index cab6a97..43dc413 100644 --- a/idhub/templates/idhub/admin/credentials.html +++ b/idhub/templates/idhub/admin/credentials.html @@ -1,5 +1,6 @@ {% extends "idhub/base_admin.html" %} {% load i18n %} +{% load render_table from django_tables2 %} {% block content %}

diff --git a/idhub/templates/idhub/admin/dids.html b/idhub/templates/idhub/admin/dids.html index c5cb896..4ae6213 100644 --- a/idhub/templates/idhub/admin/dids.html +++ b/idhub/templates/idhub/admin/dids.html @@ -1,5 +1,6 @@ {% extends "idhub/base_admin.html" %} {% load i18n %} +{% load render_table from django_tables2 %} {% block content %}

@@ -8,6 +9,8 @@

+ {% render_table table %} + {% comment %}
@@ -31,6 +34,7 @@ {% endfor %}
+ {% endcomment %} diff --git a/idhub/templates/idhub/admin/services.html b/idhub/templates/idhub/admin/services.html index 639d1c1..8b4693b 100644 --- a/idhub/templates/idhub/admin/services.html +++ b/idhub/templates/idhub/admin/services.html @@ -1,5 +1,6 @@ {% extends "idhub/base_admin.html" %} {% load i18n %} +{% load render_table from django_tables2 %} {% block content %}

@@ -8,33 +9,10 @@

-
- - - - - - - - - - - - {% for service in services.all %} - - - - - - - - {% endfor %} - -
{{ service.domain }}{{ service.description }}{{ service.get_roles }}
+ {% render_table table %} -
{% endblock %}