IdHub/idhub/admin/tables.py

283 lines
8.8 KiB
Python
Raw Permalink Normal View History

import django_tables2 as tables
2023-11-29 13:41:15 +00:00
from django.utils.html import format_html
2024-03-20 12:02:50 +00:00
from django.utils.translation import gettext_lazy as _
2023-11-29 13:41:15 +00:00
2023-12-06 10:45:04 +00:00
from idhub.models import (
Rol,
Event,
Service,
VerificableCredential,
DID,
2023-12-15 17:40:56 +00:00
File_datas,
Schemas
2023-12-06 10:45:04 +00:00
)
from idhub_auth.models import User
2023-11-29 13:41:15 +00:00
class ButtonColumn(tables.Column):
attrs = {
"a": {
"type": "button",
"class": "text-primary",
}
}
2023-11-30 16:05:31 +00:00
# it makes no sense to order a column of buttons
orderable = False
2023-11-29 13:41:15 +00:00
# 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):
2023-11-29 13:41:15 +00:00
view_user = ButtonColumn(
2024-03-20 12:02:50 +00:00
verbose_name=_("View"),
2023-11-29 13:41:15 +00:00
linkify={
"viewname": "idhub:admin_people",
"args": [tables.A("pk")]
},
2023-11-30 16:05:31 +00:00
orderable=False
)
2024-03-20 12:02:50 +00:00
membership = tables.Column(verbose_name=_("Membership"), empty_values=())
role = tables.Column(verbose_name=_("Role"), empty_values=())
2023-11-29 13:41:15 +00:00
2023-11-30 16:05:31 +00:00
def render_view_user(self):
return format_html('<i class="bi bi-eye"></i>')
2023-11-29 13:41:15 +00:00
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"
2023-11-29 13:41:15 +00:00
fields = ("last_name", "first_name", "email", "membership", "role",
"view_user")
class RolesTable(tables.Table):
2023-11-30 16:05:31 +00:00
view_role = ButtonColumn(
2024-03-20 12:02:50 +00:00
verbose_name=_("View"),
2023-11-30 16:05:31 +00:00
linkify={
"viewname": "idhub:admin_rol_edit",
"args": [tables.A("pk")]
},
orderable=False
)
delete_role = ButtonColumn(
2024-03-20 12:02:50 +00:00
verbose_name=_("Delete"),
2023-11-30 16:05:31 +00:00
linkify={
"viewname": "idhub:admin_rol_del",
"args": [tables.A("pk")]
},
orderable=False
)
def render_view_role(self):
return format_html('<i class="bi bi-pencil-square"></i>')
def render_delete_role(self):
return format_html('<i class="bi bi-trash">')
class Meta:
model = Rol
template_name = "idhub/custom_table.html"
fields = ("name", "description")
2023-12-01 11:08:22 +00:00
class ServicesTable(tables.Table):
domain = tables.Column(verbose_name="Service")
role = tables.Column(empty_values=())
edit_service = ButtonColumn(
2024-03-20 12:02:50 +00:00
verbose_name=_("Edit"),
2023-12-01 11:08:22 +00:00
linkify={
"viewname": "idhub:admin_service_edit",
"args": [tables.A("pk")]
},
orderable=False
)
delete_service = ButtonColumn(
2024-03-20 12:02:50 +00:00
verbose_name=_("Delete"),
2023-12-01 11:08:22 +00:00
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('<i class="bi bi-pencil-square"></i>')
def render_delete_service(self):
return format_html('<i class="bi bi-trash">')
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")
empty_text = "No events available"
2023-12-01 11:08:22 +00:00
class CredentialTable(tables.Table):
2024-03-20 12:02:50 +00:00
type = tables.Column(verbose_name=_("Type"), empty_values=())
# Pending VerificableCredential description fix
2024-03-20 12:02:50 +00:00
details = tables.Column(_("Details"), empty_values=(), orderable=False)
issued_on = tables.Column(verbose_name=_("Issued"))
2023-12-01 11:08:22 +00:00
view_credential = ButtonColumn(
2024-03-20 12:02:50 +00:00
verbose_name=_("View"),
2023-12-01 11:08:22 +00:00
linkify={
"viewname": "idhub:admin_credential",
"args": [tables.A("pk")]
},
orderable=False
)
def render_details(self, record):
return record.description()
def render_view_credential(self):
return format_html('<i class="bi bi-eye"></i>')
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,
2024-03-20 12:02:50 +00:00
verbose_name="Edit"
2023-12-01 11:08:22 +00:00
)
delete_template_code = """<a class="text-danger"
href="javascript:void()"
data-bs-toggle="modal"
data-bs-target="#confirm-delete-{{ record.id }}"
title="Remove"
><i class="bi bi-trash"></i></a>"""
delete_did = tables.TemplateColumn(template_code=delete_template_code,
orderable=False,
2024-03-20 12:02:50 +00:00
verbose_name="Delete")
2023-12-01 11:08:22 +00:00
def render_edit_did(self):
return format_html('<i class="bi bi-pencil-square"></i>')
class Meta:
model = DID
template_name = "idhub/custom_table.html"
fields = ("created_at", "label", "did", "edit_did", "delete_did")
2023-12-06 10:45:04 +00:00
class DataTable(tables.Table):
2024-03-20 12:02:50 +00:00
created_at = tables.Column(verbose_name=_("Date"))
file_name = tables.Column(verbose_name=_("File"))
2024-03-12 17:37:01 +00:00
delete_template_code = """<a class="text-danger"
href="{% url 'idhub:admin_import_del' record.id %}"
title="Remove"
><i class="bi bi-trash"></i></a>"""
delete_data = tables.TemplateColumn(template_code=delete_template_code,
orderable=False,
2024-03-20 12:02:50 +00:00
verbose_name=_("Delete"))
2023-12-06 10:45:04 +00:00
class Meta:
model = File_datas
template_name = "idhub/custom_table.html"
2024-03-12 17:37:01 +00:00
fields = ("created_at", "file_name", "success", "delete_data")
2023-12-15 17:40:56 +00:00
class TemplateTable(tables.Table):
view_schema = ButtonColumn(
2024-03-20 12:02:50 +00:00
verbose_name=_("View"),
2023-12-15 17:40:56 +00:00
linkify={
"viewname": "idhub:admin_schemas_download",
"args": [tables.A("pk")]
},
orderable=False
)
delete_template_code = """<a class="text-danger"
href="javascript:void()"
data-bs-toggle="modal"
2024-03-01 08:31:43 +00:00
data-bs-target="#confirm-delete-{}"
2023-12-15 17:40:56 +00:00
title="Remove"
><i class="bi bi-trash"></i></a>"""
delete_schema = tables.TemplateColumn(template_code=delete_template_code,
orderable=False,
2024-03-20 12:02:50 +00:00
verbose_name=_("Delete"))
2023-12-15 17:40:56 +00:00
2024-03-20 12:02:50 +00:00
name = tables.Column(verbose_name=_("Name"))
description = tables.Column(verbose_name=_("Description"))
def order_name(self, queryset, is_descending):
queryset = Schemas.objects.order_by(
("-" if is_descending else "") + "_name"
)
return (queryset, True)
def order_description(self, queryset, is_descending):
queryset = Schemas.objects.order_by(
("-" if is_descending else "") + "_description"
)
return (queryset, True)
def render_delete_schema(self, value, record):
2024-03-01 08:31:43 +00:00
if not record.has_credentials:
tmpl = self.delete_template_code.format(record.id)
return format_html(tmpl)
else:
return ""
2023-12-15 17:40:56 +00:00
class Meta:
model = Schemas
template_name = "idhub/custom_table.html"
fields = ("created_at", "file_schema", "name", "description",
2023-12-15 17:40:56 +00:00
"view_schema", "delete_schema")