From 72cf965fbd6f216c31aad03b1194fd8871edee4f Mon Sep 17 00:00:00 2001 From: Joshua Laymon Date: Sat, 6 Sep 2025 04:20:10 +0000 Subject: [PATCH] Update web/core/context_processors.py --- web/core/context_processors.py | 58 ++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/web/core/context_processors.py b/web/core/context_processors.py index d2121d8..ccb6e31 100644 --- a/web/core/context_processors.py +++ b/web/core/context_processors.py @@ -10,32 +10,50 @@ def app_version(request): except FileNotFoundError: return {"APP_VERSION": "v0.0.0"} +# core/context_processors.py +from pathlib import Path +from django.contrib.staticfiles import finders +from django.conf import settings +import os + def available_themes(request): """ - Return clean theme names by scanning ONLY /static/themes/*.css. - Ignore admin css and fingerprinted variants. + Return theme names by scanning /static/themes/*.css across both + STATICFILES_DIRS and app static folders. Filter out hashed/backup files + (anything with an extra dot before .css). """ - bases = set() + seen = set() + # 1) Check project-level STATICFILES_DIRS + for root in getattr(settings, "STATICFILES_DIRS", []): + dirpath = os.path.join(root, "themes") + if os.path.isdir(dirpath): + for fname in os.listdir(dirpath): + if not fname.endswith(".css"): + continue + base = fname[:-4] # strip .css + # Ignore hashed/backup or hidden files + if not base or base.startswith("_") or "." in base: + continue + seen.add(base) + + # 2) Check app static folders discovered by staticfiles finders 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 + # list() with the 'themes' prefix limits the walk + for path, storage in finder.list(["themes"]): + # Expect paths like 'themes/.css' + if not (path.startswith("themes/") and path.endswith(".css")): + continue + base = os.path.basename(path)[:-4] + if not base or base.startswith("_") or "." in base: + continue + seen.add(base) + + # Nice ordering: keep 'classic' first, then alphabetical + names = sorted(seen) + if "classic" in names: + names = ["classic"] + [n for n in names if n != "classic"] - 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):