add exceptions

This commit is contained in:
2021-06-28 08:46:22 +00:00
committed by Patrick Mayr
parent c0cba47f11
commit 97c6f1e263

View File

@@ -1,6 +1,7 @@
import os import os
from django.http import HttpResponse from collections import deque
from django.http import Http404, HttpResponse
from django.shortcuts import render from django.shortcuts import render
from .models import Album from .models import Album
@@ -8,12 +9,11 @@ from .models import Album
def __get_image_list(file_path): def __get_image_list(file_path):
img_list = [] img_list = []
valid_images = [".jpg", ".png"]
if os.path.exists(file_path): if os.path.exists(file_path):
for f in os.listdir(file_path): for f in os.listdir(file_path):
ext = os.path.splitext(f)[1] ext = os.path.splitext(f)[1]
valid_images = [".jpg", ".png"]
if ext.lower() not in valid_images: if ext.lower() not in valid_images:
continue continue
@@ -28,16 +28,19 @@ def index(request):
_path = "/static/images/" _path = "/static/images/"
if request.user.is_authenticated: if request.user.is_authenticated:
albums = Album.objects.all() albums = deque(Album.objects.all())
else: else:
albums = Album.objects.filter(status="20") # show only PUBLIC albums ("20" -> PUBLIC)
albums = deque(Album.objects.filter(status="20"))
for album in albums: for album in list(albums):
if not album.thumbnail: if not album.thumbnail:
file_path = os.path.join(_base_dir + _path, album.folder_path) file_path = os.path.join(_base_dir + _path, album.folder_path)
img_list = __get_image_list(file_path) img_list = __get_image_list(file_path)
if img_list: if img_list:
album.thumbnail = img_list[0] album.thumbnail = img_list[0]
else:
albums.remove(album)
context = { context = {
"albums": albums, "albums": albums,
@@ -53,6 +56,8 @@ def show_album(request, slug):
_path = "/static/images/" _path = "/static/images/"
album = Album.objects.filter(slug=slug).first() album = Album.objects.filter(slug=slug).first()
if not album:
raise Http404("wrong album slug")
file_path = os.path.join(_base_dir + _path, album.folder_path) file_path = os.path.join(_base_dir + _path, album.folder_path)
img_list = __get_image_list(file_path) img_list = __get_image_list(file_path)