From e8b602fcaf65ac90749d012881a4bbbd10bdc523 Mon Sep 17 00:00:00 2001 From: Patrick Mayr Date: Mon, 3 Jan 2022 11:50:19 +0000 Subject: [PATCH] add dict for image and move path strings to settings --- fet2020/fet2020/settings.py | 8 +++ fet2020/gallery/utils.py | 73 +++++++++++++++++----------- fet2020/gallery/views.py | 65 +++++++++++-------------- fet2020/templates/gallery/album.html | 8 ++- fet2020/templates/gallery/index.html | 2 +- 5 files changed, 87 insertions(+), 69 deletions(-) diff --git a/fet2020/fet2020/settings.py b/fet2020/fet2020/settings.py index 8269bad7..65ebc02a 100644 --- a/fet2020/fet2020/settings.py +++ b/fet2020/fet2020/settings.py @@ -14,6 +14,7 @@ env = environ.Env( ETHERPAD_PORT=(str, "9001"), ETHERPAD_HOST=(str, ""), ETHERPAD_GROUP=(str, ""), + GALLERY_PATH=(str, "uploads/gallery"), ) # Prints and logs are written to console @@ -330,3 +331,10 @@ LOGIN_URL = "/auth/login" # MIGRATION FROM DJANGO 3.1 TO DJANGO 3.2 DEFAULT_AUTO_FIELD = "django.db.models.AutoField" + + +# GALLERY +GALLERY = { + "path": env("GALLERY_PATH"), + "thumb_path": env("GALLERY_PATH") + "_thumb", +} diff --git a/fet2020/gallery/utils.py b/fet2020/gallery/utils.py index 31774fdd..ce378274 100644 --- a/fet2020/gallery/utils.py +++ b/fet2020/gallery/utils.py @@ -2,47 +2,74 @@ import logging import os from PIL import Image, ExifTags, ImageOps +from django.conf import settings + logger = logging.getLogger(__name__) +gallery_path = settings.GALLERY["path"] +gallery_thumb_path = settings.GALLERY["thumb_path"] valid_images = [".jpg", ".png"] -def get_image_list(file_path): +def get_image_list(folder_name): + file_path = os.path.join(settings.MEDIA_ROOT + "/" + gallery_path, folder_name) img_list = [] if os.path.exists(file_path): - for f in os.listdir(file_path): - ext = os.path.splitext(f)[1] + for img in os.listdir(file_path): + ext = os.path.splitext(img)[1] if ext.lower() not in valid_images: continue - img_list.append(f) + thumb_path = os.path.join( + settings.MEDIA_ROOT + "/" + gallery_thumb_path, folder_name + ) + thumb_file_path = os.path.join(thumb_path, f"thumb_{img}") + if os.path.exists(thumb_file_path): + thumb_url = os.path.join( + settings.MEDIA_URL + gallery_thumb_path, + folder_name + "/" + f"thumb_{img}", + ) + else: + thumb_url = None + + img_dict = { + "title": img, + "image_url": os.path.join( + settings.MEDIA_URL + gallery_path, folder_name + "/" + img + ), + "thumb_url": thumb_url, + } + + img_list.append(img_dict) return img_list -def create_thumbs(folder_path): - _base_dir = "/home/project/fet2020/fet2020" - _path = "/files/uploads/gallery" - _thumb_path = "/files/uploads/gallery_thumb" +def get_folder_list(): + return next(os.walk(settings.MEDIA_ROOT + "/" + gallery_path))[1] - file_path = os.path.join(_base_dir + _path, folder_path) - thumb_path = os.path.join(_base_dir + _thumb_path, folder_path) + +def create_thumbs(folder_path): + file_path = os.path.join(settings.MEDIA_ROOT + "/" + gallery_path, folder_path) + thumb_path = os.path.join( + settings.MEDIA_ROOT + "/" + gallery_thumb_path, folder_path + ) if os.path.exists(file_path): os.makedirs(thumb_path, exist_ok=True) for f in os.listdir(file_path): - thumb_file_path = os.path.join(thumb_path, f"thumb_{f}") - if os.path.exists(thumb_file_path): - continue - ext = os.path.splitext(f)[1] if ext.lower() not in valid_images: continue + thumb_file_path = os.path.join(thumb_path, f"thumb_{f}") + if os.path.exists(thumb_file_path): + continue + image_path = os.path.join(file_path, f) - logger.info(f"Bild {f} wird bearbeitet.") + logger.info(f"Edit picture '{f}'.") with Image.open(str(image_path), "r") as image: if image._getexif() is not None: @@ -58,18 +85,6 @@ def create_thumbs(folder_path): elif exif[orientation] == 8: image = image.rotate(90, expand=True) - thumb = ImageOps.fit(image, (256, 256), Image.ANTIALIAS) + thumb = ImageOps.fit(image, (200, 200), Image.ANTIALIAS) thumb.save(thumb_file_path) - logger.info(f"Thumb {thumb} wird gespeichert.") - - -def get_thumb(folder_path, img): - _base_dir = "/home/project/fet2020/fet2020" - _thumb_path = "/files/uploads/gallery_thumb" - - thumb_path = os.path.join(_base_dir + _thumb_path, folder_path) - thumb_file_path = os.path.join(thumb_path, f"thumb_{img}") - if os.path.exists(thumb_file_path): - return f"thumb_{img}" - - return img + logger.info(f"Save thumb 'thumb_{f}'.") diff --git a/fet2020/gallery/views.py b/fet2020/gallery/views.py index b8a496a5..0ec6255b 100644 --- a/fet2020/gallery/views.py +++ b/fet2020/gallery/views.py @@ -2,40 +2,43 @@ import os from collections import deque from random import randint -from django.http import Http404, HttpResponse -from django.shortcuts import render, redirect +from django.conf import settings +from django.http import Http404 +from django.shortcuts import render from django.utils.text import slugify -from authentications.decorators import unauthenticated_user, authenticated_user +from authentications.decorators import authenticated_user from .models import Album -from .utils import get_image_list, create_thumbs, get_thumb +from .utils import create_thumbs, get_folder_list, get_image_list def index(request): - _base_dir = "/home/project/fet2020/fet2020" - _path = "/files/uploads/gallery" - - folders = next(os.walk(_base_dir + _path))[1] - if request.user.is_authenticated: albums = deque(Album.objects.all().order_by("-event_date")) else: - # show only PUBLIC albums ("20" -> PUBLIC) + # show only PUBLIC albums ("20" -> PUBLIC). albums = deque(Album.objects.filter(status="20").order_by("-event_date")) for album in list(albums): - if not album.thumbnail: - file_path = os.path.join(_base_dir + _path, album.folder_name) - img_list = get_image_list(file_path) - if img_list: - album.thumbnail = get_thumb( - album.folder_name, img_list[randint(0, len(img_list) - 1)] - ) + img_list = get_image_list(album.folder_name) + if img_list: + if album.thumbnail: + for img in img_list: + if album.thumbnail in img["title"]: + album.thumbnail = img["thumb_url"] + break + else: + value = randint(0, len(img_list) - 1) + album.thumbnail = img_list[value]["thumb_url"] else: - albums.remove(album) + value = randint(0, len(img_list) - 1) + album.thumbnail = img_list[value]["thumb_url"] + else: + # empty album is set to DRAFT. + album.status = "10" if request.user.is_authenticated: - for folder in folders: + for folder in get_folder_list(): if not Album.objects.filter(folder_name=folder): album = Album( title=folder, @@ -44,32 +47,23 @@ def index(request): event_date=None, ) - file_path = os.path.join(_base_dir + _path, album.folder_name) - img_list = get_image_list(file_path) - + img_list = get_image_list(album.folder_name) if img_list: - album.thumbnail = get_thumb( - album.folder_name, img_list[randint(0, len(img_list) - 1)] - ) - - albums.append(album) + value = randint(0, len(img_list) - 1) + album.thumbnail = img_list[value]["thumb_url"] + albums.append(album) context = { "albums": albums, - "path": _path, } return render(request, "gallery/index.html", context) def show_album(request, slug): - _base_dir = "/home/project/fet2020/fet2020" - _path = "/files/uploads/gallery" - album = Album.objects.filter(slug=slug).first() if not album: - folders = next(os.walk(_base_dir + _path))[1] - for folder in folders: + for folder in get_folder_list(): if slug in slugify(folder): album = Album( title=folder, @@ -81,15 +75,12 @@ def show_album(request, slug): else: raise Http404("wrong album slug") - file_path = os.path.join(_base_dir + _path, album.folder_name) - img_list = get_image_list(file_path) - + img_list = get_image_list(album.folder_name) create_thumbs(album.folder_name) context = { "album": album, "images": img_list, - "path": _path, } return render(request, "gallery/album.html", context) diff --git a/fet2020/templates/gallery/album.html b/fet2020/templates/gallery/album.html index d70b4afd..04c427a1 100644 --- a/fet2020/templates/gallery/album.html +++ b/fet2020/templates/gallery/album.html @@ -59,8 +59,12 @@ diff --git a/fet2020/templates/gallery/index.html b/fet2020/templates/gallery/index.html index e2d63ceb..b95f5a4e 100644 --- a/fet2020/templates/gallery/index.html +++ b/fet2020/templates/gallery/index.html @@ -21,7 +21,7 @@ {% else %} {% endif %} - + {{ album.title }}