build available theme list dynamically
This commit is contained in:
@ -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
|
||||
|
@ -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:
|
||||
|
@ -17,8 +17,9 @@
|
||||
<form method='post'>
|
||||
<label for='theme'>Theme (beta)</label>
|
||||
<select autocomplete='off' id='theme' name='theme'>
|
||||
<option value='default' {{ 'selected' if get_prefers_theme() == 'style' }}>Default</option>
|
||||
<option value='theme-otomotone' {{ 'selected' if get_prefers_theme() == 'theme-otomotone' }}>Otomotone (beta)</option>
|
||||
{% for theme in config.allowed_themes %}
|
||||
<option value="{{ theme }}" {{ 'selected' if get_prefers_theme() == theme }}>{{ theme | theme_name }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
<label for='topic_sort_by'>Sort threads by:</label>
|
||||
<select id='topic_sort_by' name='topic_sort_by'>
|
||||
|
Reference in New Issue
Block a user