token to user panel link and add tags to tokens

This commit is contained in:
Cayo Puigdefabregas 2024-10-10 14:03:08 +02:00
parent 533bee30ec
commit 3e967c5a1a
9 changed files with 106 additions and 21 deletions

View File

@ -1,4 +1,4 @@
# Generated by Django 5.0.6 on 2024-10-10 10:35
# Generated by Django 5.0.6 on 2024-10-10 11:34
import django.db.models.deletion
from django.conf import settings
@ -26,6 +26,7 @@ class Migration(migrations.Migration):
verbose_name="ID",
),
),
("tag", models.CharField(max_length=50)),
("token", models.UUIDField()),
(
"owner",

View File

@ -1,9 +1,8 @@
from django.db import models
from user.models import User
# Create your models here.
class Token(models.Model):
tag = models.CharField(max_length=50)
token = models.UUIDField()
owner = models.ForeignKey(User, on_delete=models.CASCADE)

View File

@ -33,12 +33,30 @@ class TokensTable(tables.Table):
},
orderable=False
)
edit_token = ButtonColumn(
linkify={
"viewname": "api:edit_token",
"args": [tables.A("pk")]
},
attrs = {
"a": {
"type": "button",
"class": "text-primary",
"title": "Remove",
}
},
orderable=False,
verbose_name="Edit"
)
token = tables.Column(verbose_name=_("Token"), empty_values=())
tag = tables.Column(verbose_name=_("Tag"), empty_values=())
def render_view_user(self):
return format_html('<i class="bi bi-eye"></i>')
def render_edit_token(self):
return format_html('<i class="bi bi-pencil-square"></i>')
# def render_token(self, record):
# return record.get_memberships()
@ -63,5 +81,5 @@ class TokensTable(tables.Table):
class Meta:
model = Token
template_name = "custom_table.html"
fields = ("token", "view_user")
fields = ("token", "tag", "edit_token")

View File

@ -0,0 +1,32 @@
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<div class="row">
<div class="col">
<h3>{{ subtitle }}</h3>
</div>
</div>
{% 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 %}
{% bootstrap_form form %}
<div class="form-actions-no-box">
<a class="btn btn-grey" href="{% url 'api:tokens' %}">{% translate "Cancel" %}</a>
<input class="btn btn-green-admin" type="submit" name="submit" value="{% translate 'Save' %}" />
</div>
</form>
{% endblock %}

View File

@ -8,6 +8,7 @@
{{ subtitle }}
</h3>
{% render_table table %}
<div class="form-actions-no-box">
<a class="btn btn-green-admin" href="{% url 'api:new_token' %}">{% translate "Generate a new token" %} <i class="bi bi-plus"></i></a>
</div>

View File

@ -9,5 +9,6 @@ urlpatterns = [
path('snapshot/', views.NewSnapshot, name='new_snapshot'),
path('tokens/', views.TokenView.as_view(), name='tokens'),
path('tokens/new', views.TokenNewView.as_view(), name='new_token'),
path("tokens/<int:pk>/edit", views.EditTokenView.as_view(), name="edit_token"),
path('tokens/<int:pk>/del', views.TokenDeleteView.as_view(), name='delete_token'),
]

View File

@ -1,13 +1,18 @@
import json
from django.urls import reverse_lazy
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.core.exceptions import ValidationError
from django.views.generic.edit import DeleteView
from django.views.generic.base import View
from django.http import JsonResponse
from django_tables2 import SingleTableView
from django.views.generic.base import View
from django.views.generic.edit import (
CreateView,
DeleteView,
UpdateView,
)
from django.http import JsonResponse
from uuid import uuid4
from dashboard.mixins import DashboardView
@ -89,7 +94,7 @@ class TokenView(DashboardView, SingleTableView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({
'tokens': Token.objects,
'tokens': Token.objects.all(),
})
return context
@ -105,10 +110,42 @@ class TokenDeleteView(DashboardView, DeleteView):
return redirect('api:tokens')
class TokenNewView(DashboardView, View):
class TokenNewView(DashboardView, CreateView):
template_name = "new_token.html"
title = _("Credential management")
section = "Credential"
subtitle = _('New Tokens')
icon = 'bi bi-key'
model = Token
success_url = reverse_lazy('api:tokens')
fields = (
"tag",
)
def get(self, request, *args, **kwargs):
Token.objects.create(token=uuid4(), owner=self.request.user)
def form_valid(self, form):
form.instance.owner = self.request.user
form.instance.token = uuid4()
return super().form_valid(form)
return redirect('api:tokens')
class EditTokenView(DashboardView, UpdateView):
template_name = "new_token.html"
title = _("Credential management")
section = "Credential"
subtitle = _('New Tokens')
icon = 'bi bi-key'
model = Token
success_url = reverse_lazy('api:tokens')
fields = (
"tag",
)
def get_form_kwargs(self):
pk = self.kwargs.get('pk')
self.object = get_object_or_404(
self.model,
owner=self.request.user,
pk=pk,
)
kwargs = super().get_form_kwargs()
return kwargs

View File

@ -163,12 +163,6 @@
</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link fw-bold" href="{% url 'api:tokens' %}">
<i class="bi-menu-button-wide icon_sidebar"></i>
{% trans 'Tokens' %}
</a>
</li>
</ul>
</div>
</nav>

View File

@ -12,7 +12,9 @@
</div>
<div class="row">
<a class="nav-link fw-bold" href="{% url 'api:tokens' %}">
{% trans 'Tokens' %}
</a>
</div>
{% endblock %}