2023-08-16 15:14:11 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
import flask
|
2024-12-22 14:58:45 +00:00
|
|
|
from flask import request
|
2023-08-16 15:14:11 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
import flask_themer
|
2023-09-01 08:46:56 +00:00
|
|
|
except ImportError:
|
2023-08-16 15:14:11 +00:00
|
|
|
flask_themer = None
|
|
|
|
|
|
|
|
|
|
|
|
if flask_themer:
|
|
|
|
render_template = flask_themer.render_template
|
|
|
|
|
|
|
|
def setup_themer(app):
|
2023-12-18 17:06:03 +00:00
|
|
|
theme_config = app.config["CANAILLE"]["THEME"]
|
2023-08-16 15:14:11 +00:00
|
|
|
additional_themes_dir = (
|
2023-12-18 17:06:03 +00:00
|
|
|
os.path.abspath(os.path.dirname(theme_config))
|
|
|
|
if theme_config and os.path.exists(theme_config)
|
2023-08-16 15:14:11 +00:00
|
|
|
else None
|
|
|
|
)
|
|
|
|
themer = flask_themer.Themer(
|
|
|
|
app,
|
|
|
|
loaders=[flask_themer.FileSystemThemeLoader(additional_themes_dir)]
|
|
|
|
if additional_themes_dir
|
|
|
|
else None,
|
|
|
|
)
|
|
|
|
|
|
|
|
@themer.current_theme_loader
|
|
|
|
def get_current_theme():
|
2023-12-18 17:06:03 +00:00
|
|
|
# if config['THEME'] may be a theme name or a path
|
|
|
|
return app.config["CANAILLE"]["THEME"].split("/")[-1]
|
2023-08-16 15:14:11 +00:00
|
|
|
|
2023-12-15 10:58:25 +00:00
|
|
|
|
2023-08-16 15:14:11 +00:00
|
|
|
else: # pragma: no cover
|
|
|
|
render_template = flask.render_template
|
2024-12-22 14:58:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup_jinja(app):
|
|
|
|
from canaille.app.forms import password_strength_calculator
|
|
|
|
|
|
|
|
app.jinja_env.filters["len"] = len
|
|
|
|
app.jinja_env.filters["password_strength"] = password_strength_calculator
|
|
|
|
app.jinja_env.policies["ext.i18n.trimmed"] = True
|
|
|
|
|
|
|
|
@app.context_processor
|
|
|
|
def global_processor():
|
|
|
|
from canaille.app.session import current_user
|
|
|
|
|
|
|
|
return {
|
|
|
|
"debug": app.debug or app.config.get("TESTING", False),
|
|
|
|
"logo_url": app.config["CANAILLE"]["LOGO"],
|
|
|
|
"favicon_url": app.config["CANAILLE"]["FAVICON"]
|
|
|
|
or app.config["CANAILLE"]["LOGO"],
|
|
|
|
"website_name": app.config["CANAILLE"]["NAME"],
|
|
|
|
"user": current_user(),
|
|
|
|
"menu": True,
|
|
|
|
"is_boosted": request.headers.get("HX-Boosted", False),
|
|
|
|
"features": app.features,
|
|
|
|
}
|