diff --git a/app/Makefile b/app/Makefile index f380714..6db4b6e 100644 --- a/app/Makefile +++ b/app/Makefile @@ -10,7 +10,7 @@ APP=. SRC=$(APP)/src # Folders for Babel translation BABEL_INPUT_JINJA=$(SRC)/web/templates -BABEL_INPUT= $(foreach dir,$(BABEL_INPUT_JINJA),$(wildcard $(dir)/*.html)) \ +BABEL_INPUT= $(foreach dir,$(BABEL_INPUT_JINJA),$(wildcard $(dir)/*.html.j2)) \ BABEL_TRANSLATIONS=$(APP)/translations diff --git a/app/src/.babel.cfg b/app/src/.babel.cfg index cfaa201..a5c0c48 100644 --- a/app/src/.babel.cfg +++ b/app/src/.babel.cfg @@ -1,2 +1,3 @@ [python: **.py] -[jinja2: web/templates/**.html] \ No newline at end of file +[jinja2: web/templates/**.html] +[jinja2: web/templates/**.html.j2] \ No newline at end of file diff --git a/app/src/web/routes.py b/app/src/web/routes.py index b1f29f3..a381cc9 100644 --- a/app/src/web/routes.py +++ b/app/src/web/routes.py @@ -1,5 +1,5 @@ from quart import Blueprint -from quart import render_template +from quart import render_template, url_for from quart import send_from_directory import os @@ -13,14 +13,44 @@ async def get_icon(file: str, mime: str = 'image/png'): mimetype=mime) +def get_inv_count(): + return 1234 + + +TsunCnt = 0 + + +def get_tsun_count(): + global TsunCnt + TsunCnt += 1 + return TsunCnt + + +@web_routes.context_processor +def utility_processor(): + return dict(inv_count=get_inv_count(), + tsun_count=get_tsun_count()) + + @web_routes.route('/') async def index(): - return await render_template('index.html') + return await render_template( + 'index.html.j2', + fetch_url='.'+url_for('web_routes.data_fetch')) @web_routes.route('/page') async def empty(): - return await render_template('empty.html') + return await render_template('empty.html.j2') + + +@web_routes.route('/data-fetch') +async def data_fetch(): + global TsunCnt + TsunCnt += 1 + return { + "geology-fact": f"

{TsunCnt}

", + } @web_routes.route('/favicon-96x96.png') diff --git a/app/src/web/templates/base.html b/app/src/web/templates/base.html.j2 similarity index 85% rename from app/src/web/templates/base.html rename to app/src/web/templates/base.html.j2 index 5227c58..1a3be92 100644 --- a/app/src/web/templates/base.html +++ b/app/src/web/templates/base.html.j2 @@ -106,6 +106,36 @@ mySidebar.style.display = "none"; overlayBg.style.display = "none"; } + {% if fetch_url is defined %} + function fetch_data() { + fetch("{{fetch_url}}") + .then(response => response.json()) + .then(function (data) { + Object.keys(data).forEach(key => { + //console.log(`${key}: ${data[key]}`); + try { + elm = document.getElementById(key) + elm.innerHTML = data[key] + } + catch(err) { + console.log('error: ' + err + ' (for key: ' + key + ')'); + } + }); + }) + .catch(function (err) { + console.log('error: ' + err); + }); + } + + window.addEventListener('load', function () { + // Your document is loaded. + var fetchInterval = 5000; // 5 seconds. + + // Invoke the request every 5 seconds. + setInterval(fetch_data, fetchInterval); + }); + {% endif %} + {% endblock trailer %} diff --git a/app/src/web/templates/empty.html b/app/src/web/templates/empty.html.j2 similarity index 86% rename from app/src/web/templates/empty.html rename to app/src/web/templates/empty.html.j2 index cab4520..060dc0f 100644 --- a/app/src/web/templates/empty.html +++ b/app/src/web/templates/empty.html.j2 @@ -1,4 +1,4 @@ -{% extends 'base.html' %} +{% extends 'base.html.j2' %} {% block title %} TSUN Proxy - View {% endblock title%} {% block menu2_class %}w3-blue{% endblock %} diff --git a/app/src/web/templates/index.html b/app/src/web/templates/index.html.j2 similarity index 96% rename from app/src/web/templates/index.html rename to app/src/web/templates/index.html.j2 index 806c68a..c991e3b 100644 --- a/app/src/web/templates/index.html +++ b/app/src/web/templates/index.html.j2 @@ -1,4 +1,4 @@ -{% extends 'base.html' %} +{% extends 'base.html.j2' %} {% block title %} TSUN Proxy - Dashboard {% endblock title%} {% block menu1_class %}w3-blue{% endblock %} @@ -10,7 +10,7 @@
-

52

+

{{inv_count}}

Messages

@@ -20,7 +20,7 @@
-

99

+

{{tsun_count}}

Views

@@ -29,7 +29,7 @@
-
+

23

@@ -52,7 +52,6 @@
Regions
- Google Regional Map
Feeds
@@ -178,5 +177,3 @@
{% endblock content%} - - diff --git a/app/tests/test_web_route.py b/app/tests/test_web_route.py index 5100914..537a0ad 100644 --- a/app/tests/test_web_route.py +++ b/app/tests/test_web_route.py @@ -61,3 +61,13 @@ async def test_manifest(): response = await client.get('/site.webmanifest') assert response.status_code == 200 assert response.mimetype == 'application/manifest+json' + +@pytest.mark.asyncio +async def test_data_fetch(): + """Test the healthy route.""" + client = app.test_client() + response = await client.get('/data-fetch') + assert response.status_code == 200 + + response = await client.get('/data-fetch') + assert response.status_code == 200 diff --git a/app/translations/de/LC_MESSAGES/messages.po b/app/translations/de/LC_MESSAGES/messages.po index d76f60d..d0f6e25 100644 --- a/app/translations/de/LC_MESSAGES/messages.po +++ b/app/translations/de/LC_MESSAGES/messages.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: tsun-gen3-proxy 0.14.0\n" "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" -"POT-Creation-Date: 2025-04-18 20:46+0200\n" +"POT-Creation-Date: 2025-04-20 00:01+0200\n" "PO-Revision-Date: 2025-04-18 16:24+0200\n" "Last-Translator: FULL NAME \n" "Language: de\n" @@ -19,7 +19,7 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Generated-By: Babel 2.17.0\n" -#: src/web/templates/index.html:5 +#: src/web/templates/index.html.j2:5 msgid "My Dashboard" msgstr "Mein Dashboard" diff --git a/ha_addons/Makefile b/ha_addons/Makefile index 505db46..c661ed0 100644 --- a/ha_addons/Makefile +++ b/ha_addons/Makefile @@ -88,7 +88,7 @@ SRC_FILES := $(wildcard $(SRC_PROXY)/*.py)\ $(wildcard $(SRC_PROXY)/gen3/*.py)\ $(wildcard $(SRC_PROXY)/gen3plus/*.py)\ $(wildcard $(SRC_PROXY)/web/*.py)\ - $(wildcard $(SRC_PROXY)/web/templates/*.html)\ + $(wildcard $(SRC_PROXY)/web/templates/*.html.j2)\ $(wildcard $(SRC_PROXY)/web/static/css/*.css)\ $(wildcard $(SRC_PROXY)/web/static/font/*)\ $(wildcard $(SRC_PROXY)/web/static/font-awesome/*/*)\