diff --git a/app/src/server.py b/app/src/server.py index faf54c6..d1e62b0 100644 --- a/app/src/server.py +++ b/app/src/server.py @@ -16,6 +16,8 @@ from cnf.config_read_env import ConfigReadEnv from cnf.config_read_toml import ConfigReadToml from cnf.config_read_json import ConfigReadJson from web import Web +from web.wrapper import url_for + from modbus_tcp import ModbusTcp @@ -34,8 +36,8 @@ class ProxyState: app = Quart(__name__, template_folder='web/templates', static_folder='web/static') -Web(app, '../translations') app.secret_key = 'JKLdks.dajlKKKdladkflKwolafallsdfl' +app.jinja_env.globals.update(url_for=url_for) @app.route('/-/ready') @@ -130,6 +132,12 @@ def main(): # pragma: no cover parser.add_argument('-b', '--log_backups', type=int, default=0, help='set max number of daily log-files') + parser.add_argument('-tr', '--trans_path', type=str, + default='../translations/', + help='set path for the translations files') + parser.add_argument('-r', '--rel_urls', type=bool, + default=False, + help='use relative dashboard urls') args = parser.parse_args() # # Setup our daily, rotating logger @@ -148,6 +156,8 @@ def main(): # pragma: no cover logging.info(f"config_path: {args.config_path}") logging.info(f"json_config: {args.json_config}") logging.info(f"toml_config: {args.toml_config}") + logging.info(f"trans_path: {args.trans_path}") + logging.info(f"rel_urls: {args.rel_urls}") logging.info(f"log_path: {args.log_path}") if args.log_backups == 0: logging.info("log_backups: unlimited") @@ -186,6 +196,7 @@ def main(): # pragma: no cover Proxy.class_init() Schedule.start() ModbusTcp(loop) + Web(app, args.trans_path, args.rel_urls) # # Create tasks for our listening servers. These must be tasks! If we call @@ -202,7 +213,8 @@ def main(): # pragma: no cover try: ProxyState.set_up(True) logging.info("Start Quart") - app.run(host='0.0.0.0', port=8127, use_reloader=False, loop=loop) + app.run(host='0.0.0.0', port=8127, use_reloader=False, loop=loop, + debug=True,) logging.info("Quart stopped") except KeyboardInterrupt: diff --git a/app/src/web/__init__.py b/app/src/web/__init__.py index 32ff094..24b0e87 100644 --- a/app/src/web/__init__.py +++ b/app/src/web/__init__.py @@ -16,7 +16,11 @@ load_modules(__loader__) class Web: '''Helper Class to register the Blueprint at Quart and initializing Babel''' - def __init__(self, app: Quart, translation_directories: str | list[str]): + def __init__(self, + app: Quart, + translation_directories: str | list[str], + rel_urls: bool): + web.build_relative_urls = rel_urls app.register_blueprint(web) from .i18n import get_locale, get_tz diff --git a/app/src/web/i18n.py b/app/src/web/i18n.py index 007c592..2d4b4fb 100644 --- a/app/src/web/i18n.py +++ b/app/src/web/i18n.py @@ -1,4 +1,4 @@ -from quart import request, session, redirect, abort, url_for +from quart import request, session, redirect, abort from quart_babel.locale import get_locale as babel_get_locale from . import web @@ -38,7 +38,5 @@ def utility_processor(): async def set_language(language=None): if language in LANGUAGES: session['language'] = language - if request.referrer is not None: - return redirect(request.referrer) - return redirect(url_for('web.index')) + return redirect('../#') return abort(404) diff --git a/app/src/web/pages.py b/app/src/web/pages.py index cc1e706..d2da0e4 100644 --- a/app/src/web/pages.py +++ b/app/src/web/pages.py @@ -1,4 +1,5 @@ -from quart import render_template, url_for +from quart import render_template +from .wrapper import url_for from . import web diff --git a/app/src/web/wrapper.py b/app/src/web/wrapper.py new file mode 100644 index 0000000..57b40a9 --- /dev/null +++ b/app/src/web/wrapper.py @@ -0,0 +1,27 @@ +from quart import url_for as quart_url_for +from . import web + + +def url_for(*args, **kwargs): + """Return the url for a specific endpoint. + + This wrapper optionally convert into a relative url. + + This is most useful in templates and redirects to create a URL + that can be used in the browser. + + Arguments: + endpoint: The endpoint to build a url for, if prefixed with + ``.`` it targets endpoint's in the current blueprint. + _anchor: Additional anchor text to append (i.e. #text). + _external: Return an absolute url for external (to app) usage. + _method: The method to consider alongside the endpoint. + _scheme: A specific scheme to use. + values: The values to build into the URL, as specified in + the endpoint rule. + """ + url = quart_url_for(*args, **kwargs) + print(f"wrapper url_for: {url}") + if '/' == url[0] and web.build_relative_urls: + url = '.' + url + return url diff --git a/ha_addons/ha_addon/rootfs/run.sh b/ha_addons/ha_addon/rootfs/run.sh index 4e42950..f146a70 100755 --- a/ha_addons/ha_addon/rootfs/run.sh +++ b/ha_addons/ha_addon/rootfs/run.sh @@ -30,4 +30,4 @@ cd /home/proxy || exit export VERSION=$(cat /proxy-version.txt) echo "Start Proxyserver..." -python3 server.py --json_config=/data/options.json --log_path=/homeassistant/tsun-proxy/logs/ --config_path=/homeassistant/tsun-proxy/ --log_backups=2 +python3 server.py --rel_urls=True --json_config=/data/options.json --log_path=/homeassistant/tsun-proxy/logs/ --config_path=/homeassistant/tsun-proxy/ --log_backups=2