Illustrations/web/core/context_processors.py

51 lines
1.9 KiB
Python

# core/context_processors.py
from pathlib import Path
from .models_ann import Announcement, AnnouncementDismissal
from django.contrib.staticfiles import finders
import os
def app_version(request):
version_file = Path(__file__).resolve().parent.parent / "version.txt"
try:
with open(version_file, "r") as f:
version = f.read().strip()
except FileNotFoundError:
version = "v0.0.0"
return {"APP_VERSION": version}
def available_themes(request):
"""
Return clean theme names by scanning /static/themes/*.css.
Fingerprinted files like 'classic.400a2f.css' are normalized to 'classic'.
"""
bases = []
for finder in finders.get_finders():
for path, storage in finder.list(['themes']):
if not (path.startswith('themes/') and path.endswith('.css')):
continue
filename = os.path.basename(path)[:-4] # drop .css
base = filename.split('.', 1)[0] # drop any fingerprint
if base:
bases.append(base)
# Prefer a nice order with Classic first; fall back to alpha for unknowns
order = ["classic", "dawn", "forest", "midnight", "mint", "sandstone", "original"]
# de-dupe while preserving order preference
uniq = []
for name in bases:
if name not in uniq:
uniq.append(name)
uniq.sort(key=lambda n: (order.index(n) if n in order else len(order)+1, n))
return {"available_themes": uniq}
def pending_announcement(request):
user = getattr(request, "user", None)
if not (user and user.is_authenticated):
return {"pending_announcement": None}
current = [a for a in Announcement.objects.all() if a.is_current()]
for a in current:
if not AnnouncementDismissal.objects.filter(user=user, announcement=a).exists():
return {"pending_announcement": a}
return {"pending_announcement": None}