diff --git a/api/migrations/0001_initial.py b/api/migrations/0001_initial.py index 2101a8a..b15c44e 100644 --- a/api/migrations/0001_initial.py +++ b/api/migrations/0001_initial.py @@ -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", diff --git a/api/models.py b/api/models.py index b8bbc24..8cdf430 100644 --- a/api/models.py +++ b/api/models.py @@ -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) diff --git a/api/tables.py b/api/tables.py index ac1cc7a..27c8cbf 100644 --- a/api/tables.py +++ b/api/tables.py @@ -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('') + def render_edit_token(self): + return format_html('') + # 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") diff --git a/api/templates/new_token.html b/api/templates/new_token.html new file mode 100644 index 0000000..77e0503 --- /dev/null +++ b/api/templates/new_token.html @@ -0,0 +1,32 @@ +{% extends "base.html" %} +{% load i18n %} + +{% block content %} +
+
+

{{ subtitle }}

+
+
+ +{% load django_bootstrap5 %} +
+{% csrf_token %} +{% if form.errors %} + +{% endif %} +{% bootstrap_form form %} +
+ {% translate "Cancel" %} + +
+ +
+{% endblock %} diff --git a/api/templates/token.html b/api/templates/token.html index 5185090..2ba2ca7 100644 --- a/api/templates/token.html +++ b/api/templates/token.html @@ -8,6 +8,7 @@ {{ subtitle }} {% render_table table %} +
{% translate "Generate a new token" %}
diff --git a/api/urls.py b/api/urls.py index f3c8028..5eac279 100644 --- a/api/urls.py +++ b/api/urls.py @@ -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//edit", views.EditTokenView.as_view(), name="edit_token"), path('tokens//del', views.TokenDeleteView.as_view(), name='delete_token'), ] diff --git a/api/views.py b/api/views.py index fbccb79..057ede2 100644 --- a/api/views.py +++ b/api/views.py @@ -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 diff --git a/dashboard/templates/base.html b/dashboard/templates/base.html index 6aeee6a..40e6658 100644 --- a/dashboard/templates/base.html +++ b/dashboard/templates/base.html @@ -163,12 +163,6 @@ - diff --git a/user/templates/panel.html b/user/templates/panel.html index 99e22cb..ca27036 100644 --- a/user/templates/panel.html +++ b/user/templates/panel.html @@ -12,7 +12,9 @@ {% endblock %}