Update web/core/context_processors.py
This commit is contained in:
parent
66881bd6e3
commit
72cf965fbd
@ -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/'):
|
||||
# list() with the 'themes' prefix limits the walk
|
||||
for path, storage in finder.list(["themes"]):
|
||||
# Expect paths like 'themes/<name>.css'
|
||||
if not (path.startswith("themes/") and path.endswith(".css")):
|
||||
continue
|
||||
if not path.endswith('.css'):
|
||||
base = os.path.basename(path)[:-4]
|
||||
if not base or base.startswith("_") or "." in base:
|
||||
continue
|
||||
filename = os.path.basename(path)[:-4] # drop .css
|
||||
base = filename.split('.', 1)[0] # drop fingerprint
|
||||
if base:
|
||||
bases.add(base)
|
||||
except Exception:
|
||||
pass
|
||||
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):
|
||||
|
||||
Loading…
Reference in New Issue
Block a user