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} ${header}
<div class="container"> <div class="container">
<div class="col-12"> <div class="col-12">
<h1>General settings</h1> <h1>${title}</h1>
</div> </div>
${sections} ${sections}
</div> </div>

View File

@ -1,6 +1,8 @@
from owrx.config import Config from owrx.config import Config
from owrx.controllers.admin import AuthorizationMixin from owrx.controllers.admin import AuthorizationMixin
from owrx.controllers.template import WebpageController from owrx.controllers.template import WebpageController
from abc import ABCMeta, abstractmethod
from urllib.parse import parse_qs
class Section(object): class Section(object):
@ -31,3 +33,58 @@ class Section(object):
class SettingsController(AuthorizationMixin, WebpageController): class SettingsController(AuthorizationMixin, WebpageController):
def indexAction(self): def indexAction(self):
self.serve_template("settings.html", **self.template_variables()) 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.settings import Section, SettingsFormController
from owrx.controllers.template import WebpageController
from owrx.controllers.admin import AuthorizationMixin
from owrx.config.core import CoreConfig from owrx.config.core import CoreConfig
from owrx.config import Config
from urllib.parse import parse_qs
from owrx.form import ( from owrx.form import (
TextInput, TextInput,
NumberInput, NumberInput,
@ -33,8 +29,12 @@ import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class GeneralSettingsController(AuthorizationMixin, WebpageController): class GeneralSettingsController(SettingsFormController):
sections = [ def getTitle(self):
return "General Settings"
def getSections(self):
return [
Section( Section(
"Receiver information", "Receiver information",
TextInput("receiver_name", "Receiver name"), 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): def handle_image(self, data, image_id):
if image_id in data: if image_id in data:
config = CoreConfig() config = CoreConfig()
@ -344,18 +318,8 @@ class GeneralSettingsController(AuthorizationMixin, WebpageController):
for file in glob("{}/{}*".format(config.get_temporary_directory(), image_id)): for file in glob("{}/{}*".format(config.get_temporary_directory(), image_id)):
os.unlink(file) os.unlink(file)
def processFormData(self): def processData(self, data):
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()}
# Image handling # Image handling
for img in ["receiver_avatar", "receiver_top_photo"]: for img in ["receiver_avatar", "receiver_top_photo"]:
self.handle_image(data, img) self.handle_image(data, img)
config = Config.get() super().processData(data)
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")