From 672b3f3a8e4c21604fdd61c1c50b3da6b558245f Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 27 Jan 2025 13:42:25 +0100 Subject: [PATCH 1/2] add label and active in tokens --- webhook/tables.py | 29 +++++++++++++++++++++++------ webhook/urls.py | 1 + webhook/views.py | 16 ++++++++++++++++ 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/webhook/tables.py b/webhook/tables.py index 498a38e..06809ea 100644 --- a/webhook/tables.py +++ b/webhook/tables.py @@ -1,11 +1,12 @@ import django_tables2 as tables from django.utils.html import format_html from django.utils.translation import gettext_lazy as _ +from django.urls import reverse from webhook.models import Token - -class ButtonColumn(tables.Column): + +class ButtonRemoveColumn(tables.Column): attrs = { "a": { "type": "button", @@ -25,7 +26,7 @@ class ButtonColumn(tables.Column): class TokensTable(tables.Table): - delete = ButtonColumn( + delete = ButtonRemoveColumn( verbose_name=_("Delete"), linkify={ "viewname": "webhook:delete_token", @@ -33,11 +34,18 @@ class TokensTable(tables.Table): }, orderable=False ) + # active = tables.Column(linkify=lambda record: reverse("webhook:status_token", kwargs={"pk": record.pk})) + active = tables.Column( + linkify={ + "viewname": "webhook:status_token", + "args": [tables.A("pk")] + } + ) token = tables.Column(verbose_name=_("Token"), empty_values=()) - def render_view_user(self): - return format_html('') + # def render_view_user(self): + # return format_html('') # def render_token(self, record): # return record.get_memberships() @@ -63,5 +71,14 @@ class TokensTable(tables.Table): class Meta: model = Token template_name = "idhub/custom_table.html" - fields = ("token", "view_user") + fields = ("token", "lavel", "active") + def render_active(self, value): + """ + Render icons custom based on active value + """ + # import pdb; pdb.set_trace() + if value: # if `active` is True + return format_html('') + else: # if `active` is False + return format_html('') diff --git a/webhook/urls.py b/webhook/urls.py index c2d4bd2..b193264 100644 --- a/webhook/urls.py +++ b/webhook/urls.py @@ -11,4 +11,5 @@ urlpatterns = [ path('tokens/', views.WebHookTokenView.as_view(), name='tokens'), path('tokens/new', views.TokenNewView.as_view(), name='new_token'), path('tokens//del', views.TokenDeleteView.as_view(), name='delete_token'), + path('tokens//status', views.TokenStatusView.as_view(), name='status_token'), ] diff --git a/webhook/views.py b/webhook/views.py index fd2d931..7461df8 100644 --- a/webhook/views.py +++ b/webhook/views.py @@ -153,6 +153,22 @@ class TokenDeleteView(AdminView, DeleteView): return redirect('webhook:tokens') +class TokenStatusView(AdminView, DeleteView): + model = Token + + def get(self, request, *args, **kwargs): + self.check_valid_user() + self.pk = kwargs['pk'] + self.object = get_object_or_404(self.model, pk=self.pk) + if self.object.active: + self.object.active = False + else: + self.object.active = True + self.object.save() + + return redirect('webhook:tokens') + + class TokenNewView(AdminView, View): def get(self, request, *args, **kwargs): -- 2.30.2 From 6a5d83a4508fa8f0604b6edec02610e4b1e54379 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 27 Jan 2025 16:16:21 +0100 Subject: [PATCH 2/2] write label for token --- .../0002_token_active_token_label.py | 22 ++++++++++++ webhook/models.py | 3 ++ webhook/tables.py | 4 +-- webhook/templates/new_token.html | 34 +++++++++++++++++++ webhook/views.py | 30 +++++++++++----- 5 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 webhook/migrations/0002_token_active_token_label.py create mode 100644 webhook/templates/new_token.html diff --git a/webhook/migrations/0002_token_active_token_label.py b/webhook/migrations/0002_token_active_token_label.py new file mode 100644 index 0000000..149ce7a --- /dev/null +++ b/webhook/migrations/0002_token_active_token_label.py @@ -0,0 +1,22 @@ +# Generated by Django 4.2.5 on 2025-01-27 09:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ('webhook', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='token', + name='active', + field=models.BooleanField(default=True, verbose_name='Active'), + ), + migrations.AddField( + model_name='token', + name='label', + field=models.CharField(default='', max_length=250, verbose_name='Label'), + ), + ] diff --git a/webhook/models.py b/webhook/models.py index 54218c2..04e6109 100644 --- a/webhook/models.py +++ b/webhook/models.py @@ -1,7 +1,10 @@ from django.db import models +from django.utils.translation import gettext_lazy as _ # Create your models here. class Token(models.Model): token = models.UUIDField() + label = models.CharField(_("Label"), max_length=250, default="") + active = models.BooleanField(_("Active"), default=True) diff --git a/webhook/tables.py b/webhook/tables.py index 06809ea..981e1ce 100644 --- a/webhook/tables.py +++ b/webhook/tables.py @@ -43,6 +43,7 @@ class TokensTable(tables.Table): ) token = tables.Column(verbose_name=_("Token"), empty_values=()) + label = tables.Column(verbose_name=_("Label"), empty_values=()) # def render_view_user(self): # return format_html('') @@ -71,13 +72,12 @@ class TokensTable(tables.Table): class Meta: model = Token template_name = "idhub/custom_table.html" - fields = ("token", "lavel", "active") + fields = ("token", "label", "active") def render_active(self, value): """ Render icons custom based on active value """ - # import pdb; pdb.set_trace() if value: # if `active` is True return format_html('') else: # if `active` is False diff --git a/webhook/templates/new_token.html b/webhook/templates/new_token.html new file mode 100644 index 0000000..03355c1 --- /dev/null +++ b/webhook/templates/new_token.html @@ -0,0 +1,34 @@ +{% extends "idhub/base_admin.html" %} +{% load i18n %} + +{% block content %} +

+ + {{ subtitle }} +

+{% load django_bootstrap5 %} +
+{% csrf_token %} +{% if form.errors %} + +{% endif %} +
+
+ {% bootstrap_form form %} +
+
+ + +
+{% endblock %} diff --git a/webhook/views.py b/webhook/views.py index 7461df8..f741ae8 100644 --- a/webhook/views.py +++ b/webhook/views.py @@ -3,13 +3,14 @@ import json from django.shortcuts import get_object_or_404, redirect from django.utils.translation import gettext_lazy as _ from django.views.decorators.csrf import csrf_exempt -from django.views.generic.edit import DeleteView +from django.views.generic.edit import DeleteView, CreateView from django.views.generic.base import View from django.core.cache import cache from django.http import JsonResponse from django_tables2 import SingleTableView from pyvckit.verify import verify_vp, verify_vc from uuid import uuid4 +from django.urls import reverse_lazy from idhub.mixins import AdminView from idhub_auth.models import User @@ -30,7 +31,7 @@ def webhook_verify(request): return JsonResponse({'error': 'Invalid or missing token'}, status=401) token = auth_header.split(' ')[1].strip("'").strip('"') - tk = Token.objects.filter(token=token).first() + tk = Token.objects.filter(token=token, active=True).first() if not tk: return JsonResponse({'error': 'Invalid or missing token'}, status=401) @@ -70,7 +71,7 @@ def webhook_issue(request): return JsonResponse({'error': 'Invalid or missing token'}, status=401) token = auth_header.split(' ')[1].strip("'").strip('"') - tk = Token.objects.filter(token=token).first() + tk = Token.objects.filter(token=token, active=True).first() if not tk: return JsonResponse({'error': 'Invalid or missing token'}, status=401) @@ -169,10 +170,23 @@ class TokenStatusView(AdminView, DeleteView): return redirect('webhook:tokens') -class TokenNewView(AdminView, View): +class TokenNewView(AdminView, CreateView): + title = _("Token management") + section = "Credential" + subtitle = _('New Tokens') + icon = 'bi bi-key' + title = "Token" + template_name = "new_token.html" + model = Token + fields = ("label",) + success_url = reverse_lazy('webhook:tokens') + # def get(self, request, *args, **kwargs): + # self.check_valid_user() + # Token.objects.create(token=uuid4()) - def get(self, request, *args, **kwargs): - self.check_valid_user() - Token.objects.create(token=uuid4()) + # return redirect('webhook:tokens') - return redirect('webhook:tokens') + def form_valid(self, form): + form.instance.token = uuid4() + form.save() + return super().form_valid(form) -- 2.30.2