Added paging to gallery view.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
# Standard library imports.
|
||||
from pathlib import Path
|
||||
from math import ceil
|
||||
|
||||
# External library imports.
|
||||
import filetype
|
||||
@@ -13,6 +14,14 @@ from django.shortcuts import (render,
|
||||
# Project imports.
|
||||
from .utils import make_thumbnail
|
||||
|
||||
###########################################################################################
|
||||
# CONSTANTS. #
|
||||
###########################################################################################
|
||||
|
||||
CELLS_PER_ROW = 7
|
||||
ROWS_PER_PAGE = 4
|
||||
IMAGES_PER_PAGE = CELLS_PER_ROW * ROWS_PER_PAGE
|
||||
|
||||
###########################################################################################
|
||||
# View functions. #
|
||||
###########################################################################################
|
||||
@@ -23,7 +32,7 @@ def gallery_view(request, path = None):
|
||||
The path should be inside the GALLERY_ROOT path, otherwise a 404 error will be thrown.
|
||||
"""
|
||||
|
||||
def list2rows(lst, cells = 7):
|
||||
def list2rows(lst, cells = CELLS_PER_ROW):
|
||||
rows = []
|
||||
i = 0
|
||||
while i < len(lst):
|
||||
@@ -45,7 +54,14 @@ def gallery_view(request, path = None):
|
||||
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:
|
||||
try:
|
||||
page = int(request.GET.get('page', 1)) - 1
|
||||
except ValueError:
|
||||
page = 0
|
||||
|
||||
page_base = IMAGES_PER_PAGE * page
|
||||
|
||||
for image in images[page_base : page_base + IMAGES_PER_PAGE]:
|
||||
make_thumbnail(image)
|
||||
|
||||
img_context = {
|
||||
@@ -57,9 +73,11 @@ def gallery_view(request, path = None):
|
||||
img_data.append(img_context)
|
||||
|
||||
context = {
|
||||
'path': path,
|
||||
'subdirs': list2rows(subdirs),
|
||||
'images': list2rows(img_data)
|
||||
'path': path,
|
||||
'subdirs': list2rows(subdirs),
|
||||
'images': list2rows(img_data),
|
||||
'pages': range(1, ceil((len(images) / CELLS_PER_ROW) / ROWS_PER_PAGE) + 1),
|
||||
'num_files': len(images)
|
||||
}
|
||||
|
||||
return render(request, 'gallery_view.html', context)
|
||||
|
Reference in New Issue
Block a user