implement post_process_updated for settings, import cookie

This commit is contained in:
simon 2022-04-30 18:35:04 +07:00
parent 487c81d50b
commit 3a21690309
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
2 changed files with 20 additions and 3 deletions

View File

@ -83,6 +83,7 @@ class AppConfig:
def update_config(self, form_post): def update_config(self, form_post):
"""update config values from settings form""" """update config values from settings form"""
updated = []
for key, value in form_post.items(): for key, value in form_post.items():
if not value and not isinstance(value, int): if not value and not isinstance(value, int):
continue continue
@ -96,8 +97,10 @@ class AppConfig:
config_dict, config_value = key.split("_", maxsplit=1) config_dict, config_value = key.split("_", maxsplit=1)
self.config[config_dict][config_value] = to_write self.config[config_dict][config_value] = to_write
updated.append((config_value, to_write))
RedisArchivist().set_message("config", self.config, expire=False) RedisArchivist().set_message("config", self.config, expire=False)
return updated
@staticmethod @staticmethod
def set_user_config(form_post, user_id): def set_user_config(form_post, user_id):

View File

@ -14,6 +14,7 @@ from django.contrib.auth.forms import AuthenticationForm
from django.http import JsonResponse from django.http import JsonResponse
from django.shortcuts import redirect, render from django.shortcuts import redirect, render
from django.views import View from django.views import View
from home.src.download.yt_cookie import CookieHandler
from home.src.es.index_setup import get_available_backups from home.src.es.index_setup import get_available_backups
from home.src.frontend.api_calls import PostData from home.src.frontend.api_calls import PostData
from home.src.frontend.forms import ( from home.src.frontend.forms import (
@ -791,8 +792,7 @@ class SettingsView(View):
token = Token.objects.get_or_create(user=request.user)[0] token = Token.objects.get_or_create(user=request.user)[0]
return token return token
@staticmethod def post(self, request):
def post(request):
"""handle form post to update settings""" """handle form post to update settings"""
user_form = UserSettingsForm(request.POST) user_form = UserSettingsForm(request.POST)
if user_form.is_valid(): if user_form.is_valid():
@ -805,7 +805,8 @@ class SettingsView(View):
app_form_post = app_form.cleaned_data app_form_post = app_form.cleaned_data
if app_form_post: if app_form_post:
print(app_form_post) print(app_form_post)
AppConfig().update_config(app_form_post) updated = AppConfig().update_config(app_form_post)
self.post_process_updated(updated)
scheduler_form = SchedulerSettingsForm(request.POST) scheduler_form = SchedulerSettingsForm(request.POST)
if scheduler_form.is_valid(): if scheduler_form.is_valid():
@ -817,6 +818,19 @@ class SettingsView(View):
sleep(1) sleep(1)
return redirect("settings", permanent=True) return redirect("settings", permanent=True)
@staticmethod
def post_process_updated(updated):
"""apply changes for config"""
if not updated:
return
for config_value, updated_value in updated:
if config_value == "cookie_import":
if updated_value:
CookieHandler().import_cookie()
else:
CookieHandler().revoke()
def progress(request): def progress(request):
# pylint: disable=unused-argument # pylint: disable=unused-argument