Update web/core/views.py
This commit is contained in:
parent
574dd3bf3c
commit
151fd807d8
@ -511,7 +511,7 @@ def export_csv(request):
|
|||||||
# series = [
|
# series = [
|
||||||
# (label, Entry.objects.filter(date_added__gte=start, date_added__lt=end).count())
|
# (label, Entry.objects.filter(date_added__gte=start, date_added__lt=end).count())
|
||||||
# for label, start, end in months
|
# for label, start, end in months
|
||||||
# ]
|
# ]
|
||||||
# peak = max((v for _, v in series), default=1)
|
# peak = max((v for _, v in series), default=1)
|
||||||
# heights = [
|
# heights = [
|
||||||
# (label, value, 8 + int((value / peak) * 100) if peak else 8)
|
# (label, value, 8 + int((value / peak) * 100) if peak else 8)
|
||||||
@ -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")
|
||||||
Loading…
Reference in New Issue
Block a user