openwebrx-clone/owrx/controllers/template.py

46 lines
1.5 KiB
Python
Raw Normal View History

2021-02-15 16:12:17 +00:00
from owrx.controllers import Controller
from owrx.details import ReceiverDetails
from string import Template
2021-02-15 16:12:17 +00:00
import pkg_resources
2020-02-23 18:29:17 +00:00
class TemplateController(Controller):
def render_template(self, file, **vars):
file_content = pkg_resources.resource_string("htdocs", file).decode("utf-8")
template = Template(file_content)
return template.safe_substitute(**vars)
def serve_template(self, file, **vars):
self.send_response(self.render_template(file, **vars), content_type="text/html")
def default_variables(self):
return {}
2020-02-23 18:29:17 +00:00
class WebpageController(TemplateController):
2021-03-03 14:24:18 +00:00
def get_document_root(self):
path_parts = [part for part in self.request.path[1:].split("/")]
levels = max(0, len(path_parts) - 1)
return "../" * levels
2021-02-13 15:44:14 +00:00
def header_variables(self):
2021-03-03 14:24:18 +00:00
variables = {"document_root": self.get_document_root()}
2021-02-15 16:12:17 +00:00
variables.update(ReceiverDetails().__dict__())
return variables
2021-02-13 15:44:14 +00:00
def template_variables(self):
2021-02-13 15:44:14 +00:00
header = self.render_template("include/header.include.html", **self.header_variables())
2021-03-03 14:24:18 +00:00
return {"header": header, "document_root": self.get_document_root()}
class IndexController(WebpageController):
def indexAction(self):
self.serve_template("index.html", **self.template_variables())
class MapController(WebpageController):
def indexAction(self):
# TODO check if we have a google maps api key first?
self.serve_template("map.html", **self.template_variables())