Optimize and fix settings saving in web UI

This commit is contained in:
Théophile Diot 2024-09-06 16:39:51 +02:00
parent 02a8af2aa7
commit 0d133eab98
No known key found for this signature in database
GPG key ID: FA995104A0BA376A
4 changed files with 31 additions and 10 deletions

View file

@ -107,7 +107,9 @@ class Config:
"""
return self.__db.get_services_settings(methods=methods, with_drafts=with_drafts)
def check_variables(self, variables: dict, config: dict, *, global_config: bool = False, threaded: bool = False) -> dict:
def check_variables(
self, variables: dict, config: dict, *, global_config: bool = False, ignored_multiples: Optional[Set[str]] = None, threaded: bool = False
) -> dict:
"""Testify that the variables passed are valid
Parameters
@ -178,8 +180,9 @@ class Config:
flash(message, "error")
variables.pop(k)
ignored_multiples = ignored_multiples or set()
for k in config:
if k in plugins_settings:
if k in plugins_settings or k in ignored_multiples:
continue
setting = k[0 : k.rfind("_")] # noqa: E203

View file

@ -1,4 +1,5 @@
from contextlib import suppress
from re import match
from threading import Thread
from time import time
from typing import Dict
@ -30,15 +31,19 @@ def global_config_page():
wait_applying()
# Edit check fields and remove already existing ones
config = DB.get_config(methods=True, with_drafts=True)
config = DB.get_config(methods=True, with_drafts=True, filtered_settings=list(variables.keys()))
services = config["SERVER_NAME"]["value"].split(" ")
ignored_multiples = set()
for variable, value in variables.copy().items():
setting = config.get(variable, {"value": None, "global": True})
if setting["global"] and value == setting["value"]:
if match(r"^.+_\d+$", variable):
ignored_multiples.add(variable)
del variables[variable]
continue
variables = BW_CONFIG.check_variables(variables, config, global_config=True, threaded=threaded)
variables = BW_CONFIG.check_variables(variables, config, global_config=True, ignored_multiples=ignored_multiples, threaded=threaded)
if not variables:
content = "The global configuration was not edited because no values were changed."

View file

@ -1,3 +1,4 @@
from re import match
from threading import Thread
from time import time
from typing import Dict
@ -50,19 +51,22 @@ def services_service_page(service: str):
# Edit check fields and remove already existing ones
if service_exists:
config = DB.get_config(methods=True, with_drafts=True, service=service)
config = DB.get_config(methods=True, with_drafts=True, filtered_settings=list(variables.keys()), service=service)
else:
config = DB.get_config(methods=True, with_drafts=True)
config = DB.get_config(methods=True, with_drafts=True, filtered_settings=list(variables.keys()))
was_draft = config.get(f"{service}_IS_DRAFT", {"value": "no"})["value"] == "yes"
old_server_name = variables.pop("OLD_SERVER_NAME", "")
ignored_multiples = set()
# Edit check fields and remove already existing ones
for variable, value in variables.copy().items():
if variable != "SERVER_NAME" and value == config.get(f"{service}_{variable}", {"value": None})["value"]:
if match(r"^.+_\d+$", variable):
ignored_multiples.add(variable)
del variables[variable]
variables = BW_CONFIG.check_variables(variables, config, threaded=threaded)
variables = BW_CONFIG.check_variables(variables, config, ignored_multiples=ignored_multiples, threaded=threaded)
if was_draft == is_draft and not variables:
content = f"The service {service} was not edited because no values were changed."

View file

@ -107,7 +107,7 @@ $(document).ready(() => {
},
);
$(".plugin-setting").on("input", function () {
$(document).on("input", ".plugin-setting", function () {
const isValid = $(this).data("pattern")
? new RegExp($(this).data("pattern")).test($(this).val())
: true;
@ -116,7 +116,7 @@ $(document).ready(() => {
.toggleClass("is-invalid", !isValid);
});
$(".plugin-setting").on("focusout", function () {
$(document).on("focusout", ".plugin-setting", function () {
$(this).removeClass("is-valid");
});
@ -152,7 +152,16 @@ $(document).ready(() => {
$(".add-multiple").on("click", function () {
const multipleId = $(this).attr("id").replace("add-", "");
const suffix = $(`#${multipleId}`).find(".multiple-container").length;
const suffix =
parseInt(
$(`#${multipleId}`)
.find(".multiple-container")
.last()
.find(".multiple-collapse")
.attr("id")
.replace(`${multipleId}-`, ""),
10,
) + 1;
const cloneId = `${multipleId}-${suffix}`;
// Clone the first .multiple-container and reset input values