72 lines
2.3 KiB
Python
72 lines
2.3 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.
|
|
"""
|
|
|
|
def list2rows(lst, cells = 7):
|
|
rows = []
|
|
i = 0
|
|
while i < len(lst):
|
|
row = []
|
|
for c in range(cells):
|
|
try:
|
|
row.append(lst[i + c])
|
|
except:
|
|
pass
|
|
rows.append(row)
|
|
i += cells
|
|
return rows
|
|
|
|
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))])
|
|
img_data = []
|
|
|
|
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
|
|
}
|
|
|
|
img_data.append(img_context)
|
|
|
|
context = {
|
|
'path': path,
|
|
'subdirs': list2rows(subdirs),
|
|
'images': list2rows(img_data)
|
|
}
|
|
|
|
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')
|