2020-03-26 20:52:34 +00:00
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from .admin import AdminController
|
2020-03-26 21:08:24 +00:00
|
|
|
from owrx.config import Config
|
2020-03-26 22:04:02 +00:00
|
|
|
from urllib.parse import parse_qs
|
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2020-03-26 20:52:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Input(ABC):
|
|
|
|
def __init__(self, id, label, infotext=None):
|
|
|
|
self.id = id
|
|
|
|
self.label = label
|
|
|
|
self.infotext = infotext
|
|
|
|
|
|
|
|
def bootstrap_decorate(self, input):
|
|
|
|
infotext = "<small>{text}</small>".format(text=self.infotext) if self.infotext else ""
|
|
|
|
return """
|
|
|
|
<div class="form-group row">
|
|
|
|
<label class="col-form-label col-form-label-sm col-3" for="{id}">{label}</label>
|
|
|
|
<div class="col-9 p-0">
|
|
|
|
{input}
|
|
|
|
{infotext}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
""".format(id=self.id, label=self.label, input=input, infotext=infotext)
|
|
|
|
|
|
|
|
def input_classes(self):
|
|
|
|
return " ".join(["form-control", "form-control-sm"])
|
|
|
|
|
|
|
|
@abstractmethod
|
2020-03-26 21:08:24 +00:00
|
|
|
def render_input(self, value):
|
2020-03-26 20:52:34 +00:00
|
|
|
pass
|
|
|
|
|
2020-03-26 21:08:24 +00:00
|
|
|
def render(self, config):
|
|
|
|
return self.bootstrap_decorate(self.render_input(config[self.id]))
|
2020-03-26 20:52:34 +00:00
|
|
|
|
2020-03-27 00:14:38 +00:00
|
|
|
def parse(self, data):
|
2020-03-26 22:04:02 +00:00
|
|
|
return {self.id: data[self.id][0]} if self.id in data else {}
|
|
|
|
|
2020-03-26 20:52:34 +00:00
|
|
|
|
|
|
|
class TextInput(Input):
|
2020-03-26 21:08:24 +00:00
|
|
|
def render_input(self, value):
|
2020-03-26 20:52:34 +00:00
|
|
|
return """
|
2020-03-26 21:08:24 +00:00
|
|
|
<input type="text" class="{classes}" id="{id}" name="{id}" placeholder="{label}" value="{value}">
|
|
|
|
""".format(id=self.id, label=self.label, classes=self.input_classes(), value=value)
|
2020-03-26 20:52:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LocationInput(Input):
|
2020-03-26 21:08:24 +00:00
|
|
|
def render_input(self, value):
|
2020-03-26 20:52:34 +00:00
|
|
|
# TODO make this work and pretty
|
|
|
|
return "Placeholder for a map widget to select receiver location"
|
|
|
|
|
|
|
|
|
|
|
|
class TextAreaInput(Input):
|
2020-03-26 21:08:24 +00:00
|
|
|
def render_input(self, value):
|
2020-03-26 20:52:34 +00:00
|
|
|
return """
|
2020-03-26 21:08:24 +00:00
|
|
|
<textarea class="{classes}" id="{id}" name="{id}" style="height:200px;">{value}</textarea>
|
|
|
|
""".format(id=self.id, classes=self.input_classes(), value=value)
|
2020-03-26 20:52:34 +00:00
|
|
|
|
|
|
|
|
2020-03-27 00:14:38 +00:00
|
|
|
class CheckboxInput(Input):
|
|
|
|
def __init__(self, id, label, checkboxText, infotext=None):
|
|
|
|
super().__init__(id, label, infotext=infotext)
|
|
|
|
self.checkboxText = checkboxText
|
2020-03-26 20:52:34 +00:00
|
|
|
|
2020-03-27 00:14:38 +00:00
|
|
|
def render_input(self, value):
|
|
|
|
return """
|
|
|
|
<div class="{classes}">
|
|
|
|
<input class="form-check-input" type="checkbox" id="{id}" name="{id}" {checked}>
|
|
|
|
<label class="form-check-label" for="{id}">
|
|
|
|
{checkboxText}
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
""".format(id=self.id, classes=self.input_classes(), checked="checked" if value else "", checkboxText=self.checkboxText)
|
2020-03-26 20:52:34 +00:00
|
|
|
|
2020-03-27 00:14:38 +00:00
|
|
|
def input_classes(self):
|
|
|
|
return " ".join(["form-check", "form-control-sm"])
|
|
|
|
|
|
|
|
def parse(self, data):
|
|
|
|
return {self.id: self.id in data and data[self.id][0] == "on"}
|
|
|
|
|
|
|
|
|
|
|
|
class Section(object):
|
|
|
|
def __init__(self, title, *inputs):
|
|
|
|
self.title = title
|
|
|
|
self.inputs = inputs
|
|
|
|
|
|
|
|
def render_inputs(self):
|
2020-03-26 21:08:24 +00:00
|
|
|
config = Config.get()
|
2020-03-27 00:14:38 +00:00
|
|
|
return "".join([i.render(config) for i in self.inputs])
|
|
|
|
|
|
|
|
def render(self):
|
2020-03-26 22:04:02 +00:00
|
|
|
return """
|
2020-03-27 00:14:38 +00:00
|
|
|
<div class="col-12 settings-category">
|
|
|
|
<h3 class="settings-header">
|
|
|
|
{title}
|
|
|
|
</h3>
|
2020-03-26 22:04:02 +00:00
|
|
|
{inputs}
|
2020-03-27 00:14:38 +00:00
|
|
|
</div>
|
|
|
|
""".format(title=self.title, inputs=self.render_inputs())
|
|
|
|
|
|
|
|
def parse(self, data):
|
|
|
|
return {k: v for i in self.inputs for k, v in i.parse(data).items()}
|
|
|
|
|
|
|
|
|
|
|
|
class SettingsController(AdminController):
|
|
|
|
sections = [
|
|
|
|
Section(
|
|
|
|
"General Settings",
|
|
|
|
TextInput("receiver_name", "Receiver name"),
|
|
|
|
TextInput("receiver_location", "Receiver location"),
|
|
|
|
TextInput("receiver_asl", "Receiver elevation", infotext="Elevation in meters above mean see level"),
|
|
|
|
TextInput("receiver_admin", "Receiver admin"),
|
|
|
|
LocationInput("receiver_gps", "Receiver coordinates"),
|
|
|
|
TextInput("photo_title", "Photo title"),
|
|
|
|
TextAreaInput("photo_desc", "Photo description"),
|
|
|
|
),
|
|
|
|
Section(
|
|
|
|
"sdr.hu",
|
|
|
|
TextInput("sdrhu_key", "sdr.hu key", infotext="Please obtain your personal key on <a href=\"https://sdr.hu\">sdr.hu</a>"),
|
|
|
|
CheckboxInput("sdrhu_public_listing", "List on sdr.hu", "List msy receiver on sdr.hu"),
|
|
|
|
TextInput("server_hostname", "Hostname"),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
def render_sections(self):
|
|
|
|
sections = "".join(section.render() for section in SettingsController.sections)
|
|
|
|
return """
|
|
|
|
<form class="settings-body" method="POST">
|
|
|
|
{sections}
|
2020-03-26 22:04:02 +00:00
|
|
|
<div class="buttons">
|
|
|
|
<button type="submit" class="btn btn-primary">Apply</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
2020-03-27 00:14:38 +00:00
|
|
|
""".format(sections=sections)
|
|
|
|
|
|
|
|
def indexAction(self):
|
|
|
|
self.serve_template("admin.html", **self.template_variables())
|
2020-03-26 20:52:34 +00:00
|
|
|
|
|
|
|
def template_variables(self):
|
|
|
|
variables = super().template_variables()
|
2020-03-27 00:14:38 +00:00
|
|
|
variables["sections"] = self.render_sections()
|
2020-03-26 20:52:34 +00:00
|
|
|
return variables
|
2020-03-26 22:04:02 +00:00
|
|
|
|
|
|
|
def processFormData(self):
|
|
|
|
data = parse_qs(self.get_body().decode("utf-8"))
|
2020-03-27 00:14:38 +00:00
|
|
|
data = {k: v for i in SettingsController.sections for k, v in i.parse(data).items()}
|
2020-03-26 22:04:02 +00:00
|
|
|
config = Config.get()
|
|
|
|
for k, v in data.items():
|
|
|
|
config[k] = v
|
|
|
|
self.send_redirect("/admin")
|