render inputs in code, not in html

This commit is contained in:
Jakob Ketterl 2020-03-26 21:52:34 +01:00
parent 16639c0b5b
commit ab9df41a21
4 changed files with 78 additions and 38 deletions

View File

@ -17,39 +17,7 @@ ${header}
<h3 class="settings-header">
General Settings
</h3>
<form class="settings-body">
<div class="form-group row">
<label class="col-form-label col-form-label-sm col-3" for="receiver_name">Receiver name</label>
<input type="text" class="form-control form-control-sm col-9" id="receiver_name" name="receiver_name" placeholder="Receiver name">
</div>
<div class="form-group row">
<label class="col-form-label col-form-label-sm col-3" for="receiver_location">Receiver location</label>
<input type="text" class="form-control form-control-sm col-9" id="receiver_location" name="receiver_location" placeholder="Receiver location">
</div>
<div class="form-group row">
<label class="col-form-label col-form-label-sm col-3" for="receiver_asl">Receiver elevation</label>
<div class="col-9 p-0">
<input type="text" class="form-control form-control-sm" id="receiver_asl" name="receiver_asl" placeholder="Receiver elevation">
<small class="form-text text-muted">Elevation in meters above mean see level</small>
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-form-label-sm col-3" for="receiver_admin">Receiver admin</label>
<input type="text" class="form-control form-control-sm col-9" id="receiver_admin" name="receiver_admin" placeholder="Receiver admin">
</div>
<div class="form-group row">
<label class="col-form-label col-form-label-sm col-3" for="receiver_gps">Receiver coordinates</label>
<div class="col-9">Placeholder for a map widget to select receiver location</div>
</div>
<div class="form-group row">
<label class="col-form-label col-form-label-sm col-3" for="photo_title">Photo title</label>
<input type="text" class="form-control form-control-sm col-9" id="photo_title" name="photo_title" placeholder="Photo title">
</div>
<div class="form-group row">
<label class="col-form-label col-form-label-sm col-3" for="photo_desc">Photo description</label>
<textarea class="form-control form-control-sm col-9" id="photo_desc" name="photo_desc"></textarea>
</div>
</form>
${form}
</div>
</div>
</body>

View File

@ -20,6 +20,3 @@ class AdminController(WebpageController):
super().handle_request()
else:
self.send_redirect("/login")
def indexAction(self):
self.serve_template("admin.html", **self.template_variables())

View File

@ -0,0 +1,75 @@
from abc import ABC, abstractmethod
from .admin import AdminController
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
def render_input(self):
pass
def render(self):
return self.bootstrap_decorate(self.render_input())
class TextInput(Input):
def render_input(self):
return """
<input type="text" class="{classes}" id="{id}" name="{id}" placeholder="{label}">
""".format(id=self.id, label=self.label, classes=self.input_classes())
class LocationInput(Input):
def render_input(self):
# TODO make this work and pretty
return "Placeholder for a map widget to select receiver location"
class TextAreaInput(Input):
def render_input(self):
return """
<textarea class="{classes}" id="{id}" name="{id}"></textarea>
""".format(id=self.id, classes=self.input_classes())
class SettingsController(AdminController):
inputs = [
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")
]
def indexAction(self):
self.serve_template("admin.html", **self.template_variables())
def render_form(self):
inputs = "".join([i.render() for i in SettingsController.inputs])
return "<form class=\"settings-body\">{inputs}</form>".format(inputs=inputs)
def template_variables(self):
variables = super().template_variables()
variables["form"] = self.render_form()
return variables

View File

@ -11,7 +11,7 @@ from owrx.controllers.assets import (
from owrx.controllers.websocket import WebSocketController
from owrx.controllers.api import ApiController
from owrx.controllers.metrics import MetricsController
from owrx.controllers.admin import AdminController
from owrx.controllers.settings import SettingsController
from owrx.controllers.session import SessionController
from http.server import BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs
@ -100,7 +100,7 @@ class Router(object):
StaticRoute("/features", FeatureController),
StaticRoute("/api/features", ApiController),
StaticRoute("/metrics", MetricsController),
StaticRoute("/admin", AdminController),
StaticRoute("/admin", SettingsController),
StaticRoute("/login", SessionController, options={"action": "loginAction"}),
StaticRoute("/login", SessionController, method="POST", options={"action": "processLoginAction"}),
StaticRoute("/logout", SessionController, options={"action": "logoutAction"}),