Add API key management functionality with views, forms, and templates

This commit is contained in:
Eduardo Silva
2026-02-09 21:59:16 -03:00
parent 533fed2bec
commit a7985ba065
8 changed files with 383 additions and 8 deletions

View File

@@ -0,0 +1,41 @@
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% load i18n %}
{% block content %}
<div class='row'>
<div class='{% if form_size %}{{ form_size }}{% else %}col-lg-6{% endif %}'>
<div class="card card-primary card-outline">
{% if page_title %}
<div class="card-header">
<h3 class="card-title">{{ page_title }}</h3>
</div>
{% endif %}
<div class="card-body row">
<div class="col-lg-12">
{% csrf_token %}
{% crispy form %}
</div>
</div>
</div>
</div>
{% if form_description %}
<div class='{% if form_description.size %}{{ form_description.size }}{% else %}col-lg-6{% endif %}'>
<div class="card card-primary card-outline">
<div class="card-body row">
<div class="col-lg-12">
{{ form_description.content|safe }}
</div>
</div>
</div>
</div>
{% endif %}
</div>
{% endblock %}
{% block custom_page_scripts %}
{% endblock %}

101
templates/api_v2/list.html Normal file
View File

@@ -0,0 +1,101 @@
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>{% trans 'Name' %}</th>
<th>{% trans 'Token' %}</th>
<th>{% trans 'Allowed Instances' %}</th>
<th class="text-center">{% trans 'Enabled' %}</th>
<th class="text-center"><i class="fas fa-power-off" title="{% trans 'Allow Restart' %}"></i></th>
<th class="text-center"><i class="fas fa-sync" title="{% trans 'Allow Reload' %}"></i></th>
<th class="text-center"><i class="fas fa-download" title="{% trans 'Allow Export' %}"></i></th>
<th class="text-center"><i class="far fa-edit"></i></th>
</tr>
</thead>
<tbody>
{% for key in api_keys %}
<tr>
<td>{{ key.name }}</td>
<td>
<div class="input-group input-group-sm" style="max-width: 300px;">
<input type="password" class="form-control token-input" value="{{ key.token }}" readonly>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="button" onclick="toggleListToken(this)">
<i class="fas fa-eye"></i>
</button>
</div>
</div>
</td>
<td>
{% for instance in key.allowed_instances.all %}
<span class="badge badge-info">{{ instance }}</span>
{% empty %}
<span class="badge badge-success">{% trans 'All' %}</span>
{% endfor %}
</td>
<td class="text-center">
{% if key.enabled %}
<i class="fas fa-check text-success" title="{% trans 'Enabled' %}"></i>
{% else %}
<i class="fas fa-times text-danger" title="{% trans 'Disabled' %}"></i>
{% endif %}
</td>
<td class="text-center">
{% if key.allow_restart %}
<i class="fas fa-check text-success"></i>
{% else %}
<i class="fas fa-times text-danger"></i>
{% endif %}
</td>
<td class="text-center">
{% if key.allow_reload %}
<i class="fas fa-check text-success"></i>
{% else %}
<i class="fas fa-times text-danger"></i>
{% endif %}
</td>
<td class="text-center">
{% if key.allow_export %}
<i class="fas fa-check text-success"></i>
{% else %}
<i class="fas fa-times text-danger"></i>
{% endif %}
</td>
<td class="text-center" style="width: 1%; white-space: nowrap;">
<a href="/manage_api/v2/manage/?uuid={{ key.uuid }}" title="{% trans 'Edit' %}">
<i class="far fa-edit"></i>
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="row">
<div class="col-md-12">
<a href="/manage_api/v2/manage/" class="btn btn-primary">
<i class="fas fa-plus"></i> {% trans 'Add API Key' %}
</a>
</div>
</div>
<script>
function toggleListToken(btn) {
var input = btn.closest('.input-group').querySelector('input');
var icon = btn.querySelector('i');
if (input.type === 'password') {
input.type = 'text';
icon.classList.remove('fa-eye');
icon.classList.add('fa-eye-slash');
} else {
input.type = 'password';
icon.classList.remove('fa-eye-slash');
icon.classList.add('fa-eye');
}
}
</script>
{% endblock %}

View File

@@ -100,6 +100,16 @@
</a>
</li>
<li class="nav-item">
<a href="/manage_api/v2/list/"
class="nav-link {% if '/manage_api/' in request.path %}active{% endif %}">
<i class="fas fa-key nav-icon"></i>
<p>
{% trans 'API Keys' %}
</p>
</a>
</li>
<li class="nav-item">
<a href="/cluster/" class="nav-link {% if '/cluster/' in request.path %}active{% endif %}">
<i class="fas fa-server nav-icon"></i>