feat(index): Add parametrization to links

This commit is contained in:
Tom Hubrecht 2024-09-24 11:31:54 +02:00
parent b9f165c1e6
commit 2490d83459
Signed by: thubrecht
SSH key fingerprint: SHA256:r+nK/SIcWlJ0zFZJGHtlAoRwq1Rm+WcKAm5ADYMoQPc
4 changed files with 62 additions and 17 deletions

View file

@ -0,0 +1,4 @@
<a class="button bt-link {{ link.color }}" href="{% url link.reverse %}">
{% if link.icon %}<span class="icon"><i class="ti ti-{{ link.icon }}"></i></span>{% endif %}
<span>{{ link.text }}</span>
</a>

View file

@ -0,0 +1,20 @@
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<section class="section">
{% if user.is_authenticated %}
{% for link in links.authenticated %}
{% include "_index_link.html" %}
{% endfor %}
{% endif %}
{% if user.is_admin %}
<hr>
{% for link in links.admin %}
{% include "_index_link.html" %}
{% endfor %}
{% endif %}
</section>
{% endblock content %}

View file

@ -1,4 +1,4 @@
from typing import Any from typing import Any, NamedTuple
from asgiref.sync import async_to_sync from asgiref.sync import async_to_sync
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
@ -6,6 +6,7 @@ from django.contrib.messages.views import SuccessMessageMixin
from django.core.mail import EmailMessage from django.core.mail import EmailMessage
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.utils.functional import Promise
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.views.generic import FormView, ListView, RedirectView, TemplateView from django.views.generic import FormView, ListView, RedirectView, TemplateView
from django.views.generic.detail import SingleObjectMixin from django.views.generic.detail import SingleObjectMixin
@ -16,9 +17,45 @@ from dgsi.models import Service, User
from shared.kanidm import client from shared.kanidm import client
class Link(NamedTuple):
color: str
reverse: str
text: str | Promise
icon: str | None = None
class IndexView(TemplateView): class IndexView(TemplateView):
template_name = "dgsi/index.html" template_name = "dgsi/index.html"
def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
return super().get_context_data(
links={
"authenticated": [
Link(
"is-primary",
"dgsi:dgn-profile",
_("Mon profil"),
"user-filled",
),
],
"admin": [
Link(
"is-danger",
"dgsi:dgn-create_user",
_("Créer un nouveau compte Kanidm"),
"user-plus",
),
Link(
"is-warning",
"admin:index",
_("Interface d'administration"),
"settings-filled",
),
],
},
**kwargs,
)
class ProfileView(LoginRequiredMixin, TemplateView): class ProfileView(LoginRequiredMixin, TemplateView):
template_name = "dgsi/profile.html" template_name = "dgsi/profile.html"

View file

@ -1,16 +0,0 @@
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<section class="section">
{% if user.is_authenticated %}
<a class="button bt-link is-primary" href="{% url 'dgsi:dgn-profile' %}">{% trans "Mon profil" %}</a>
{% endif %}
{% if user.is_admin %}
<a class="button bt-link is-danger"
href="{% url 'dgsi:dgn-create_user' %}">{% trans "Créer un nouveau compte Kanidm" %}</a>
<a class="button bt-link is-warning" href="{% url 'admin:index' %}">{% trans "Interface admin" %}</a>
{% endif %}
</section>
{% endblock content %}