From ab9df41a21a5132e1713e05b292b342006bff26f Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Thu, 26 Mar 2020 21:52:34 +0100 Subject: [PATCH] render inputs in code, not in html --- htdocs/admin.html | 34 +--------------- owrx/controllers/admin.py | 3 -- owrx/controllers/settings.py | 75 ++++++++++++++++++++++++++++++++++++ owrx/http.py | 4 +- 4 files changed, 78 insertions(+), 38 deletions(-) create mode 100644 owrx/controllers/settings.py diff --git a/htdocs/admin.html b/htdocs/admin.html index 6bcdd9e..acddb4b 100644 --- a/htdocs/admin.html +++ b/htdocs/admin.html @@ -17,39 +17,7 @@ ${header}

General Settings

-
-
- - -
-
- - -
-
- -
- - Elevation in meters above mean see level -
-
-
- - -
-
- -
Placeholder for a map widget to select receiver location
-
-
- - -
-
- - -
-
+ ${form} \ No newline at end of file diff --git a/owrx/controllers/admin.py b/owrx/controllers/admin.py index a775068..adae5b3 100644 --- a/owrx/controllers/admin.py +++ b/owrx/controllers/admin.py @@ -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()) diff --git a/owrx/controllers/settings.py b/owrx/controllers/settings.py new file mode 100644 index 0000000..ed9462d --- /dev/null +++ b/owrx/controllers/settings.py @@ -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 = "{text}".format(text=self.infotext) if self.infotext else "" + return """ +
+ +
+ {input} + {infotext} +
+
+ """.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 """ + + """.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 """ + + """.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 "
{inputs}
".format(inputs=inputs) + + def template_variables(self): + variables = super().template_variables() + variables["form"] = self.render_form() + return variables diff --git a/owrx/http.py b/owrx/http.py index 86fa8f1..2863243 100644 --- a/owrx/http.py +++ b/owrx/http.py @@ -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"}),