apply new values to config

This commit is contained in:
Jakob Ketterl 2020-03-26 23:04:02 +01:00
parent d71dc35239
commit 7efe254a66
3 changed files with 28 additions and 1 deletions

View File

@ -10,3 +10,7 @@ body {
background-color: #2e2e2e;
color: #DDD;
}
.buttons {
text-align: right;
}

View File

@ -1,6 +1,10 @@
from abc import ABC, abstractmethod
from .admin import AdminController
from owrx.config import Config
from urllib.parse import parse_qs
import logging
logger = logging.getLogger(__name__)
class Input(ABC):
@ -31,6 +35,9 @@ class Input(ABC):
def render(self, config):
return self.bootstrap_decorate(self.render_input(config[self.id]))
def validate(self, data):
return {self.id: data[self.id][0]} if self.id in data else {}
class TextInput(Input):
def render_input(self, value):
@ -69,9 +76,24 @@ class SettingsController(AdminController):
def render_form(self):
config = Config.get()
inputs = "".join([i.render(config) for i in SettingsController.inputs])
return "<form class=\"settings-body\">{inputs}</form>".format(inputs=inputs)
return """
<form class="settings-body" method="POST">
{inputs}
<div class="buttons">
<button type="submit" class="btn btn-primary">Apply</button>
</div>
</form>
""".format(inputs=inputs)
def template_variables(self):
variables = super().template_variables()
variables["form"] = self.render_form()
return variables
def processFormData(self):
data = parse_qs(self.get_body().decode("utf-8"))
data = {k: v for i in SettingsController.inputs for k, v in i.validate(data).items()}
config = Config.get()
for k, v in data.items():
config[k] = v
self.send_redirect("/admin")

View File

@ -101,6 +101,7 @@ class Router(object):
StaticRoute("/api/features", ApiController),
StaticRoute("/metrics", MetricsController),
StaticRoute("/admin", SettingsController),
StaticRoute("/admin", SettingsController, method="POST", options={"action": "processFormData"}),
StaticRoute("/login", SessionController, options={"action": "loginAction"}),
StaticRoute("/login", SessionController, method="POST", options={"action": "processLoginAction"}),
StaticRoute("/logout", SessionController, options={"action": "logoutAction"}),