2018-12-28 17:47:35 +00:00
|
|
|
import logging
|
|
|
|
|
2018-12-30 10:43:14 +00:00
|
|
|
from django.conf import settings
|
2018-12-21 10:28:06 +00:00
|
|
|
from django.contrib.auth import authenticate, login
|
|
|
|
from django.contrib.auth.models import User
|
2018-12-28 17:47:35 +00:00
|
|
|
from django.http import HttpResponseForbidden
|
|
|
|
from django.shortcuts import redirect
|
2018-12-01 15:28:37 +00:00
|
|
|
from django.urls import reverse_lazy
|
2018-12-28 17:47:35 +00:00
|
|
|
from django.views.generic import FormView
|
2018-12-01 15:28:37 +00:00
|
|
|
|
2019-12-18 22:27:06 +00:00
|
|
|
from YtManagerApp.services.scheduler.jobs.synchronize_job import SynchronizeJob
|
2019-12-16 20:19:50 +00:00
|
|
|
from YtManagerApp.services import Services
|
2019-01-02 21:11:39 +00:00
|
|
|
from YtManagerApp.views.forms.first_time import WelcomeForm, ApiKeyForm, PickAdminUserForm, ServerConfigForm, DoneForm, \
|
|
|
|
UserCreationForm, LoginForm
|
2018-12-21 10:28:06 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger("FirstTimeWizard")
|
|
|
|
|
2018-12-10 21:23:00 +00:00
|
|
|
|
2018-12-29 21:16:04 +00:00
|
|
|
class WizardStepMixin:
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2018-12-10 21:23:00 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
2018-12-28 17:47:35 +00:00
|
|
|
|
|
|
|
# Prevent access if application is already initialized
|
2019-12-18 22:27:06 +00:00
|
|
|
if Services.appConfig().initialized:
|
2018-12-28 17:47:35 +00:00
|
|
|
logger.debug(f"Attempted to access {request.path}, but first time setup already run. Redirected to home "
|
|
|
|
f"page.")
|
2018-12-10 21:23:00 +00:00
|
|
|
return redirect('home')
|
2018-12-28 17:47:35 +00:00
|
|
|
|
2018-12-10 21:23:00 +00:00
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
2019-12-18 22:27:06 +00:00
|
|
|
if Services.appConfig().initialized:
|
2018-12-21 10:28:06 +00:00
|
|
|
logger.debug(f"Attempted to post {request.path}, but first time setup already run.")
|
2018-12-10 21:23:00 +00:00
|
|
|
return HttpResponseForbidden()
|
|
|
|
return super().post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Step 0: welcome screen
|
|
|
|
#
|
2018-12-28 17:47:35 +00:00
|
|
|
class Step0WelcomeView(WizardStepMixin, FormView):
|
2018-12-10 21:23:00 +00:00
|
|
|
template_name = 'YtManagerApp/first_time_setup/step0_welcome.html'
|
2018-12-28 17:47:35 +00:00
|
|
|
form_class = WelcomeForm
|
2018-12-10 21:23:00 +00:00
|
|
|
success_url = reverse_lazy('first_time_1')
|
|
|
|
|
2018-12-30 10:43:14 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context.update({
|
|
|
|
'config_errors': settings.CONFIG_ERRORS,
|
|
|
|
'config_warnings': settings.CONFIG_WARNINGS,
|
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
2018-12-10 21:23:00 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Step 1: setup API key
|
|
|
|
#
|
2018-12-28 17:47:35 +00:00
|
|
|
class Step1ApiKeyView(WizardStepMixin, FormView):
|
2018-12-10 21:23:00 +00:00
|
|
|
template_name = 'YtManagerApp/first_time_setup/step1_apikey.html'
|
2018-12-28 17:47:35 +00:00
|
|
|
form_class = ApiKeyForm
|
2018-12-10 21:23:00 +00:00
|
|
|
success_url = reverse_lazy('first_time_2')
|
|
|
|
|
2018-12-28 17:47:35 +00:00
|
|
|
def get_initial(self):
|
|
|
|
initial = super().get_initial()
|
2019-12-18 22:27:06 +00:00
|
|
|
initial['api_key'] = Services.appConfig().youtube_api_key
|
2018-12-28 17:47:35 +00:00
|
|
|
return initial
|
|
|
|
|
2018-12-10 21:23:00 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
key = form.cleaned_data['api_key']
|
|
|
|
# TODO: validate key
|
|
|
|
if key is not None and len(key) > 0:
|
2019-12-18 22:27:06 +00:00
|
|
|
Services.appConfig().youtube_api_key = key
|
2018-12-10 21:23:00 +00:00
|
|
|
|
2019-03-31 20:15:23 +00:00
|
|
|
return super().form_valid(form)
|
|
|
|
|
2018-12-10 21:23:00 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Step 2: create admin user
|
|
|
|
#
|
2018-12-28 17:47:35 +00:00
|
|
|
class Step2SetupAdminUserView(WizardStepMixin, FormView):
|
2018-12-21 10:28:06 +00:00
|
|
|
template_name = 'YtManagerApp/first_time_setup/step2_admin.html'
|
|
|
|
success_url = reverse_lazy('first_time_3')
|
2018-12-28 17:47:35 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_form_class(self):
|
|
|
|
have_users = User.objects.count() > 0
|
|
|
|
have_admin = User.objects.filter(is_superuser=True).count() > 0
|
|
|
|
|
2018-12-21 10:28:06 +00:00
|
|
|
# Check if an admin user already exists
|
2019-01-02 21:11:39 +00:00
|
|
|
if have_admin:
|
2018-12-28 17:47:35 +00:00
|
|
|
logger.debug("Admin user already exists and is not logged in!")
|
2019-01-02 21:11:39 +00:00
|
|
|
return LoginForm
|
2018-12-28 17:47:35 +00:00
|
|
|
|
2019-01-02 21:11:39 +00:00
|
|
|
elif have_users and 'register' not in self.kwargs:
|
2018-12-28 17:47:35 +00:00
|
|
|
logger.debug("There are users but no admin!")
|
2019-01-02 21:11:39 +00:00
|
|
|
return PickAdminUserForm
|
2018-12-28 17:47:35 +00:00
|
|
|
|
2019-01-02 21:11:39 +00:00
|
|
|
logger.debug("No admin user exists, will register a new account!")
|
|
|
|
return UserCreationForm
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
|
|
|
# Skip if admin is already logged in
|
|
|
|
if request.user.is_authenticated and request.user.is_superuser:
|
|
|
|
logger.debug("Admin user already exists and is logged in!")
|
|
|
|
return redirect(self.success_url)
|
2018-12-21 10:28:06 +00:00
|
|
|
|
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2019-01-02 21:11:39 +00:00
|
|
|
if isinstance(form, LoginForm):
|
|
|
|
form.apply_session_expiry(self.request)
|
2018-12-28 17:47:35 +00:00
|
|
|
login(self.request, form.get_user())
|
2018-12-21 10:28:06 +00:00
|
|
|
|
2018-12-28 17:47:35 +00:00
|
|
|
elif isinstance(form, UserCreationForm):
|
|
|
|
user = form.save()
|
|
|
|
user.is_staff = True
|
|
|
|
user.is_superuser = True
|
|
|
|
user.save()
|
2018-12-21 10:28:06 +00:00
|
|
|
|
2018-12-28 17:47:35 +00:00
|
|
|
username = form.cleaned_data.get('username')
|
|
|
|
password = form.cleaned_data.get('password1')
|
|
|
|
user = authenticate(username=username, password=password)
|
|
|
|
login(self.request, user)
|
2018-12-21 10:28:06 +00:00
|
|
|
|
2018-12-28 17:47:35 +00:00
|
|
|
elif isinstance(form, PickAdminUserForm):
|
|
|
|
user = form.cleaned_data['admin_user']
|
|
|
|
user.is_staff = True
|
|
|
|
user.is_superuser = True
|
|
|
|
user.save()
|
2018-12-21 10:28:06 +00:00
|
|
|
|
2018-12-28 17:47:35 +00:00
|
|
|
return redirect('first_time_2', assigned_success='1')
|
2018-12-21 10:28:06 +00:00
|
|
|
|
|
|
|
return super().form_valid(form)
|
2018-12-10 21:23:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Step 3: configure server
|
|
|
|
#
|
2018-12-28 17:47:35 +00:00
|
|
|
class Step3ConfigureView(WizardStepMixin, FormView):
|
2018-12-10 21:23:00 +00:00
|
|
|
template_name = 'YtManagerApp/first_time_setup/step3_configure.html'
|
2018-12-28 17:47:35 +00:00
|
|
|
form_class = ServerConfigForm
|
2018-12-10 21:23:00 +00:00
|
|
|
success_url = reverse_lazy('first_time_done')
|
|
|
|
|
2018-12-28 17:47:35 +00:00
|
|
|
def get_initial(self):
|
|
|
|
initial = super().get_initial()
|
2019-12-18 22:27:06 +00:00
|
|
|
initial['allow_registrations'] = Services.appConfig().allow_registrations
|
|
|
|
initial['sync_schedule'] = Services.appConfig().sync_schedule
|
2018-12-29 18:43:39 +00:00
|
|
|
initial['auto_download'] = self.request.user.preferences['auto_download']
|
|
|
|
initial['download_location'] = self.request.user.preferences['download_path']
|
2018-12-28 17:47:35 +00:00
|
|
|
return initial
|
|
|
|
|
2018-12-10 21:23:00 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
allow_registrations = form.cleaned_data['allow_registrations']
|
|
|
|
if allow_registrations is not None:
|
2019-12-18 22:27:06 +00:00
|
|
|
Services.appConfig().allow_registrations = allow_registrations
|
2018-12-10 21:23:00 +00:00
|
|
|
|
|
|
|
sync_schedule = form.cleaned_data['sync_schedule']
|
|
|
|
if sync_schedule is not None and len(sync_schedule) > 0:
|
2019-12-18 22:27:06 +00:00
|
|
|
Services.appConfig().sync_schedule = sync_schedule
|
2018-12-01 15:28:37 +00:00
|
|
|
|
2018-12-10 21:23:00 +00:00
|
|
|
auto_download = form.cleaned_data['auto_download']
|
|
|
|
if auto_download is not None:
|
2018-12-29 18:43:39 +00:00
|
|
|
self.request.user.preferences['auto_download'] = auto_download
|
2018-12-10 21:23:00 +00:00
|
|
|
|
|
|
|
download_location = form.cleaned_data['download_location']
|
|
|
|
if download_location is not None and len(download_location) > 0:
|
2018-12-29 18:43:39 +00:00
|
|
|
self.request.user.preferences['download_path'] = download_location
|
2018-12-10 21:23:00 +00:00
|
|
|
|
|
|
|
# Set initialized to true
|
2019-12-18 22:27:06 +00:00
|
|
|
Services.appConfig().initialized = True
|
2018-12-31 11:53:25 +00:00
|
|
|
|
|
|
|
# Start scheduler if not started
|
2019-12-16 20:19:50 +00:00
|
|
|
Services.scheduler.initialize()
|
2019-08-14 14:14:16 +00:00
|
|
|
SynchronizeJob.schedule_global_job()
|
2018-12-31 11:53:25 +00:00
|
|
|
|
2018-12-10 21:23:00 +00:00
|
|
|
return super().form_valid(form)
|
|
|
|
|
2018-12-28 17:47:35 +00:00
|
|
|
|
2018-12-10 21:23:00 +00:00
|
|
|
#
|
|
|
|
# Done screen
|
|
|
|
#
|
|
|
|
class DoneView(FormView):
|
|
|
|
template_name = 'YtManagerApp/first_time_setup/done.html'
|
|
|
|
form_class = DoneForm
|
|
|
|
success_url = reverse_lazy('home')
|