diff --git a/app/__init__.py b/app/__init__.py index 8bfd139..d9bdc98 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -83,6 +83,18 @@ def create_app(): app.config['MAX_CONTENT_LENGTH'] = 1000 * 1000 os.makedirs(os.path.dirname(app.config["DB_PATH"]), exist_ok = True) + + css_dir = 'data/static/css/' + allowed_themes = [] + for f in os.listdir(css_dir): + if not os.path.isfile(os.path.join(css_dir, f)): + continue + theme_name = os.path.splitext(os.path.basename(f))[0] + allowed_themes.append(theme_name) + + allowed_themes.sort(key=(lambda x: (x != 'style', x))) + app.config['allowed_themes'] = allowed_themes + with app.app_context(): from .schema import create as create_tables from .migrations import run_migrations @@ -179,4 +191,11 @@ def create_app(): def cachebust(subject): return f"{subject}?v={str(int(time.time()))}" + @app.template_filter('theme_name') + def get_theme_name(subject: str): + if subject == 'style': + return 'Default' + + return f'{subject.removeprefix('theme-').capitalize()} (beta)' + return app diff --git a/app/routes/users.py b/app/routes/users.py index 80f8073..23cba5f 100644 --- a/app/routes/users.py +++ b/app/routes/users.py @@ -169,6 +169,9 @@ def get_prefers_theme(): if not 'theme' in session: return 'style' + if session['theme'] not in current_app.config['allowed_themes']: + return 'style' + return session['theme'] @bp.get("/log_in") @@ -294,8 +297,8 @@ def settings_form(username): # we silently ignore the passed username # and grab the correct user from the session user = get_active_user() - theme = request.form.get('theme', default='default') - if theme == 'default': + theme = request.form.get('theme', default='style') + if theme == 'style': if 'theme' in session: session.pop('theme') else: diff --git a/app/templates/users/settings.html b/app/templates/users/settings.html index 8ab9e8b..fbce53c 100644 --- a/app/templates/users/settings.html +++ b/app/templates/users/settings.html @@ -17,8 +17,9 @@