openwebrx-clone/owrx/controllers/settings/__init__.py

111 lines
3.1 KiB
Python
Raw Normal View History

from owrx.config import Config
from owrx.controllers.admin import AuthorizationMixin
from owrx.controllers.template import WebpageController
2021-02-15 14:40:37 +00:00
from abc import ABCMeta, abstractmethod
from urllib.parse import parse_qs
class Section(object):
def __init__(self, title, *inputs):
self.title = title
self.inputs = inputs
2021-02-22 22:49:28 +00:00
def render_input(self, input, data):
return input.render(data)
2021-02-19 17:18:25 +00:00
def render_inputs(self, data):
2021-02-22 22:49:28 +00:00
return "".join([self.render_input(i, data) for i in self.inputs])
def classes(self):
return ["col-12", "settings-section"]
2021-02-19 17:18:25 +00:00
def render(self, data):
return """
2021-02-22 22:49:28 +00:00
<div class="{classes}">
<h3 class="settings-header">
{title}
</h3>
{inputs}
</div>
""".format(
2021-02-22 22:49:28 +00:00
classes=" ".join(self.classes()), title=self.title, inputs=self.render_inputs(data)
)
def parse(self, data):
return {k: v for i in self.inputs for k, v in i.parse(data).items()}
class SettingsController(AuthorizationMixin, WebpageController):
def indexAction(self):
self.serve_template("settings.html", **self.template_variables())
2021-02-15 14:40:37 +00:00
class SettingsFormController(AuthorizationMixin, WebpageController, metaclass=ABCMeta):
@abstractmethod
def getSections(self):
pass
@abstractmethod
def getTitle(self):
pass
2021-02-19 17:18:25 +00:00
def getData(self):
return Config.get()
2021-02-15 14:40:37 +00:00
def render_sections(self):
2021-02-19 17:18:25 +00:00
sections = "".join(section.render(self.getData()) for section in self.getSections())
buttons = self.render_buttons()
2021-02-15 14:40:37 +00:00
return """
<form class="settings-body" method="POST">
{sections}
2021-02-24 22:04:23 +00:00
<div class="buttons container">
{buttons}
2021-02-15 14:40:37 +00:00
</div>
</form>
""".format(
sections=sections,
buttons=buttons,
2021-02-15 14:40:37 +00:00
)
def render_buttons(self):
return """
<button type="submit" class="btn btn-primary">Apply and save</button>
"""
2021-02-15 14:40:37 +00:00
def indexAction(self):
self.serve_template("settings/general.html", **self.template_variables())
def template_variables(self):
variables = super().template_variables()
2021-02-18 22:05:43 +00:00
variables["content"] = self.render_sections()
2021-02-15 14:40:37 +00:00
variables["title"] = self.getTitle()
variables["modal"] = self.buildModal()
2021-02-15 14:40:37 +00:00
return variables
def parseFormData(self):
data = parse_qs(self.get_body().decode("utf-8"), keep_blank_values=True)
return {k: v for i in self.getSections() for k, v in i.parse(data).items()}
def getSuccessfulRedirect(self):
return self.request.path
2021-02-15 14:40:37 +00:00
def processFormData(self):
self.processData(self.parseFormData())
2021-02-19 17:45:29 +00:00
self.store()
self.send_redirect(self.getSuccessfulRedirect())
2021-02-15 14:40:37 +00:00
def processData(self, data):
2021-02-19 17:45:29 +00:00
config = self.getData()
2021-02-15 14:40:37 +00:00
for k, v in data.items():
if v is None:
if k in config:
del config[k]
else:
config[k] = v
2021-02-19 17:45:29 +00:00
def store(self):
Config.get().store()
def buildModal(self):
return ""