kopia lustrzana https://github.com/bellingcat/auto-archiver
Proof of concept for settings page
rodzic
5211c5de18
commit
7562938151
|
@ -33,3 +33,4 @@ dist*
|
|||
docs/_build/
|
||||
docs/source/autoapi/
|
||||
docs/source/modules/autogen/
|
||||
scripts/settings_page.html
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
import os
|
||||
from auto_archiver.core.module import ModuleFactory
|
||||
from auto_archiver.core.consts import MODULE_TYPES
|
||||
import jinja2
|
||||
|
||||
# Get available modules
|
||||
module_factory = ModuleFactory()
|
||||
available_modules = module_factory.available_modules()
|
||||
|
||||
modules_by_type = {}
|
||||
# Categorize modules by type
|
||||
for module in available_modules:
|
||||
for type in module.type:
|
||||
modules_by_type.setdefault(type, []).append(module)
|
||||
|
||||
|
||||
module_sections = ""
|
||||
# Add module sections
|
||||
for module_type in MODULE_TYPES:
|
||||
module_sections += f"<div class='module-section'><h3>{module_type}</h3>"
|
||||
for module in modules_by_type[module_type]:
|
||||
module_name = module.name
|
||||
module_sections += f"""
|
||||
<div>
|
||||
<input type="checkbox" id="{module.name}" name="{module.name}" onclick="toggleModuleConfig(this, '{module.name}')">
|
||||
<label for="{module.name}">{module.display_name} <a href="#{module.name}-config" id="{module.name}-config-link" style="display:none;">(configure)</a></label>
|
||||
</div>
|
||||
"""
|
||||
module_sections += "</div>"
|
||||
|
||||
# Add module configuration sections
|
||||
|
||||
all_modules_ordered_by_type = sorted(available_modules, key=lambda x: (MODULE_TYPES.index(x.type[0]), not x.requires_setup))
|
||||
|
||||
module_configs = ""
|
||||
|
||||
for module in all_modules_ordered_by_type:
|
||||
if not module.configs:
|
||||
continue
|
||||
module_configs += f"<div id='{module.name}-config' class='module-config'><h3>{module.display_name} Configuration</h3>"
|
||||
for option, value in module.configs.items():
|
||||
# create a human readable label
|
||||
option = option.replace('_', ' ').title()
|
||||
|
||||
# type - if value has 'choices', then it's a select
|
||||
if 'choices' in value:
|
||||
module_configs += f"""
|
||||
<div>
|
||||
<label for="{module.name}-{option}">{option}</label>
|
||||
<select id="{module.name}-{option}" name="{module.name}-{option}">
|
||||
"""
|
||||
for choice in value['choices']:
|
||||
module_configs += f"<option value='{choice}'>{choice}</option>"
|
||||
module_configs += "</select></div>"
|
||||
elif value.get('type') == 'bool' or isinstance(value.get('default', None), bool):
|
||||
module_configs += f"""
|
||||
<div>
|
||||
<input type="checkbox" id="{module.name}-{option}" name="{module.name}-{option}">
|
||||
<label for="{module.name}-{option}">{option}</label>
|
||||
</div>
|
||||
"""
|
||||
else:
|
||||
module_configs += f"""
|
||||
<div>
|
||||
<label for="{module.name}-{option}">{option}</label>
|
||||
<input type="text" id="{module.name}-{option}" name="{module.name}-{option}">
|
||||
</div>
|
||||
"""
|
||||
module_configs += "</div>"
|
||||
|
||||
# format the settings.html jinja page with the module sections and module configuration sections
|
||||
settings_page = "settings.html"
|
||||
template_loader = jinja2.FileSystemLoader(searchpath="./")
|
||||
template_env = jinja2.Environment(loader=template_loader)
|
||||
template = template_env.get_template(settings_page)
|
||||
html_content = template.render(module_sections=module_sections, module_configs=module_configs)
|
||||
|
||||
# Write HTML content to file
|
||||
output_file = '/Users/patrick/Developer/auto-archiver/scripts/settings_page.html'
|
||||
with open(output_file, 'w') as file:
|
||||
file.write(html_content)
|
||||
|
||||
print(f"Settings page generated at {output_file}")
|
|
@ -0,0 +1,103 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Module Settings</title>
|
||||
<style>
|
||||
.module-section { margin-bottom: 20px; }
|
||||
.module-config { display: none; }
|
||||
</style>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-yaml/4.1.0/js-yaml.min.js"></script>
|
||||
<script>
|
||||
function toggleModuleConfig(elem, moduleName) {
|
||||
var configSection = document.getElementById(moduleName + '-config');
|
||||
configSection.style.display = elem.checked ? 'block' : 'none';
|
||||
}
|
||||
|
||||
function loadSettings() {
|
||||
var settingsFile = document.getElementById('settings-file').value || '/Users/patrick/Developer/auto-archiver/secrets/orchestration.yaml';
|
||||
fetch(settingsFile)
|
||||
.then(response => response.text())
|
||||
.then(yamlText => {
|
||||
var config = jsyaml.load(yamlText);
|
||||
updateSettings(config);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error loading settings file:', error);
|
||||
alert('Failed to load settings file.');
|
||||
});
|
||||
}
|
||||
|
||||
function updateSettings(config) {
|
||||
for (var moduleName in config) {
|
||||
var moduleCheckbox = document.getElementById(moduleName);
|
||||
if (moduleCheckbox) {
|
||||
moduleCheckbox.checked = true;
|
||||
toggleModuleConfig(moduleName);
|
||||
var moduleConfig = config[moduleName].options;
|
||||
for (var option in moduleConfig) {
|
||||
var input = document.getElementById(moduleName + '-' + option);
|
||||
if (input) {
|
||||
if (input.type === 'checkbox') {
|
||||
input.checked = moduleConfig[option];
|
||||
} else if (input.type === 'select-one') {
|
||||
input.value = moduleConfig[option];
|
||||
} else {
|
||||
input.value = moduleConfig[option];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function saveSettings() {
|
||||
var formData = new FormData(document.querySelector('form'));
|
||||
var config = {};
|
||||
|
||||
formData.forEach((value, key) => {
|
||||
var [moduleName, option] = key.split('-');
|
||||
if (!config[moduleName]) {
|
||||
config[moduleName] = { enabled: false, options: {} };
|
||||
}
|
||||
if (option === undefined) {
|
||||
config[moduleName].enabled = value === 'on';
|
||||
} else {
|
||||
config[moduleName].options[option] = value;
|
||||
}
|
||||
});
|
||||
|
||||
fetch('/save_settings', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(config)
|
||||
}).then(response => {
|
||||
if (response.ok) {
|
||||
alert('Settings saved successfully!');
|
||||
} else {
|
||||
alert('Failed to save settings.');
|
||||
}
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Module Settings</h1>
|
||||
<form onsubmit="event.preventDefault(); saveSettings();">
|
||||
<label for="settings-file">Settings File:</label>
|
||||
<input type="text" id="settings-file" name="settings-file" value="/Users/patrick/Developer/auto-archiver/secrets/orchestration.yaml">
|
||||
<button type="button" onclick="loadSettings()">Load Settings</button>
|
||||
<h2>Steps</h2>
|
||||
|
||||
{{module_sections}}
|
||||
|
||||
<h2>Module Configuration Section</h2>
|
||||
{{module_configs}}
|
||||
|
||||
<button type="submit">Save Settings</button>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
Ładowanie…
Reference in New Issue