user-panel #9

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

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