mirror of
https://github.com/tubearchivist/tubearchivist-frontend.git
synced 2024-11-24 20:50:14 +00:00
use cleaned_data for config form parser
This commit is contained in:
parent
39a6fbea74
commit
b1a7a6a148
@ -83,31 +83,30 @@ 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"""
|
||||||
config = self.config
|
|
||||||
for key, value in form_post.items():
|
for key, value in form_post.items():
|
||||||
to_write = value[0]
|
if not value and not isinstance(value, int):
|
||||||
if len(to_write):
|
continue
|
||||||
if to_write == "0":
|
|
||||||
|
if value in ["0", 0]:
|
||||||
to_write = False
|
to_write = False
|
||||||
elif to_write == "1":
|
elif value == "1":
|
||||||
to_write = True
|
to_write = True
|
||||||
elif to_write.isdigit():
|
else:
|
||||||
to_write = int(to_write)
|
to_write = value
|
||||||
|
|
||||||
config_dict, config_value = key.split("_", maxsplit=1)
|
config_dict, config_value = key.split("_", maxsplit=1)
|
||||||
config[config_dict][config_value] = to_write
|
self.config[config_dict][config_value] = to_write
|
||||||
|
|
||||||
RedisArchivist().set_message("config", config, expire=False)
|
RedisArchivist().set_message("config", self.config, expire=False)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def set_user_config(form_post, user_id):
|
def set_user_config(form_post, user_id):
|
||||||
"""set values in redis for user settings"""
|
"""set values in redis for user settings"""
|
||||||
for key, value in form_post.items():
|
for key, value in form_post.items():
|
||||||
to_write = value[0]
|
if not value:
|
||||||
if len(to_write):
|
continue
|
||||||
if to_write.isdigit():
|
|
||||||
to_write = int(to_write)
|
message = {"status": value}
|
||||||
message = {"status": to_write}
|
|
||||||
redis_key = f"{user_id}:{key}"
|
redis_key = f"{user_id}:{key}"
|
||||||
RedisArchivist().set_message(redis_key, message, expire=False)
|
RedisArchivist().set_message(redis_key, message, expire=False)
|
||||||
|
|
||||||
@ -172,12 +171,11 @@ class ScheduleBuilder:
|
|||||||
print("processing form, restart container for changes to take effect")
|
print("processing form, restart container for changes to take effect")
|
||||||
redis_config = self.config
|
redis_config = self.config
|
||||||
for key, value in form_post.items():
|
for key, value in form_post.items():
|
||||||
to_check = value[0]
|
if key in self.SCHEDULES and value:
|
||||||
if key in self.SCHEDULES and to_check:
|
|
||||||
try:
|
try:
|
||||||
to_write = self.value_builder(key, to_check)
|
to_write = self.value_builder(key, value)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print(f"failed: {key} {to_check}")
|
print(f"failed: {key} {value}")
|
||||||
mess_dict = {
|
mess_dict = {
|
||||||
"status": "message:setting",
|
"status": "message:setting",
|
||||||
"level": "error",
|
"level": "error",
|
||||||
@ -188,8 +186,8 @@ class ScheduleBuilder:
|
|||||||
return
|
return
|
||||||
|
|
||||||
redis_config["scheduler"][key] = to_write
|
redis_config["scheduler"][key] = to_write
|
||||||
if key in self.CONFIG and to_check:
|
if key in self.CONFIG and value:
|
||||||
redis_config["scheduler"][key] = int(to_check)
|
redis_config["scheduler"][key] = int(value)
|
||||||
RedisArchivist().set_message("config", redis_config, expire=False)
|
RedisArchivist().set_message("config", redis_config, expire=False)
|
||||||
mess_dict = {
|
mess_dict = {
|
||||||
"status": "message:setting",
|
"status": "message:setting",
|
||||||
@ -199,26 +197,26 @@ class ScheduleBuilder:
|
|||||||
}
|
}
|
||||||
RedisArchivist().set_message("message:setting", mess_dict)
|
RedisArchivist().set_message("message:setting", mess_dict)
|
||||||
|
|
||||||
def value_builder(self, key, to_check):
|
def value_builder(self, key, value):
|
||||||
"""validate single cron form entry and return cron dict"""
|
"""validate single cron form entry and return cron dict"""
|
||||||
print(f"change schedule for {key} to {to_check}")
|
print(f"change schedule for {key} to {value}")
|
||||||
if to_check == "0":
|
if value == "0":
|
||||||
# deactivate this schedule
|
# deactivate this schedule
|
||||||
return False
|
return False
|
||||||
if re.search(r"[\d]{1,2}\/[\d]{1,2}", to_check):
|
if re.search(r"[\d]{1,2}\/[\d]{1,2}", value):
|
||||||
# number/number cron format will fail in celery
|
# number/number cron format will fail in celery
|
||||||
print("number/number schedule formatting not supported")
|
print("number/number schedule formatting not supported")
|
||||||
raise ValueError
|
raise ValueError
|
||||||
|
|
||||||
keys = ["minute", "hour", "day_of_week"]
|
keys = ["minute", "hour", "day_of_week"]
|
||||||
if to_check == "auto":
|
if value == "auto":
|
||||||
# set to sensible default
|
# set to sensible default
|
||||||
values = self.SCHEDULES[key].split()
|
values = self.SCHEDULES[key].split()
|
||||||
else:
|
else:
|
||||||
values = to_check.split()
|
values = value.split()
|
||||||
|
|
||||||
if len(keys) != len(values):
|
if len(keys) != len(values):
|
||||||
print(f"failed to parse {to_check} for {key}")
|
print(f"failed to parse {value} for {key}")
|
||||||
raise ValueError("invalid input")
|
raise ValueError("invalid input")
|
||||||
|
|
||||||
to_write = dict(zip(keys, values))
|
to_write = dict(zip(keys, values))
|
||||||
|
@ -8,7 +8,6 @@ import json
|
|||||||
import urllib.parse
|
import urllib.parse
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
from django import forms
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth import login
|
from django.contrib.auth import login
|
||||||
from django.contrib.auth.forms import AuthenticationForm
|
from django.contrib.auth.forms import AuthenticationForm
|
||||||
@ -802,23 +801,25 @@ class SettingsView(View):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def post(request):
|
def post(request):
|
||||||
"""handle form post to update settings"""
|
"""handle form post to update settings"""
|
||||||
|
user_form = UserSettingsForm(request.POST)
|
||||||
|
if user_form.is_valid():
|
||||||
|
user_form_post = user_form.cleaned_data
|
||||||
|
if any(user_form_post.values()):
|
||||||
|
AppConfig().set_user_config(user_form_post, request.user.id)
|
||||||
|
|
||||||
form_response = forms.Form(request.POST)
|
app_form = ApplicationSettingsForm(request.POST)
|
||||||
if form_response.is_valid():
|
if app_form.is_valid():
|
||||||
form_post = dict(request.POST)
|
app_form_post = app_form.cleaned_data
|
||||||
print(form_post)
|
if app_form_post:
|
||||||
del form_post["csrfmiddlewaretoken"]
|
print(app_form_post)
|
||||||
config_handler = AppConfig()
|
AppConfig().update_config(app_form_post)
|
||||||
if "application-settings" in form_post:
|
|
||||||
del form_post["application-settings"]
|
scheduler_form = SchedulerSettingsForm(request.POST)
|
||||||
config_handler.update_config(form_post)
|
if scheduler_form.is_valid():
|
||||||
elif "user-settings" in form_post:
|
scheduler_form_post = scheduler_form.cleaned_data
|
||||||
del form_post["user-settings"]
|
if any(scheduler_form_post.values()):
|
||||||
config_handler.set_user_config(form_post, request.user.id)
|
print(scheduler_form_post)
|
||||||
elif "scheduler-settings" in form_post:
|
ScheduleBuilder().update_schedule_conf(scheduler_form_post)
|
||||||
del form_post["scheduler-settings"]
|
|
||||||
print(form_post)
|
|
||||||
ScheduleBuilder().update_schedule_conf(form_post)
|
|
||||||
|
|
||||||
sleep(1)
|
sleep(1)
|
||||||
return redirect("settings", permanent=True)
|
return redirect("settings", permanent=True)
|
||||||
|
Loading…
Reference in New Issue
Block a user