feat(viewer): persist theme/sort via toggle view; use middleware-provided theme/sort in views and templates

This commit is contained in:
Miguel Astor
2026-03-25 04:43:35 -04:00
parent 8719be3227
commit 73b538b698
5 changed files with 107 additions and 24 deletions

View File

@@ -16,6 +16,7 @@ from .common import (
normalize_theme,
build_query,
gallery_url,
append_query,
sort_images,
build_sort_options,
build_breadcrumbs,
@@ -68,9 +69,16 @@ def render_directory(request, path_text, full_path):
in the file system, or logical gallery directories like search result pages.
"""
# Search remains a GET parameter. For sort and theme prefer explicit
# GET parameters when present (so query-preserving links behave as
# callers expect), otherwise fall back to middleware-provided settings.
search_text = request.GET.get("search", "").strip()
sort_key = normalize_sort(request.GET.get("sort", "abc"))
theme = normalize_theme(request.GET.get("theme", "dark"))
sort_key = normalize_sort(
request.GET.get("sort") or getattr(request, "sort", None) or "abc"
)
theme = normalize_theme(
request.GET.get("theme") or getattr(request, "theme", None) or "dark"
)
query_state = build_query(search_text, sort_key, theme)
try:
@@ -130,10 +138,18 @@ def render_directory(request, path_text, full_path):
)
next_theme = "light" if theme == "dark" else "dark"
theme_query = {"sort": sort_key, "theme": next_theme}
# The theme toggle button now calls the persistent settings endpoint.
# Include `next` (for redirect back) and also surface `sort` and `search`
# as top-level query parameters so templates/tests can inspect them easily.
theme_toggle_query = {
"next": request.get_full_path(),
"theme": next_theme,
"sort": sort_key,
}
if search_text != "":
theme_query["search"] = search_text
theme_toggle_query["search"] = search_text
search_action_query = {"sort": sort_key, "theme": theme}
@@ -147,8 +163,8 @@ def render_directory(request, path_text, full_path):
"breadcrumbs": build_breadcrumbs(path_text, query_state),
"images": image_data,
"subdirs": subdir_data,
"theme_toggle_url": gallery_url(
Path(path_text) if path_text != "" else None, True, theme_query
"theme_toggle_url": append_query(
"/gallery/toggle-settings/", theme_toggle_query
),
"search_action_url": gallery_url(
Path(path_text) if path_text != "" else None, True, search_action_query