Illustrations/web/core/context_processors.py

50 lines
1.8 KiB
Python

from pathlib import Path
from django.contrib.staticfiles import finders
from .models_ann import Announcement, AnnouncementDismissal
import os
def app_version(request):
version_file = Path(__file__).resolve().parent.parent / "version.txt"
try:
return {"APP_VERSION": version_file.read_text(encoding="utf-8").strip()}
except FileNotFoundError:
return {"APP_VERSION": "v0.0.0"}
def available_themes(request):
"""
Return clean theme names by scanning ONLY /static/themes/*.css.
Ignore admin css and fingerprinted variants.
"""
bases = set()
for finder in finders.get_finders():
try:
for path, storage in finder.list(['themes']): # only look under themes/
if not path.startswith('themes/'):
continue
if not path.endswith('.css'):
continue
filename = os.path.basename(path)[:-4] # drop .css
base = filename.split('.', 1)[0] # drop fingerprint
if base:
bases.add(base)
except Exception:
pass
preferred = ["classic", "dawn", "forest", "midnight", "mint", "sandstone", "original"]
names = sorted(
bases,
key=lambda n: (preferred.index(n) if n in preferred else len(preferred) + 1, n)
)
return {"available_themes": names}
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}