Skip to content

Commit

Permalink
refactor: only use flask-themer when a theme is defined
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Jan 14, 2025
1 parent d0b32e3 commit c31e10a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
2 changes: 1 addition & 1 deletion canaille/app/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def validate_smpp_configuration(config):


def validate_theme(config):
if not os.path.exists(config["THEME"]) and not os.path.exists(
if config["THEME"] is not None and not os.path.exists(
os.path.join(ROOT, "themes", config["THEME"])
):
raise ConfigurationException(f"Cannot find theme '{config['THEME']}'")
Expand Down
47 changes: 26 additions & 21 deletions canaille/app/templating.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
import os

import flask
from flask import current_app

try:
import flask_themer
except ImportError:
flask_themer = None


if flask_themer:
render_template = flask_themer.render_template
def render_template(*args, **kwargs):
if flask_themer and current_app.config["CANAILLE"]["THEME"]:
return flask_themer.render_template(*args, **kwargs)

def setup_themer(app):
theme_config = app.config["CANAILLE"]["THEME"]
additional_themes_dir = (
os.path.abspath(os.path.dirname(theme_config))
if theme_config and os.path.exists(theme_config)
else None
)
themer = flask_themer.Themer(
app,
loaders=[flask_themer.FileSystemThemeLoader(additional_themes_dir)]
if additional_themes_dir
else None,
)
return flask.render_template(*args, **kwargs)

@themer.current_theme_loader
def get_current_theme():
# if config['THEME'] may be a theme name or a path
return app.config["CANAILLE"]["THEME"].split("/")[-1]

def setup_themer(app):
theme_path = app.config["CANAILLE"]["THEME"]

else: # pragma: no cover
render_template = flask.render_template
if not theme_path:
with app.app_context():

@current_app.context_processor
def global_processor():
return {"theme": lambda theme: theme}

return

theme_path = os.path.abspath(theme_path)
themes_dir, theme_name = theme_path.rsplit("/", 1)

themer = flask_themer.Themer(
app, loaders=[flask_themer.FileSystemThemeLoader(themes_dir)]
)

@themer.current_theme_loader
def get_current_theme():
return theme_name


def setup_jinja(app):
Expand Down
6 changes: 3 additions & 3 deletions canaille/core/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,10 @@ class CoreSettings(BaseModel):
If unset and :attr:`LOGO` is set, then the logo will be used.
"""

THEME: str = "default"
"""The name of a theme in the 'theme' directory, or a path to a theme.
THEME: str | None = None
"""A path to a theme.
Defaults to ``default``. Theming is done with `flask-themer <https://github.com/tktech/flask-themer>`_.
See the :doc:`theming documentation <tutorial/theming>` for more details.
"""

LANGUAGE: str | None = None
Expand Down
1 change: 0 additions & 1 deletion canaille/themes/default/base.html

This file was deleted.

0 comments on commit c31e10a

Please sign in to comment.