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