build available theme list dynamically

This commit is contained in:
2025-08-17 20:32:17 +03:00
parent 751be27b52
commit 317182ae12
3 changed files with 27 additions and 4 deletions

View File

@ -83,6 +83,18 @@ def create_app():
app.config['MAX_CONTENT_LENGTH'] = 1000 * 1000 app.config['MAX_CONTENT_LENGTH'] = 1000 * 1000
os.makedirs(os.path.dirname(app.config["DB_PATH"]), exist_ok = True) 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(): with app.app_context():
from .schema import create as create_tables from .schema import create as create_tables
from .migrations import run_migrations from .migrations import run_migrations
@ -179,4 +191,11 @@ def create_app():
def cachebust(subject): def cachebust(subject):
return f"{subject}?v={str(int(time.time()))}" 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 return app

View File

@ -169,6 +169,9 @@ def get_prefers_theme():
if not 'theme' in session: if not 'theme' in session:
return 'style' return 'style'
if session['theme'] not in current_app.config['allowed_themes']:
return 'style'
return session['theme'] return session['theme']
@bp.get("/log_in") @bp.get("/log_in")
@ -294,8 +297,8 @@ def settings_form(username):
# we silently ignore the passed username # we silently ignore the passed username
# and grab the correct user from the session # and grab the correct user from the session
user = get_active_user() user = get_active_user()
theme = request.form.get('theme', default='default') theme = request.form.get('theme', default='style')
if theme == 'default': if theme == 'style':
if 'theme' in session: if 'theme' in session:
session.pop('theme') session.pop('theme')
else: else:

View File

@ -17,8 +17,9 @@
<form method='post'> <form method='post'>
<label for='theme'>Theme (beta)</label> <label for='theme'>Theme (beta)</label>
<select autocomplete='off' id='theme' name='theme'> <select autocomplete='off' id='theme' name='theme'>
<option value='default' {{ 'selected' if get_prefers_theme() == 'style' }}>Default</option> {% for theme in config.allowed_themes %}
<option value='theme-otomotone' {{ 'selected' if get_prefers_theme() == 'theme-otomotone' }}>Otomotone (beta)</option> <option value="{{ theme }}" {{ 'selected' if get_prefers_theme() == theme }}>{{ theme | theme_name }}</option>
{% endfor %}
</select> </select>
<label for='topic_sort_by'>Sort threads by:</label> <label for='topic_sort_by'>Sort threads by:</label>
<select id='topic_sort_by' name='topic_sort_by'> <select id='topic_sort_by' name='topic_sort_by'>