openwebrx-clone/owrx/controllers/template.py

46 lines
1.4 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-02-13 15:44:14 +00:00
def header_variables(self):
2021-02-15 16:12:17 +00:00
variables = {"assets_prefix": ""}
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())
return {"header": header}
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())
class FeatureController(WebpageController):
def indexAction(self):
self.serve_template("features.html", **self.template_variables())