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:
|
except FileNotFoundError:
|
||||||
return {"APP_VERSION": "v0.0.0"}
|
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):
|
def available_themes(request):
|
||||||
"""
|
"""
|
||||||
Return clean theme names by scanning ONLY /static/themes/*.css.
|
Return theme names by scanning /static/themes/*.css across both
|
||||||
Ignore admin css and fingerprinted variants.
|
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():
|
for finder in finders.get_finders():
|
||||||
try:
|
# list() with the 'themes' prefix limits the walk
|
||||||
for path, storage in finder.list(['themes']): # only look under themes/
|
for path, storage in finder.list(["themes"]):
|
||||||
if not path.startswith('themes/'):
|
# Expect paths like 'themes/<name>.css'
|
||||||
continue
|
if not (path.startswith("themes/") and path.endswith(".css")):
|
||||||
if not path.endswith('.css'):
|
continue
|
||||||
continue
|
base = os.path.basename(path)[:-4]
|
||||||
filename = os.path.basename(path)[:-4] # drop .css
|
if not base or base.startswith("_") or "." in base:
|
||||||
base = filename.split('.', 1)[0] # drop fingerprint
|
continue
|
||||||
if base:
|
seen.add(base)
|
||||||
bases.add(base)
|
|
||||||
except Exception:
|
# Nice ordering: keep 'classic' first, then alphabetical
|
||||||
pass
|
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}
|
return {"available_themes": names}
|
||||||
|
|
||||||
def pending_announcement(request):
|
def pending_announcement(request):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user