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

@@ -23,6 +23,7 @@ from .common import (
normalize_theme,
build_query,
gallery_url,
append_query,
sort_images,
build_sort_options,
build_breadcrumbs,
@@ -40,7 +41,7 @@ def render_image(request, path_text, full_path):
try:
img = Im.objects.get(path=full_path, user=request.user)
if request.method == 'POST':
if request.method == "POST":
img.favorite = not img.favorite
except Im.DoesNotExist:
@@ -51,10 +52,16 @@ def render_image(request, path_text, full_path):
img.visits = img.visits + 1
img.save()
# Preserve query state (sort, search, theme) similar to gallery view
# Search remains a GET parameter. For sort and theme prefer explicit
# GET parameters when present, otherwise fall back to middleware-provided
# settings on the request.
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)
image = Path("/imgs/").joinpath(path_text)
@@ -169,9 +176,13 @@ def render_image(request, path_text, full_path):
breadcrumbs.append({"label": full_path.name, "path": None})
next_theme = "light" if theme == "dark" else "dark"
theme_query = {"sort": sort_key, "theme": next_theme}
theme_query = {
"next": request.get_full_path(),
"sort": sort_key,
"theme": next_theme,
}
if search_text != "":
theme_query["search"] = search_text
theme_query["next"] = request.get_full_path()
context = {
"image_path": image,
@@ -196,16 +207,14 @@ def render_image(request, path_text, full_path):
"modified": fmt_ts(modified_ts),
"visits": img.visits,
"visited": fmt_ts(img.last_visited.timestamp()),
"favorite": img.favorite
"favorite": img.favorite,
},
"breadcrumbs": breadcrumbs,
"theme": theme,
"sort_key": sort_key,
"sort_label": "",
"sort_options": build_sort_options(request, search_text, sort_key, theme),
"theme_toggle_url": gallery_url(
Path(dir_path_text) if dir_path_text != "" else None, True, theme_query
),
"theme_toggle_url": append_query("/gallery/toggle-settings/", theme_query),
"path": path_text,
}