generalize settings controller

This commit is contained in:
Jakob Ketterl 2021-02-15 15:40:37 +01:00
parent 391069653a
commit 49640b5e33
3 changed files with 324 additions and 303 deletions

View File

@ -13,7 +13,7 @@
${header}
<div class="container">
<div class="col-12">
<h1>General settings</h1>
<h1>${title}</h1>
</div>
${sections}
</div>

View File

@ -1,6 +1,8 @@
from owrx.config import Config
from owrx.controllers.admin import AuthorizationMixin
from owrx.controllers.template import WebpageController
from abc import ABCMeta, abstractmethod
from urllib.parse import parse_qs
class Section(object):
@ -31,3 +33,58 @@ class Section(object):
class SettingsController(AuthorizationMixin, WebpageController):
def indexAction(self):
self.serve_template("settings.html", **self.template_variables())
class SettingsFormController(AuthorizationMixin, WebpageController, metaclass=ABCMeta):
@abstractmethod
def getSections(self):
pass
@abstractmethod
def getTitle(self):
pass
def render_sections(self):
sections = "".join(section.render() for section in self.getSections())
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()
variables["sections"] = self.render_sections()
variables["title"] = self.getTitle()
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())
def processData(self, data):
config = Config.get()
for k, v in data.items():
if v is None:
if k in config:
del config[k]
else:
config[k] = v
config.store()
self.send_redirect(self.request.path)

View File

@ -1,9 +1,5 @@
from owrx.controllers.settings import Section
from owrx.controllers.template import WebpageController
from owrx.controllers.admin import AuthorizationMixin
from owrx.controllers.settings import Section, SettingsFormController
from owrx.config.core import CoreConfig
from owrx.config import Config
from urllib.parse import parse_qs
from owrx.form import (
TextInput,
NumberInput,
@ -33,8 +29,12 @@ import logging
logger = logging.getLogger(__name__)
class GeneralSettingsController(AuthorizationMixin, WebpageController):
sections = [
class GeneralSettingsController(SettingsFormController):
def getTitle(self):
return "General Settings"
def getSections(self):
return [
Section(
"Receiver information",
TextInput("receiver_name", "Receiver name"),
@ -294,32 +294,6 @@ class GeneralSettingsController(AuthorizationMixin, WebpageController):
),
]
def render_sections(self):
sections = "".join(section.render() for section in GeneralSettingsController.sections)
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()
variables["sections"] = self.render_sections()
return variables
def handle_image(self, data, image_id):
if image_id in data:
config = CoreConfig()
@ -344,18 +318,8 @@ class GeneralSettingsController(AuthorizationMixin, WebpageController):
for file in glob("{}/{}*".format(config.get_temporary_directory(), image_id)):
os.unlink(file)
def processFormData(self):
data = parse_qs(self.get_body().decode("utf-8"), keep_blank_values=True)
data = {k: v for i in GeneralSettingsController.sections for k, v in i.parse(data).items()}
def processData(self, data):
# Image handling
for img in ["receiver_avatar", "receiver_top_photo"]:
self.handle_image(data, img)
config = Config.get()
for k, v in data.items():
if v is None:
if k in config:
del config[k]
else:
config[k] = v
config.store()
self.send_redirect("/settings/general")
super().processData(data)