2018-10-27 00:33:45 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2018-12-29 15:11:20 +00:00
|
|
|
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
|
|
|
|
2019-12-18 22:27:06 +00:00
|
|
|
from YtManagerApp.management.scheduler.jobs.synchronize_job import SynchronizeJob
|
2018-12-29 15:11:20 +00:00
|
|
|
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)
|
2018-12-29 15:11:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
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())
|
2018-12-29 15:11:20 +00:00
|
|
|
return initial
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2018-12-29 18:43:39 +00:00
|
|
|
form.save()
|
2019-08-14 14:14:16 +00:00
|
|
|
SynchronizeJob.schedule_global_job()
|
2018-12-29 15:11:20 +00:00
|
|
|
return super().form_valid(form)
|