Files
NibasaViewer/viewer/views.py

47 lines
1.3 KiB
Python

"""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.
"""
# Django imports.
from django.http import HttpResponseNotFound
from django.conf import settings
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
@login_required
def index(request):
return redirect("gallery_view_root")
@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)