Verified Commit 3c195afb authored by Štěpán Henek's avatar Štěpán Henek 🌩

render index.html (a.k.a. login page) using jinja2

parent 33981860
......@@ -56,6 +56,8 @@ def prepare_common_app(args, app_name, init_function, top_index, logger, load_pl
# internationalization
i18n_defaults(bottle.SimpleTemplate, bottle.request)
i18n_defaults(bottle.Jinja2Template, bottle.request)
bottle.Jinja2Template.settings["extensions"] = ['jinja2.ext.i18n']
# setup default template defaults
prepare_template_defaults()
......
......@@ -22,7 +22,7 @@ import logging
import time
import urllib
from bottle import Bottle, request, template, response
from bottle import Bottle, request, template, response, jinja2_template
import bottle
from foris.common import require_contract_valid, login
......@@ -636,6 +636,7 @@ def config_page_get(page_name):
bottle.redirect(reverse("config_page", page_name=current_state.guide.current))
bottle.SimpleTemplate.defaults['active_config_page_key'] = page_name
bottle.Jinja2Template.defaults['active_config_page_key'] = page_name
ConfigPage = get_config_page(page_name)
config_page = ConfigPage()
return config_page.render(active_config_page_key=page_name)
......@@ -644,6 +645,7 @@ def config_page_get(page_name):
@login_required
def config_page_post(page_name):
bottle.SimpleTemplate.defaults['active_config_page_key'] = page_name
bottle.Jinja2Template.defaults['active_config_page_key'] = page_name
ConfigPage = get_config_page(page_name)
config_page = ConfigPage(request.POST)
if request.is_xhr:
......@@ -667,6 +669,7 @@ def config_page_post(page_name):
@login_required
def config_action(page_name, action):
bottle.SimpleTemplate.defaults['active_config_page'] = page_name
bottle.Jinja2Template.defaults['active_config_page'] = page_name
ConfigPage = get_config_page(page_name)
config_page = ConfigPage()
try:
......@@ -679,6 +682,7 @@ def config_action(page_name, action):
@login_required
def config_action_post(page_name, action):
bottle.SimpleTemplate.defaults['active_config_page_key'] = page_name
bottle.Jinja2Template.defaults['active_config_page_key'] = page_name
ConfigPage = get_config_page(page_name)
config_page = ConfigPage(request.POST)
if request.is_xhr:
......@@ -707,6 +711,7 @@ def config_action_post(page_name, action):
@login_required
def config_ajax(page_name):
bottle.SimpleTemplate.defaults['active_config_page_key'] = page_name
bottle.Jinja2Template.defaults['active_config_page_key'] = page_name
action = request.params.get("action")
if not action:
raise bottle.HTTPError(404, "AJAX action not specified.")
......@@ -734,6 +739,7 @@ def init_app():
app.route("/<page_name:re:.+>/", name="config_page",
callback=config_page_get)
bottle.SimpleTemplate.defaults['config_pages'] = config_page_map
bottle.Jinja2Template.defaults['config_pages'] = config_page_map
return app
......@@ -744,7 +750,7 @@ def login_redirect():
bottle.redirect(reverse("config_index"))
@bottle.view("index")
@bottle.jinja2_view("index.html.j2")
def top_index():
session = bottle.request.environ['foris.session']
if bottle.request.method == 'POST':
......
......@@ -31,6 +31,7 @@ logger = logging.getLogger("foris.config")
bottle.SimpleTemplate.defaults['contract_valid'] = contract_valid
bottle.Jinja2Template.defaults['contract_valid'] = contract_valid
def prepare_config_app(args):
......
......@@ -63,6 +63,7 @@ class CSRFPlugin(object):
def setup(self, app):
bottle.SimpleTemplate.defaults['get_csrf_token'] = get_csrf_token
bottle.Jinja2Template.defaults['get_csrf_token'] = get_csrf_token
def apply(self, callback, route):
# make CSRF protection implicitly enabled (since it's more fool-proof)
......
<span class="minor-text">{{ trans("foris version:") }} {{ foris_info.foris_version }} </span>
{{ trans("Language") }}:
<span>{{ iso2to3.get(lang(), lang()) }}</span>
{% for code in translations %}
{% if code != lang() %}
| <a href="{{ url("change_lang", lang=code, backlink=request.fullpath) }}">{{ iso2to3.get(code, code) }}</a>
{% endif %}
{% endfor %}
<!DOCTYPE html>
<html lang="{{ lang() }}">
<head>
<meta charset="utf-8">
<title>{{ title + " | " if title|default(False) else "" }}{{ trans("Turris router administration interface") }}</title>
<!--[if gt IE 8]><!--><link href="{{ static("css/screen.css") }}" rel="stylesheet" media="screen"><!--<![endif]-->
<!--[if lt IE 9]>
<script src="{{ static("js/contrib/html5.js") }}"></script>
<link href="{{ static("css/ie8.css") }}" rel="stylesheet" media="screen">
<![endif]-->
<link rel="shortcut icon" href="{{ static("img/favicon.ico") }}">
{% if PLUGIN_STYLES is defined and PLUGIN_STYLES %}
{% for static_filename in PLUGIN_STYLES %}
<link href="{{ static("plugins/%s/%s" % (PLUGIN_NAME, static_filename)) }}" rel="stylesheet" media="screen">
{% endfor %}
{% endif %}
<link href="{{ static("css/fa-regular.min.css") }}" rel="stylesheet" media="screen">
<link href="{{ static("css/fa-solid.min.css") }}" rel="stylesheet" media="screen">
<link href="{{ static("css/fontawesome.min.css") }}" rel="stylesheet" media="screen">
<meta name="viewport" content="width=device-width, initial-scale=1">
{% if foris_info.websockets["ws_port"] %}
<meta name="foris-ws-port" content="{{ str(foris_info.websockets["ws_port"]) }}">
{% endif %}
{% if foris_info.websockets["ws_path"] %}
<meta name="foris-ws-path" content="{{ foris_info.websockets["ws_path"] }}">
{% endif %}
{% if foris_info.websockets["wss_port"] %}
<meta name="foris-wss-port" content="{{ str(foris_info.websockets["wss_port"]) }}">
{% endif %}
{% if foris_info.websockets["wss_path"] %}
<meta name="foris-wss-path" content="{{ foris_info.websockets["wss_path"] }}">
{% endif %}
<script src="{{ static("js/contrib/jquery.min.js") }}"></script>
<script src="{{ static("js/contrib/parsley.min.js") }}"></script>
<script src="{{ static("js/parsley.foris-extend.js") }}"></script>
<script src="{{ static("js/foris.js") }}"></script>
<script src="{{ generated_static("javascript/foris.js") }}"></script>
<script src="{{ generated_static("javascript/parsley.messages.js") }}"></script>
{% if PLUGIN_STATIC_SCRIPTS is defined and PLUGIN_STATIC_SCRIPTS %}
{% for static_filename in PLUGIN_STATIC_SCRIPTS %}
<script src="{{ static("plugins/%s/%s" % (PLUGIN_NAME, static_filename)) }}"></script>
{% endfor %}
{% endif %}
{% if PLUGIN_DYNAMIC_SCRIPTS is defined and PLUGIN_DYNAMIC_SCRIPTS %}
{% for static_filename in PLUGIN_DYNAMIC_SCRIPTS %}
<script src="{{ generated_static("javascript/" + PLUGIN_NAME + "/" + filename) }}"></script>
{% endfor %}
{% endif %}
</head>
<body>
{% block base %}
{% endblock %}
</body>
</html>
{% for message in get_messages() %}
<div class="message {{ message.classes }}">
{{ message.text|safe }}
</div>
{% endfor %}
<noscript>
<div class="message info">
{{ trans("This page requires JavaScript for proper function. Please enable it and refresh the page.") }}
</div>
</noscript>
%# Foris - web administration interface for OpenWrt based on NETCONF
%# Copyright (C) 2013 CZ.NIC, z.s.p.o. <http://www.nic.cz>
%#
%# This program is free software: you can redistribute it and/or modify
%# it under the terms of the GNU General Public License as published by
%# the Free Software Foundation, either version 3 of the License, or
%# (at your option) any later version.
%#
%# This program is distributed in the hope that it will be useful,
%# but WITHOUT ANY WARRANTY; without even the implied warranty of
%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%# GNU General Public License for more details.
%#
%# You should have received a copy of the GNU General Public License
%# along with this program. If not, see <http://www.gnu.org/licenses/>.
%#
%rebase("_layout.tpl", **locals())
{% extends '_layout.html.j2' %}
<%
doc_url = "https://doc.turris.cz/doc/%(lang)s/howto/https" % dict(lang=("cs" if lang() == "cs" else "en"))
%>
{% set doc_url = "https://doc.turris.cz/doc/%(lang)s/howto/https" % dict(lang=("cs" if lang() == "cs" else "en")) %}
{% block base %}
<div id="login-page">
<div class="foris-version">
%include("_foris_version.tpl")
{% include '_foris_version.html.j2' %}
</div>
<div class="language-switch">
%include("_lang_flat", translations=translations, iso2to3=iso2to3)
{% include '_lang_flat.html.j2' %}
</div>
<h1><img src="{{ static("img/logo-turris.svg") }}" alt="{{ trans("Project:Turris") }}" width="295"></h1>
%include("_messages")
{% include '_messages.html.j2' %}
%if user_authenticated():
{% if user_authenticated() %}
<a href="{{ url("logout") }}">{{ trans("Log out") }}</a>
%else:
{% else %}
<form action="{{ request.fullpath }}{{ '?next=%s' % next if next else '' }}" method="POST">
<input type="hidden" name="csrf_token" value="{{ get_csrf_token() }}">
%if request.GET.get("next"):
{% if request.GET.get("next") %}
<input type="hidden" name="next" value="{{ request.GET['next'] }}">
%end
{% endif %}
<label for="field-password">{{ trans("Password") }}</label>
<input id="field-password" type="password" name="password" placeholder="{{ trans("Password") }}" autofocus>
<button class="button" type="submit">{{ trans("Log in") }}</button>
</form>
%end
{% endif %}
<div class="footer">
{{ trans("Foris Configuration Interface") }}<br>
%if defined("luci_path"):
{% if luci_path is defined %}
<a href="{{ luci_path }}">{{ trans("Go to LuCI") }}</a>
%end
{% endif %}
</div>
%if request.urlparts.scheme == 'http':
{% if request.urlparts.scheme == 'http' %}
<div id="flashes">
<input type="checkbox" hidden id="flash-1">
<label for="flash-1" class="flash warning">
......@@ -67,6 +50,7 @@
<small>Turris OS</small>
</label>
</div>
%end
{% endif %}
</div>
{% endblock %}
......@@ -48,6 +48,26 @@ def prepare_template_defaults():
bottle.SimpleTemplate.defaults["get_csrf_token"] = get_csrf_token
bottle.SimpleTemplate.defaults["helpers"] = template_helpers
bottle.Jinja2Template.defaults['trans'] = lambda msgid: ugettext(msgid) # workaround
bottle.Jinja2Template.defaults['translation_names'] = translation_names
bottle.Jinja2Template.defaults['translations'] = [e for e in translations]
bottle.Jinja2Template.defaults['iso2to3'] = iso2to3
bottle.Jinja2Template.defaults['ungettext'] = \
lambda singular, plural, n: ungettext(singular, plural, n)
bottle.Jinja2Template.defaults['foris_info'] = current_state
# template defaults
# this is not really straight-forward, check for user_authenticated() (with brackets) in template,
# because bool(user_authenticated) is always True - it means bool(<function ...>)
bottle.Jinja2Template.defaults["user_authenticated"] =\
lambda: bottle.request.environ["foris.session"].get("user_authenticated")
bottle.Jinja2Template.defaults["request"] = bottle.request
bottle.Jinja2Template.defaults["url"] = lambda name, **kwargs: reverse(name, **kwargs)
bottle.Jinja2Template.defaults["static"] = static_path
bottle.Jinja2Template.defaults["generated_static"] = generated_static
bottle.Jinja2Template.defaults["get_csrf_token"] = get_csrf_token
bottle.Jinja2Template.defaults["helpers"] = template_helpers
def disable_caching(authenticated_only=True):
"""
......
......@@ -134,8 +134,11 @@ def set_template_defaults():
Add template functions as template defaults to supplied Bottle template
adapter.
:param template: Bottle template adapter - class inheriting from BaseTemplate
"""
bottle.SimpleTemplate.defaults['get_messages'] = get_messages
bottle.SimpleTemplate.defaults['get_alert_messages'] = \
functools.partial(get_messages, min_level=WARNING)
bottle.Jinja2Template.defaults['get_messages'] = get_messages
bottle.Jinja2Template.defaults['get_alert_messages'] = \
functools.partial(get_messages, min_level=WARNING)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment