From 97c6f1e2634ac8fd89b90ed1cee80681773b15a2 Mon Sep 17 00:00:00 2001 From: patrick Date: Mon, 28 Jun 2021 08:46:22 +0000 Subject: [PATCH] add exceptions --- fet2020/gallery/views.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/fet2020/gallery/views.py b/fet2020/gallery/views.py index 2c8c64dc..f453705a 100644 --- a/fet2020/gallery/views.py +++ b/fet2020/gallery/views.py @@ -1,6 +1,7 @@ import os -from django.http import HttpResponse +from collections import deque +from django.http import Http404, HttpResponse from django.shortcuts import render from .models import Album @@ -8,12 +9,11 @@ from .models import Album def __get_image_list(file_path): img_list = [] + valid_images = [".jpg", ".png"] if os.path.exists(file_path): for f in os.listdir(file_path): ext = os.path.splitext(f)[1] - valid_images = [".jpg", ".png"] - if ext.lower() not in valid_images: continue @@ -28,16 +28,19 @@ def index(request): _path = "/static/images/" if request.user.is_authenticated: - albums = Album.objects.all() + albums = deque(Album.objects.all()) 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: file_path = os.path.join(_base_dir + _path, album.folder_path) img_list = __get_image_list(file_path) if img_list: album.thumbnail = img_list[0] + else: + albums.remove(album) context = { "albums": albums, @@ -53,6 +56,8 @@ def show_album(request, slug): _path = "/static/images/" 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) img_list = __get_image_list(file_path)