Update web/core/views.py

This commit is contained in:
Joshua Laymon 2025-09-06 03:01:08 +00:00
parent 574dd3bf3c
commit 151fd807d8

View File

@ -1052,26 +1052,29 @@ def audit_log(request):
return render(request, "tools/audit_log.html", {"rows": rows}) return render(request, "tools/audit_log.html", {"rows": rows})
from django.contrib.staticfiles import finders # === Theme selection (robust) ================================================
from django.http import HttpResponseBadRequest, HttpResponseRedirect
from django.urls import reverse
import os
def _is_valid_theme(name: str) -> bool: def _is_valid_theme(name: str) -> bool:
# Validate against files present in /static/themes # Validate that /static/themes/<name>.css exists (works with collectstatic)
for finder in finders.get_finders(): try:
for path, storage in finder.list(['themes']): return staticfiles_storage.exists(f"themes/{name}.css")
if path == f'themes/{name}.css': except Exception:
return True
return False return False
@login_required
@require_POST
def set_theme(request): def set_theme(request):
if request.method != 'POST': theme = (request.POST.get("theme") or "").strip()
return HttpResponseBadRequest('Invalid method') if not theme:
theme = (request.POST.get('theme') or '').strip() messages.error(request, "Pick a theme first.")
return redirect("settings_home")
if not _is_valid_theme(theme): if not _is_valid_theme(theme):
return HttpResponseBadRequest('Unknown theme') messages.error(
request.session['theme'] = theme request,
# also write to localStorage on next load via inline script in base.html f"Theme “{theme}” not found. Make sure /static/themes/{theme}.css exists (and run collectstatic in production)."
# redirect back to settings )
return HttpResponseRedirect(reverse('settings')) # adjust to your settings view name/URL return redirect("settings_home")
request.session["theme"] = theme
messages.success(request, f"Theme set to {theme.title()}.")
return redirect("settings_home")