add settings page for download settings file

This commit is contained in:
Cayo Puigdefabregas 2024-10-11 13:04:37 +02:00
parent 3e967c5a1a
commit e0929c03c7
6 changed files with 95 additions and 3 deletions

20
user/forms.py Normal file
View File

@ -0,0 +1,20 @@
from django import forms
class SettingsForm(forms.Form):
token = forms.ChoiceField(
choices = []
)
erasure = forms.ChoiceField(
choices = [(0, 'Not erasure'),
('erasure1', 'Erasure easy'),
('erasure2', 'Erasure mediom'),
('erasure3', 'Erasure hard'),
],
)
def __init__(self, *args, **kwargs):
tokens = kwargs.pop('tokens')
super().__init__(*args, **kwargs)
tk = [(str(x.token), x.tag) for x in tokens]
self.fields['token'].choices = tk

View File

@ -12,9 +12,16 @@
</div> </div>
<div class="row"> <div class="row">
<a class="nav-link fw-bold" href="{% url 'api:tokens' %}"> <div class="col">
{% trans 'Tokens' %} <a class="nav-link fw-bold" href="{% url 'api:tokens' %}">
</a> {% translate 'Admin your Tokens' %}
</a>
</div>
<div class="col">
<a class="nav-link fw-bold" href="{% url 'user:settings' %}">
{% translate 'Download a settings file' %}
</a>
</div>
</div> </div>
{% endblock %} {% endblock %}

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 'user:panel' %}">{% translate "Cancel" %}</a>
<input class="btn btn-green-admin" type="submit" name="submit" value="{% translate 'Save' %}" />
</div>
</form>
{% endblock %}

View File

@ -0,0 +1,3 @@
token = {{ token }}
erasure = {{ erasure }}
legacy = False

View File

@ -5,4 +5,5 @@ app_name = 'user'
urlpatterns = [ urlpatterns = [
path("panel/", views.PanelView.as_view(), name="panel"), path("panel/", views.PanelView.as_view(), name="panel"),
path("settings/", views.SettingsView.as_view(), name="settings"),
] ]

View File

@ -1,6 +1,14 @@
from django.http import HttpResponse
from django.shortcuts import render
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.views.generic.base import TemplateView from django.views.generic.base import TemplateView
from dashboard.mixins import DashboardView from dashboard.mixins import DashboardView
from django.views.generic.edit import (
FormView,
)
from user.forms import SettingsForm
from api.models import Token
class PanelView(DashboardView, TemplateView): class PanelView(DashboardView, TemplateView):
@ -8,3 +16,24 @@ class PanelView(DashboardView, TemplateView):
title = _("User") title = _("User")
breadcrumb = "User / Panel" breadcrumb = "User / Panel"
subtitle = "User panel" subtitle = "User panel"
class SettingsView(DashboardView, FormView):
template_name = "settings.html"
title = _("Download Settings")
breadcrumb = "user / workbench / settings"
form_class = SettingsForm
def form_valid(self, form):
form.devices = self.get_session_devices()
data = render(self.request, "settings.ini", form.cleaned_data)
response = HttpResponse(data.content, content_type="application/text")
response['Content-Disposition'] = 'attachment; filename={}'.format("settings.ini")
return response
def get_form_kwargs(self):
tokens = Token.objects.filter(owner=self.request.user)
kwargs = super().get_form_kwargs()
kwargs['tokens'] = tokens
return kwargs