Files
NibasaViewer/viewer/views.py

47 lines
1.3 KiB
Python
Raw Normal View History

"""Top-level views module.
After refactor this file only keeps the minimal public view entry points and imports
helpers from the new `directory` and `image` modules.
"""
2023-07-29 18:46:58 -04:00
2023-07-30 18:55:12 -04:00
# Django imports.
from django.http import HttpResponseNotFound
from django.conf import settings
2023-08-23 17:45:39 -04:00
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect
# Local helpers split into modules
from .directory import render_directory
from .image import render_image
2023-08-21 21:20:27 -04:00
2025-01-15 15:14:40 -04:00
2023-08-23 17:45:39 -04:00
@login_required
def index(request):
return redirect("gallery_view_root")
2023-08-23 17:45:39 -04:00
2025-01-15 15:14:40 -04:00
@login_required
def gallery_view(request, path=None):
"""
Shows a list of subdirectories and image files inside the given path.
The path should be inside the GALLERY_ROOT path, otherwise a 404 error will be thrown.
"""
root = settings.GALLERY_ROOT.resolve()
try:
candidate = root.joinpath(path).resolve() if path is not None else root
candidate.relative_to(root)
except Exception:
return HttpResponseNotFound("Not found")
if not candidate.exists():
return HttpResponseNotFound("Not found")
path_text = path if path is not None else ""
if candidate.is_dir():
return render_directory(request, path_text, candidate)
return render_image(request, path_text, candidate)