mirror of
https://github.com/bunkerity/bunkerweb
synced 2026-05-24 09:28:37 +00:00
Refactor configuration handling to remove unused parameters and improve clarity in multiple settings processing
This commit is contained in:
parent
383af8f421
commit
c97fd79e0e
6 changed files with 60 additions and 20 deletions
|
|
@ -116,7 +116,6 @@ class Config:
|
|||
config: dict,
|
||||
*,
|
||||
global_config: bool = False,
|
||||
ignored_multiples: Optional[Set[str]] = None,
|
||||
new: bool = False,
|
||||
threaded: bool = False,
|
||||
) -> dict:
|
||||
|
|
@ -197,9 +196,8 @@ class Config:
|
|||
flash(message, "error")
|
||||
variables.pop(k)
|
||||
|
||||
ignored_multiples = ignored_multiples or set()
|
||||
for k in config:
|
||||
if k in plugins_settings or k in ignored_multiples:
|
||||
if k in plugins_settings:
|
||||
continue
|
||||
setting = k[0 : k.rfind("_")] # noqa: E203
|
||||
|
||||
|
|
|
|||
|
|
@ -32,19 +32,18 @@ def global_config_page():
|
|||
wait_applying()
|
||||
|
||||
# Edit check fields and remove already existing ones
|
||||
config = DB.get_config(methods=True, with_drafts=True, filtered_settings=list(variables.keys()))
|
||||
config = DB.get_config(methods=True, with_drafts=True)
|
||||
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)
|
||||
continue
|
||||
del variables[variable]
|
||||
continue
|
||||
|
||||
variables = BW_CONFIG.check_variables(variables, config, global_config=True, ignored_multiples=ignored_multiples, threaded=threaded)
|
||||
variables = BW_CONFIG.check_variables(variables, config, global_config=True, threaded=threaded)
|
||||
|
||||
if not variables:
|
||||
content = "The global configuration was not edited because no values were changed."
|
||||
|
|
|
|||
|
|
@ -204,12 +204,11 @@ def services_service_page(service: str):
|
|||
if service != "new":
|
||||
db_config = DB.get_config(methods=True, with_drafts=True, service=service)
|
||||
else:
|
||||
db_config = DB.get_config(global_only=True, methods=True, filtered_settings=list(variables.keys()))
|
||||
db_config = DB.get_config(global_only=True, methods=True)
|
||||
|
||||
was_draft = db_config.get("IS_DRAFT", {"value": "no"})["value"] == "yes"
|
||||
|
||||
old_server_name = variables.pop("OLD_SERVER_NAME", "")
|
||||
ignored_multiples = set()
|
||||
db_custom_configs = {}
|
||||
new_configs = set()
|
||||
configs_changed = False
|
||||
|
|
@ -279,10 +278,10 @@ def services_service_page(service: str):
|
|||
for variable, value in variables.copy().items():
|
||||
if (mode == "advanced" or variable != "SERVER_NAME") and value == db_config.get(variable, {"value": None})["value"]:
|
||||
if match(r"^.+_\d+$", variable):
|
||||
ignored_multiples.add(variable)
|
||||
continue
|
||||
del variables[variable]
|
||||
|
||||
variables = BW_CONFIG.check_variables(variables, db_config, ignored_multiples=ignored_multiples, new=service == "new", threaded=True)
|
||||
variables = BW_CONFIG.check_variables(variables, db_config, new=service == "new", threaded=True)
|
||||
|
||||
if service != "new" and was_draft == is_draft and not variables and not configs_changed:
|
||||
DATA["TO_FLASH"].append(
|
||||
|
|
|
|||
|
|
@ -303,10 +303,17 @@ $(document).ready(() => {
|
|||
settingValue = $this.is(":checked") ? "yes" : "no";
|
||||
}
|
||||
|
||||
// Check if it's a multiple setting with numeric suffix
|
||||
const isMultipleSetting =
|
||||
settingName &&
|
||||
$this.attr("id").startsWith("multiple-") &&
|
||||
/_\d+$/.test(settingName);
|
||||
|
||||
if (
|
||||
!isEasy &&
|
||||
settingName !== "SERVER_NAME" &&
|
||||
settingValue == originalValue
|
||||
settingValue == originalValue &&
|
||||
!isMultipleSetting
|
||||
)
|
||||
return;
|
||||
|
||||
|
|
@ -1117,7 +1124,7 @@ $(document).ready(() => {
|
|||
}, 30);
|
||||
});
|
||||
|
||||
$(".plugin-setting").on("keydown", function (e) {
|
||||
$(document).on("keydown", ".plugin-setting", function () {
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
$(".save-settings").trigger("click");
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@
|
|||
{% for plugin_data in plugins.values() %}
|
||||
{% set filtered_settings = get_filtered_settings(plugin_data["settings"], current_endpoint == "global-config") %}
|
||||
{% if filtered_settings %}
|
||||
{% for setting, setting_data in filtered_settings.items() if setting not in blacklisted_settings %}
|
||||
{% for setting, setting_data in filtered_settings.items() if not setting_data.get('multiple', false) and setting not in blacklisted_settings %}
|
||||
{% set setting_config = config.get(setting, {}) %}
|
||||
{% set setting_default = setting_data.get("default", "") %}
|
||||
{% set setting_value = setting_config.get("value", setting_default) %}
|
||||
|
|
@ -49,6 +49,22 @@
|
|||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% set plugin_multiples = get_multiples(filtered_settings, config) %}
|
||||
{% if plugin_multiples %}
|
||||
{% for multiple, multiples in plugin_multiples.items() %}
|
||||
{% for setting_suffix, settings in multiples.items() %}
|
||||
{% for setting, setting_data in settings.items() if setting not in blacklisted_settings %}
|
||||
{% set setting_config = config.get(setting, {}) %}
|
||||
{% set setting_default = setting_data.get("default", "") %}
|
||||
{% set setting_value = setting_config.get("value", setting_default) %}
|
||||
{% if setting_value != setting_default %}
|
||||
{% if config_lines.append(setting + "=" + setting_value) %}{% endif %}
|
||||
{% if default_settings.append(setting + "=" + setting_default) %}{% endif %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
<!-- Join the configuration lines with newlines -->
|
||||
{% set raw_config = config_lines | join('\r\n') %}
|
||||
|
|
|
|||
|
|
@ -102,9 +102,11 @@ def handle_stop(signum, frame):
|
|||
|
||||
def get_multiples(settings: dict, config: dict) -> Dict[str, Dict[str, Dict[str, dict]]]:
|
||||
plugin_multiples = {}
|
||||
|
||||
for setting, data in settings.items():
|
||||
multiple = data.get("multiple")
|
||||
if multiple:
|
||||
# Add the setting without suffix for reference
|
||||
data = data | {"setting_no_suffix": setting}
|
||||
|
||||
if multiple not in plugin_multiples:
|
||||
|
|
@ -112,22 +114,41 @@ def get_multiples(settings: dict, config: dict) -> Dict[str, Dict[str, Dict[str,
|
|||
if "0" not in plugin_multiples[multiple]:
|
||||
plugin_multiples[multiple]["0"] = {}
|
||||
|
||||
# Add the base (suffix "0") setting
|
||||
plugin_multiples[multiple]["0"].update({setting: data})
|
||||
|
||||
for config_setting in config:
|
||||
# Process config settings with suffixes
|
||||
for config_setting, value in config.items():
|
||||
setting_match = match(setting + r"_(?P<suffix>\d+)$", config_setting)
|
||||
if setting_match:
|
||||
suffix = setting_match.group("suffix")
|
||||
if suffix == "0":
|
||||
continue
|
||||
|
||||
if suffix not in plugin_multiples[multiple]:
|
||||
plugin_multiples[multiple][suffix] = {}
|
||||
plugin_multiples[multiple][suffix].update({config_setting: data})
|
||||
plugin_multiples[multiple][suffix][config_setting] = {
|
||||
**data,
|
||||
"value": value, # Include the value from the config
|
||||
}
|
||||
|
||||
# Ensure every suffix group has all settings in the same order as "0"
|
||||
base_settings = plugin_multiples[multiple]["0"]
|
||||
for suffix, settings_dict in plugin_multiples[multiple].items():
|
||||
if suffix == "0":
|
||||
continue
|
||||
for default_setting, default_data in base_settings.items():
|
||||
if f"{default_setting}_{suffix}" not in settings_dict:
|
||||
settings_dict[f"{default_setting}_{suffix}"] = {
|
||||
**default_data,
|
||||
"value": default_data.get("value"), # Default value if not in config
|
||||
}
|
||||
|
||||
# Preserve the order of settings based on suffix "0"
|
||||
plugin_multiples[multiple][suffix] = {
|
||||
f"{default_setting}_{suffix}": settings_dict[f"{default_setting}_{suffix}"] for default_setting in base_settings
|
||||
}
|
||||
|
||||
# Sort the multiples and their settings
|
||||
for multiple, multiples in plugin_multiples.items():
|
||||
plugin_multiples[multiple] = dict(sorted(multiples.items()))
|
||||
plugin_multiples[multiple] = dict(sorted(multiples.items(), key=lambda x: int(x[0])))
|
||||
|
||||
return plugin_multiples
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue