58 lines
1.9 KiB
Python
58 lines
1.9 KiB
Python
# Standard library imports.
|
|
from pathlib import Path
|
|
|
|
# External library imports.
|
|
import filetype
|
|
|
|
# Django imports.
|
|
from django.http import HttpResponseNotFound
|
|
from django.conf import settings
|
|
from django.shortcuts import (render,
|
|
redirect)
|
|
|
|
# Project imports.
|
|
from .utils import make_thumbnail
|
|
|
|
###########################################################################################
|
|
# View functions. #
|
|
###########################################################################################
|
|
|
|
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.
|
|
"""
|
|
|
|
full_path = settings.GALLERY_ROOT.joinpath(path) if path is not None else settings.GALLERY_ROOT
|
|
|
|
if full_path.exists():
|
|
if full_path.is_dir():
|
|
subdirs = sorted([i for i in full_path.iterdir() if i.is_dir()])
|
|
images = sorted([i for i in full_path.iterdir() if i.is_file() and filetype.is_image(str(i))])
|
|
display_images = []
|
|
|
|
for image in images:
|
|
make_thumbnail(image)
|
|
|
|
img_context = {
|
|
'path': image.name,
|
|
'name': image.name,
|
|
'thumbnail': '/thumbs/' + path + '/' + image.name if path is not None else '/thumbs/' + image.name
|
|
}
|
|
|
|
display_images.append(img_context)
|
|
|
|
context = {
|
|
'path': path,
|
|
'subdirs': subdirs,
|
|
'images': display_images
|
|
}
|
|
|
|
return render(request, 'gallery_view.html', context)
|
|
|
|
else:
|
|
return render(request, 'image_view.html', {'image_path': Path('/imgs/').joinpath(path)})
|
|
|
|
else:
|
|
return HttpResponseNotFound('Not found')
|