special galleries: helper module, renderers, templates & urls; hide back in special views; highlight active special

This commit is contained in:
2026-03-27 20:09:35 -04:00
parent 24c1c96f19
commit 532690a329
8 changed files with 252 additions and 64 deletions

View File

@@ -24,6 +24,8 @@ from .common import (
is_image_file,
make_thumbnail,
)
from .specials import get_special_paths, special_name
from .models import Image as ImModel
def do_recursive_search(start_path, query):
@@ -63,7 +65,7 @@ def do_recursive_search(start_path, query):
return subdirs, images
def render_directory(request, path_text, full_path):
def render_directory(request, path_text, full_path, special=None):
"""
Renders the gallery view related to directories, be it the contents of an actual directory
in the file system, or logical gallery directories like search result pages.
@@ -81,31 +83,42 @@ def render_directory(request, path_text, full_path):
)
query_state = build_query(search_text)
try:
current_entries = [
entry for entry in full_path.iterdir() if not entry.is_symlink()
]
except OSError:
return HttpResponseNotFound("Not found")
current_subdirs = sorted(
[entry for entry in current_entries if entry.is_dir()],
key=lambda item: (
item.name.lower(),
str(item.relative_to(settings.GALLERY_ROOT)).lower(),
),
)
if search_text == "":
images = [
entry
for entry in current_entries
if entry.is_file() and is_image_file(entry)
]
# If rendering a logical special gallery, obtain the image list from the
# Image model and skip filesystem subdir enumeration.
if special is not None:
# build images using the helper that returns Path objects
images = get_special_paths(request.user, special)
# Respect the client's sort_key only if the special is favorites
# otherwise the ordering comes from the DB query (most-visited/recent).
if special == "favorites":
images = sort_images(images, sort_key)
current_subdirs = []
else:
_, images = do_recursive_search(full_path, search_text)
try:
current_entries = [
entry for entry in full_path.iterdir() if not entry.is_symlink()
]
except OSError:
return HttpResponseNotFound("Not found")
images = sort_images(images, sort_key)
current_subdirs = sorted(
[entry for entry in current_entries if entry.is_dir()],
key=lambda item: (
item.name.lower(),
str(item.relative_to(settings.GALLERY_ROOT)).lower(),
),
)
if search_text == "":
images = [
entry
for entry in current_entries
if entry.is_file() and is_image_file(entry)
]
else:
_, images = do_recursive_search(full_path, search_text)
images = sort_images(images, sort_key)
image_data = []
for image in images:
@@ -162,10 +175,15 @@ def render_directory(request, path_text, full_path):
except Exception:
dir_text = ""
back_url = gallery_url(
Path(dir_text) if dir_text != "" else None, True, query_state
)
back_thumb = get_first_image_thumbnail_url(full_path.parent)
# For special galleries hide the Back control but still present Home
if special is not None:
back_url = None
back_thumb = None
else:
back_url = gallery_url(
Path(dir_text) if dir_text != "" else None, True, query_state
)
back_thumb = get_first_image_thumbnail_url(full_path.parent)
home_url = gallery_url(None, True, query_state)
home_thumb = get_first_image_thumbnail_url(settings.GALLERY_ROOT)
@@ -189,11 +207,25 @@ def render_directory(request, path_text, full_path):
"search_action_url": gallery_url(
Path(path_text) if path_text != "" else None, True, search_action_query
),
"clear_search_url": gallery_url(
"clear_search_url": gallery_url(
Path(path_text) if path_text != "" else None, True, None
),
}
# When rendering a special gallery adjust breadcrumbs and hide search
# and back controls. Use `is_special` and `special_name` in the template.
if special is not None:
context["is_special"] = True
context["special_name"] = special_name(special)
# expose which special is active so templates can highlight it
context["active_special"] = special
# Override breadcrumbs for special galleries to be a single label
context["breadcrumbs"] = [{"label": context["special_name"], "path": None}]
# Hide the search box (templates use `is_special` to decide)
context["search_text"] = ""
context["search_action_url"] = ""
context["clear_search_url"] = ""
# sort_label depends on SORT_LABELS in common; import lazily to avoid circulars
from .common import SORT_LABELS