ytsm/app/YtManagerApp/views/settings.py

50 lines
1.6 KiB
Python
Raw Normal View History

from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponseForbidden
2018-10-26 15:41:52 +00:00
from django.urls import reverse_lazy
2018-12-29 18:43:39 +00:00
from django.views.generic import FormView
2018-10-26 15:41:52 +00:00
2020-04-10 21:30:24 +00:00
from YtManagerApp.services import Services
from YtManagerApp.views.forms.settings import SettingsForm, AdminSettingsForm
2018-10-26 15:41:52 +00:00
2018-12-29 18:43:39 +00:00
class SettingsView(LoginRequiredMixin, FormView):
2018-10-26 15:41:52 +00:00
form_class = SettingsForm
template_name = 'YtManagerApp/settings.html'
success_url = reverse_lazy('home')
2018-12-29 18:43:39 +00:00
def get_initial(self):
initial = super().get_initial()
initial.update(SettingsForm.get_initials(self.request.user))
return initial
def form_valid(self, form):
form.save(self.request.user)
return super().form_valid(form)
class AdminSettingsView(LoginRequiredMixin, FormView):
form_class = AdminSettingsForm
template_name = 'YtManagerApp/settings_admin.html'
success_url = reverse_lazy('home')
def post(self, request, *args, **kwargs):
if not request.user.is_authenticated or not request.user.is_superuser:
return HttpResponseForbidden()
return super().post(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# TODO: present stats
return context
def get_initial(self):
initial = super().get_initial()
2018-12-29 18:43:39 +00:00
initial.update(AdminSettingsForm.get_initials())
return initial
def form_valid(self, form):
2018-12-29 18:43:39 +00:00
form.save()
2020-04-10 21:30:24 +00:00
Services.subscriptionManager().schedule_global_synchronize_job()
return super().form_valid(form)