user-panel #9

Merged
cayop merged 25 commits from user-panel into main 2024-10-11 14:26:36 +00:00
9 changed files with 106 additions and 21 deletions
Showing only changes of commit 3e967c5a1a - Show all commits

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 import django.db.models.deletion
from django.conf import settings from django.conf import settings
@ -26,6 +26,7 @@ class Migration(migrations.Migration):
verbose_name="ID", verbose_name="ID",
), ),
), ),
("tag", models.CharField(max_length=50)),
("token", models.UUIDField()), ("token", models.UUIDField()),
( (
"owner", "owner",

View file

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

View file

@ -33,12 +33,30 @@ class TokensTable(tables.Table):
}, },
orderable=False 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=()) token = tables.Column(verbose_name=_("Token"), empty_values=())
tag = tables.Column(verbose_name=_("Tag"), 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>')
def render_edit_token(self):
return format_html('<i class="bi bi-pencil-square"></i>')
# def render_token(self, record): # def render_token(self, record):
# return record.get_memberships() # return record.get_memberships()
@ -63,5 +81,5 @@ class TokensTable(tables.Table):
class Meta: class Meta:
model = Token model = Token
template_name = "custom_table.html" 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 }} {{ subtitle }}
</h3> </h3>
{% render_table table %} {% render_table table %}
<div class="form-actions-no-box"> <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> <a class="btn btn-green-admin" href="{% url 'api:new_token' %}">{% translate "Generate a new token" %} <i class="bi bi-plus"></i></a>
</div> </div>

View file

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

View file

@ -1,13 +1,18 @@
import json import json
from django.urls import reverse_lazy
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.core.exceptions import ValidationError 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_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 uuid import uuid4
from dashboard.mixins import DashboardView from dashboard.mixins import DashboardView
@ -89,7 +94,7 @@ class TokenView(DashboardView, SingleTableView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context.update({ context.update({
'tokens': Token.objects, 'tokens': Token.objects.all(),
}) })
return context return context
@ -105,10 +110,42 @@ class TokenDeleteView(DashboardView, DeleteView):
return redirect('api:tokens') 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): def form_valid(self, form):
Token.objects.create(token=uuid4(), owner=self.request.user) 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> </li>
</ul> </ul>
</li> </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> </ul>
</div> </div>
</nav> </nav>

View file

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