Implemented first time setup wizard. There are still some problems to be solved.

This commit is contained in:
2018-12-10 23:23:00 +02:00
parent 34e358a9c4
commit 899de7adf5
22 changed files with 283 additions and 49 deletions

View File

@ -3,49 +3,165 @@ from crispy_forms.layout import Layout, HTML, Submit
from django import forms
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.views.generic import UpdateView
from django.views.generic import UpdateView, FormView
from django.shortcuts import render, redirect
from YtManagerApp.views.auth import RegisterView
from YtManagerApp.models import UserSettings
from YtManagerApp.management.appconfig import global_prefs
from django.http import HttpResponseForbidden
class SettingsForm(forms.ModelForm):
class Meta:
model = UserSettings
exclude = ['user']
class ProtectInitializedMixin(object):
def get(self, request, *args, **kwargs):
if global_prefs['hidden__initialized']:
return redirect('home')
return super().get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
if global_prefs['hidden__initialized']:
return HttpResponseForbidden()
return super().post(request, *args, **kwargs)
#
# Step 0: welcome screen
#
class Step0WelcomeForm(forms.Form):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
Submit('submit', value='Continue')
)
class Step0WelcomeView(ProtectInitializedMixin, FormView):
template_name = 'YtManagerApp/first_time_setup/step0_welcome.html'
form_class = Step0WelcomeForm
success_url = reverse_lazy('first_time_1')
#
# Step 1: setup API key
#
class Step1ApiKeyForm(forms.Form):
api_key = forms.CharField(label="YouTube API Key:")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-lg-3'
self.helper.field_class = 'col-lg-9'
self.helper.layout = Layout(
'mark_deleted_as_watched',
'delete_watched',
HTML('<h2>Download settings</h2>'),
'auto_download',
'download_path',
'download_file_pattern',
'download_format',
'download_order',
'download_global_limit',
'download_subscription_limit',
HTML('<h2>Subtitles download settings</h2>'),
'download_subtitles',
'download_subtitles_langs',
'download_subtitles_all',
'download_autogenerated_subtitles',
'download_subtitles_format',
Submit('submit', value='Save')
'api_key',
Submit('submit', value='Continue'),
)
class SettingsView(LoginRequiredMixin, UpdateView):
form_class = SettingsForm
model = UserSettings
template_name = 'YtManagerApp/settings.html'
success_url = reverse_lazy('home')
class Step1ApiKeyView(ProtectInitializedMixin, FormView):
template_name = 'YtManagerApp/first_time_setup/step1_apikey.html'
form_class = Step1ApiKeyForm
success_url = reverse_lazy('first_time_2')
def get_object(self, queryset=None):
obj, _ = self.model.objects.get_or_create(user=self.request.user)
return obj
def form_valid(self, form):
key = form.cleaned_data['api_key']
# TODO: validate key
if key is not None and len(key) > 0:
global_prefs['general__youtube_api_key'] = key
#
# Step 2: create admin user
#
class Step2CreateAdminUserView(ProtectInitializedMixin, RegisterView):
template_name = 'YtManagerApp/first_time_setup/step2_admin.html'
success_url = reverse_lazy('first_time_3')
#
# Step 3: configure server
#
class Step3ConfigureForm(forms.Form):
allow_registrations = forms.BooleanField(
label="Allow user registrations",
help_text="Disabling this option will prevent anyone from registering to the site.",
initial=True,
required=False
)
sync_schedule = forms.CharField(
label="Synchronization schedule",
help_text="How often should the application look for new videos.",
initial="5 * * * *",
required=True
)
auto_download = forms.BooleanField(
label="Download videos automatically",
required=False
)
download_location = forms.CharField(
label="Download location",
help_text="Location on the server where videos are downloaded.",
required=True
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
HTML('<h3>Server settings</h3>'),
'sync_schedule',
'allow_registrations',
HTML('<h3>User settings</h3>'),
'auto_download',
'download_location',
Submit('submit', value='Continue'),
)
class Step3ConfigureView(ProtectInitializedMixin, FormView):
template_name = 'YtManagerApp/first_time_setup/step3_configure.html'
form_class = Step3ConfigureForm
success_url = reverse_lazy('first_time_done')
def form_valid(self, form):
allow_registrations = form.cleaned_data['allow_registrations']
if allow_registrations is not None:
global_prefs['general__allow_registrations'] = allow_registrations
sync_schedule = form.cleaned_data['sync_schedule']
if sync_schedule is not None and len(sync_schedule) > 0:
global_prefs['scheduler__synchronization_schedule'] = sync_schedule
auto_download = form.cleaned_data['auto_download']
if auto_download is not None:
self.request.user.preferences['downloader__auto_enabled'] = auto_download
download_location = form.cleaned_data['download_location']
if download_location is not None and len(download_location) > 0:
self.request.user.preferences['downloader__download_path'] = download_location
# Set initialized to true
global_prefs['hidden__initialized'] = True
return super().form_valid(form)
#
# Done screen
#
class DoneForm(forms.Form):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
Submit('submit', value='Finish')
)
class DoneView(FormView):
template_name = 'YtManagerApp/first_time_setup/done.html'
form_class = DoneForm
success_url = reverse_lazy('home')

View File

@ -5,11 +5,12 @@ from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import Q
from django.http import HttpRequest, HttpResponseBadRequest, JsonResponse
from django.shortcuts import render
from django.shortcuts import render, redirect
from django.views.generic import CreateView, UpdateView, DeleteView, FormView
from django.views.generic.edit import FormMixin
from django.conf import settings
from YtManagerApp.management.videos import get_videos
from YtManagerApp.management.appconfig import global_prefs
from YtManagerApp.models import Subscription, SubscriptionFolder, VIDEO_ORDER_CHOICES, VIDEO_ORDER_MAPPING
from YtManagerApp.utils import youtube, subscription_file_parser
from YtManagerApp.views.controls.modal import ModalMixin
@ -94,6 +95,10 @@ def __tree_sub_id(sub_id):
def index(request: HttpRequest):
if not global_prefs['hidden__initialized']:
return redirect('first_time_0')
context = {
'config_errors': settings.CONFIG_ERRORS,
'config_warnings': settings.CONFIG_WARNINGS,