write label for token

This commit is contained in:
Cayo Puigdefabregas 2025-01-27 16:16:21 +01:00 committed by pedro
parent 672b3f3a8e
commit 6a5d83a450
5 changed files with 83 additions and 10 deletions

View file

@ -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'),
),
]

View file

@ -1,7 +1,10 @@
from django.db import models from django.db import models
from django.utils.translation import gettext_lazy as _
# Create your models here. # Create your models here.
class Token(models.Model): class Token(models.Model):
token = models.UUIDField() token = models.UUIDField()
label = models.CharField(_("Label"), max_length=250, default="")
active = models.BooleanField(_("Active"), default=True)

View file

@ -43,6 +43,7 @@ class TokensTable(tables.Table):
) )
token = tables.Column(verbose_name=_("Token"), empty_values=()) token = tables.Column(verbose_name=_("Token"), empty_values=())
label = tables.Column(verbose_name=_("Label"), empty_values=())
# def render_view_user(self): # def render_view_user(self):
# return format_html('<i class="bi bi-eye"></i>') # return format_html('<i class="bi bi-eye"></i>')
@ -71,13 +72,12 @@ class TokensTable(tables.Table):
class Meta: class Meta:
model = Token model = Token
template_name = "idhub/custom_table.html" template_name = "idhub/custom_table.html"
fields = ("token", "lavel", "active") fields = ("token", "label", "active")
def render_active(self, value): def render_active(self, value):
""" """
Render icons custom based on active value Render icons custom based on active value
""" """
# import pdb; pdb.set_trace()
if value: # if `active` is True if value: # if `active` is True
return format_html('<i class="bi bi-toggle-on text-primary"></i>') return format_html('<i class="bi bi-toggle-on text-primary"></i>')
else: # if `active` is False else: # if `active` is False

View file

@ -0,0 +1,34 @@
{% extends "idhub/base_admin.html" %}
{% load i18n %}
{% block content %}
<h3>
<i class="{{ icon }}"></i>
{{ subtitle }}
</h3>
{% load django_bootstrap5 %}
<form role="form" method="post">
{% csrf_token %}
{% if form.errors %}
<div class="alert alert-danger alert-icon alert-icon-border alert-dismissible" role="alert">
<div class="icon"><span class="mdi mdi-close-circle-o"></span></div>
<div class="message">
{% for field, error in form.errors.items %}
{{ error }}<br />
{% endfor %}
<button class="btn-close" type="button" data-dismiss="alert" aria-label="Close"></button>
</div>
</div>
{% endif %}
<div class="row">
<div class="col-sm-4">
{% bootstrap_form form %}
</div>
</div>
<div class="form-actions-no-box">
<a class="btn btn-grey" href="{% url 'webhook:tokens' %}">{% translate "Cancel" %}</a>
<input class="btn btn-green-admin" type="submit" name="submit" value="{% translate 'Save' %}" />
</div>
</form>
{% endblock %}

View file

@ -3,13 +3,14 @@ import json
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.views.decorators.csrf import csrf_exempt 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.views.generic.base import View
from django.core.cache import cache from django.core.cache import cache
from django.http import JsonResponse from django.http import JsonResponse
from django_tables2 import SingleTableView from django_tables2 import SingleTableView
from pyvckit.verify import verify_vp, verify_vc from pyvckit.verify import verify_vp, verify_vc
from uuid import uuid4 from uuid import uuid4
from django.urls import reverse_lazy
from idhub.mixins import AdminView from idhub.mixins import AdminView
from idhub_auth.models import User from idhub_auth.models import User
@ -30,7 +31,7 @@ def webhook_verify(request):
return JsonResponse({'error': 'Invalid or missing token'}, status=401) return JsonResponse({'error': 'Invalid or missing token'}, status=401)
token = auth_header.split(' ')[1].strip("'").strip('"') 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: if not tk:
return JsonResponse({'error': 'Invalid or missing token'}, status=401) 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) return JsonResponse({'error': 'Invalid or missing token'}, status=401)
token = auth_header.split(' ')[1].strip("'").strip('"') 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: if not tk:
return JsonResponse({'error': 'Invalid or missing token'}, status=401) return JsonResponse({'error': 'Invalid or missing token'}, status=401)
@ -169,10 +170,23 @@ class TokenStatusView(AdminView, DeleteView):
return redirect('webhook:tokens') 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): # return redirect('webhook:tokens')
self.check_valid_user()
Token.objects.create(token=uuid4())
return redirect('webhook:tokens') def form_valid(self, form):
form.instance.token = uuid4()
form.save()
return super().form_valid(form)