diff options
Diffstat (limited to 'src/jwebmail/__init__.py')
-rw-r--r-- | src/jwebmail/__init__.py | 53 |
1 files changed, 42 insertions, 11 deletions
diff --git a/src/jwebmail/__init__.py b/src/jwebmail/__init__.py index db0c796..b5279fb 100644 --- a/src/jwebmail/__init__.py +++ b/src/jwebmail/__init__.py @@ -1,8 +1,8 @@ -import os.path as ospath +import os.path as os_path import pwd import sys -from flask import Flask +from flask import Flask, g from flask_babel import Babel, get_locale from flask_login import LoginManager, login_required from jinja2 import ChainableUndefined @@ -21,6 +21,7 @@ from .webmail import ( readmail, sendmail, writemail, + DEFAULT_LANGUAGE, ) if sys.version_info >= (3, 11): @@ -37,9 +38,9 @@ def validate_config(app): assert "@" in conf["JWEBMAIL"]["ADMIN_MAIL"] assert pwd.getpwnam(conf["JWEBMAIL"]["READ_MAILS"]["MAILBOX_USER"]) - assert ospath.isdir(conf["JWEBMAIL"]["READ_MAILS"]["MAILBOX"]) - assert ospath.isfile(conf["JWEBMAIL"]["READ_MAILS"]["AUTHENTICATOR"]) - assert ospath.isfile(conf["JWEBMAIL"]["READ_MAILS"]["BACKEND"]) + assert os_path.isdir(conf["JWEBMAIL"]["READ_MAILS"]["MAILBOX"]) + assert os_path.isfile(conf["JWEBMAIL"]["READ_MAILS"]["AUTHENTICATOR"]) + assert os_path.isfile(conf["JWEBMAIL"]["READ_MAILS"]["BACKEND"]) def create_app(): @@ -49,7 +50,7 @@ def create_app(): app.config.from_file("../../jwebmail.toml", load=toml_load, text=False) validate_config(app) - Babel(app, locale_selector=lambda: "de") + Babel(app, locale_selector=lambda: g.get("lang_code", DEFAULT_LANGUAGE)) app.cli.add_command(compile_css_command) @@ -58,32 +59,62 @@ def create_app(): login_manager.user_loader(load_user) login_manager.init_app(app) + add_view_funcs(app) + route(app) + @app.context_processor def inject_version(): return {"version": "4.0", "get_locale": get_locale, "format_mail": format_mail} - add_view_funcs(app) - route(app) + @app.url_defaults + def add_language_code(endpoint, values): + if "lang_code" in values: + return + if app.url_map.is_endpoint_expecting(endpoint, "lang_code"): + values["lang_code"] = g.get("lang_code", DEFAULT_LANGUAGE) + + @app.url_value_preprocessor + def pull_lang_code(endpoint, values): + g.lang_code = ( + values.pop("lang_code", DEFAULT_LANGUAGE) if values else DEFAULT_LANGUAGE + ) return app def route(app): app.add_url_rule("/", view_func=login, methods=["GET", "POST"]) + app.add_url_rule("/<lang_code>/", view_func=login, methods=["GET", "POST"]) app.add_url_rule("/about", view_func=about) + app.add_url_rule("/<lang_code>/about", view_func=about) + app.add_url_rule("/logout", view_func=logout) + app.add_url_rule("/<lang_code>/logout", view_func=logout) dh = login_required(displayheaders) app.add_url_rule("/home/", view_func=dh) app.add_url_rule("/home/<folder>", view_func=dh) + app.add_url_rule("/<lang_code>/home/", view_func=dh) + app.add_url_rule("/<lang_code>/home/<folder>", view_func=dh) + lr_readmail = login_required(readmail) + app.add_url_rule("/read/<msgid>", endpoint="read", view_func=lr_readmail) + app.add_url_rule("/read/<folder>/<msgid>", endpoint="read", view_func=lr_readmail) app.add_url_rule( - "/read/<msgid>", endpoint="read", view_func=login_required(readmail) + "/<lang_code>/read/<msgid>", endpoint="read", view_func=lr_readmail ) - app.add_url_rule("/raw/<msgid>", endpoint="raw", view_func=login_required(rawmail)) + app.add_url_rule( + "/<lang_code>/read/<folder>/<msgid>", endpoint="read", view_func=lr_readmail + ) + + lr_rawmail = login_required(rawmail) + app.add_url_rule("/raw/<msgid>", endpoint="raw", view_func=rawmail) + app.add_url_rule("/raw/<folder>/<msgid>", endpoint="raw", view_func=rawmail) - app.add_url_rule("/write", endpoint="write", view_func=login_required(writemail)) + lr_writemail = login_required(writemail) + app.add_url_rule("/write", endpoint="write", view_func=lr_writemail) + app.add_url_rule("/<lang_code>/write", endpoint="write", view_func=lr_writemail) app.add_url_rule( "/write", endpoint="send", view_func=login_required(sendmail), methods=["POST"] ) |