devicehub-django/user/views.py

51 lines
1.7 KiB
Python
Raw Normal View History

2024-10-11 11:46:21 +00:00
from decouple import config
from django.urls import reverse
from django.http import HttpResponse
from django.shortcuts import render
2024-10-10 08:35:27 +00:00
from django.utils.translation import gettext_lazy as _
from django.views.generic.base import TemplateView
from dashboard.mixins import DashboardView
from django.views.generic.edit import (
FormView,
)
from user.forms import SettingsForm
from api.models import Token
2024-06-12 07:32:49 +00:00
2024-10-10 08:35:27 +00:00
class PanelView(DashboardView, TemplateView):
template_name = "panel.html"
title = _("User")
breadcrumb = "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):
2024-10-11 11:46:21 +00:00
cleaned_data = form.cleaned_data.copy()
settings_tmpl = "settings.ini"
path = reverse("api:new_snapshot")
cleaned_data['url'] = self.request.build_absolute_uri(path)
if config("LEGACY", False):
cleaned_data['token'] = config.get('TOKEN_LEGACY', '')
cleaned_data['url'] = config.get('URL_LEGACY', '')
settings_tmpl = "settings_legacy.ini"
data = render(self.request, settings_tmpl, 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