Update web/core/context_processors.py
This commit is contained in:
parent
eb7b5d3abb
commit
039ff39908
@ -1,51 +1,46 @@
|
|||||||
# core/context_processors.py
|
|
||||||
|
|
||||||
from pathlib import Path
|
|
||||||
from .models_ann import Announcement, AnnouncementDismissal
|
|
||||||
from django.contrib.staticfiles import finders
|
from django.contrib.staticfiles import finders
|
||||||
import os
|
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):
|
def available_themes(request):
|
||||||
"""
|
"""
|
||||||
Return clean theme names by scanning /static/themes/*.css.
|
Return clean theme names by scanning /static/themes/*.css.
|
||||||
Fingerprinted files like 'classic.400a2f.css' are normalized to 'classic'.
|
Works whether finder paths are 'themes/xxx.css' or '.../themes/xxx.css'
|
||||||
|
and collapses fingerprinted files (e.g., midnight.400a2f.css -> midnight).
|
||||||
"""
|
"""
|
||||||
bases = []
|
bases = set()
|
||||||
|
|
||||||
for finder in finders.get_finders():
|
for finder in finders.get_finders():
|
||||||
|
# 1) Try listing the 'themes' directory directly (some storages support this)
|
||||||
|
try:
|
||||||
for path, storage in finder.list(['themes']):
|
for path, storage in finder.list(['themes']):
|
||||||
if not (path.startswith('themes/') and path.endswith('.css')):
|
if path.endswith('.css'):
|
||||||
continue
|
|
||||||
filename = os.path.basename(path)[:-4] # drop .css
|
filename = os.path.basename(path)[:-4] # drop .css
|
||||||
base = filename.split('.', 1)[0] # drop any fingerprint
|
base = filename.split('.', 1)[0] # drop fingerprint
|
||||||
if base:
|
if base:
|
||||||
bases.append(base)
|
bases.add(base)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
# Prefer a nice order with Classic first; fall back to alpha for unknowns
|
# 2) Fallback: list everything and filter anything under /themes/
|
||||||
order = ["classic", "dawn", "forest", "midnight", "mint", "sandstone", "original"]
|
try:
|
||||||
# de-dupe while preserving order preference
|
for path, storage in finder.list([]):
|
||||||
uniq = []
|
if path.endswith('.css') and ('/themes/' in path or path.startswith('themes/')):
|
||||||
for name in bases:
|
filename = os.path.basename(path)[:-4]
|
||||||
if name not in uniq:
|
base = filename.split('.', 1)[0]
|
||||||
uniq.append(name)
|
if base:
|
||||||
|
bases.add(base)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
uniq.sort(key=lambda n: (order.index(n) if n in order else len(order)+1, n))
|
# If scan found nothing (e.g., dev env quirk), propose common themes
|
||||||
return {"available_themes": uniq}
|
if not bases:
|
||||||
|
candidates = ["classic", "dawn", "forest", "midnight", "mint", "sandstone"]
|
||||||
|
for name in candidates:
|
||||||
|
if finders.find(f"themes/{name}.css"):
|
||||||
|
bases.add(name)
|
||||||
|
|
||||||
def pending_announcement(request):
|
# Order: preferred list first, then alphabetical for anything else
|
||||||
user = getattr(request, "user", None)
|
preferred = ["classic", "dawn", "forest", "midnight", "mint", "sandstone", "original"]
|
||||||
if not (user and user.is_authenticated):
|
names = sorted(bases, key=lambda n: (preferred.index(n) if n in preferred else len(preferred)+1, n))
|
||||||
return {"pending_announcement": None}
|
|
||||||
current = [a for a in Announcement.objects.all() if a.is_current()]
|
return {"available_themes": names}
|
||||||
for a in current:
|
|
||||||
if not AnnouncementDismissal.objects.filter(user=user, announcement=a).exists():
|
|
||||||
return {"pending_announcement": a}
|
|
||||||
return {"pending_announcement": None}
|
|
||||||
Loading…
Reference in New Issue
Block a user