2021-02-15 14:29:02 +00:00
|
|
|
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
|
2021-02-15 14:29:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
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-15 14:29:02 +00:00
|
|
|
|
2021-02-19 17:18:25 +00:00
|
|
|
def render(self, data):
|
2021-02-15 14:29:02 +00:00
|
|
|
return """
|
2021-02-22 22:49:28 +00:00
|
|
|
<div class="{classes}">
|
2021-02-15 14:29:02 +00:00
|
|
|
<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)
|
2021-02-15 14:29:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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())
|
2021-02-15 14:40:37 +00:00
|
|
|
return """
|
|
|
|
<form class="settings-body" method="POST">
|
|
|
|
{sections}
|
|
|
|
<div class="buttons">
|
|
|
|
<button type="submit" class="btn btn-primary">Apply</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
""".format(
|
|
|
|
sections=sections
|
|
|
|
)
|
|
|
|
|
|
|
|
def indexAction(self):
|
|
|
|
self.serve_template("settings/general.html", **self.template_variables())
|
|
|
|
|
|
|
|
def header_variables(self):
|
|
|
|
variables = super().header_variables()
|
|
|
|
variables["assets_prefix"] = "../"
|
|
|
|
return 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()
|
2021-02-18 23:03:25 +00:00
|
|
|
variables["assets_prefix"] = "../"
|
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 processFormData(self):
|
|
|
|
self.processData(self.parseFormData())
|
2021-02-19 17:45:29 +00:00
|
|
|
self.store()
|
2021-02-19 17:18:25 +00:00
|
|
|
self.send_redirect(self.request.path)
|
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()
|