From a7756050319d774bd7eafbb0af9471f5e6e1f00c Mon Sep 17 00:00:00 2001 From: Patrick Mayr Date: Sat, 1 Jan 2022 23:31:25 +0000 Subject: [PATCH] get photos from server, optimize creating thumbs, add buttons --- fet2020/gallery/models.py | 6 +- fet2020/gallery/urls.py | 1 + fet2020/gallery/utils.py | 74 +++++++++++++++++++++++ fet2020/gallery/views.py | 87 ++++++++++++++++++---------- fet2020/templates/gallery/album.html | 27 ++++++--- fet2020/templates/gallery/index.html | 26 ++++++--- 6 files changed, 174 insertions(+), 47 deletions(-) create mode 100644 fet2020/gallery/utils.py diff --git a/fet2020/gallery/models.py b/fet2020/gallery/models.py index c0826297..a734be72 100644 --- a/fet2020/gallery/models.py +++ b/fet2020/gallery/models.py @@ -10,7 +10,7 @@ class Album(models.Model): title = models.CharField(verbose_name="Titel", max_length=200) slug = models.SlugField(unique=True, null=True, blank=True) - folder_path = models.CharField(verbose_name="Ordner Path", max_length=200) + folder_name = models.CharField(verbose_name="Ordner Name", max_length=200) thumbnail = models.CharField( verbose_name="Thumbnail", max_length=200, null=True, blank=True ) @@ -27,8 +27,8 @@ class Album(models.Model): ) status = models.CharField(max_length=2, choices=STATUS, default="10") - public_date = models.DateField( - verbose_name="Veröffentlichung", null=True, blank=True, default=timezone.now + event_date = models.DateField( + verbose_name="Event Datum", null=True, blank=True, default=timezone.now ) # Managers diff --git a/fet2020/gallery/urls.py b/fet2020/gallery/urls.py index 71f3bca6..14daf8a1 100644 --- a/fet2020/gallery/urls.py +++ b/fet2020/gallery/urls.py @@ -6,4 +6,5 @@ from . import views urlpatterns = [ path("", views.index, name="gallery"), path("/", views.show_album, name="album"), + path("draft//", views.show_draft_album, name="draft-album"), ] diff --git a/fet2020/gallery/utils.py b/fet2020/gallery/utils.py new file mode 100644 index 00000000..687c894b --- /dev/null +++ b/fet2020/gallery/utils.py @@ -0,0 +1,74 @@ +import logging +import os +from PIL import Image, ExifTags, ImageOps + + +logger = logging.getLogger(__name__) +valid_images = [".jpg", ".png"] + + +def get_image_list(file_path): + img_list = [] + + if os.path.exists(file_path): + for f in os.listdir(file_path): + ext = os.path.splitext(f)[1] + if ext.lower() not in valid_images: + continue + + img_list.append(f) + + return img_list + + +def create_thumbs(folder_path): + _base_dir = "/home/project/fet2020/fet2020" + _path = "/files/uploads/gallery" + _thumb_path = "/files/uploads/gallery_thumb" + + file_path = os.path.join(_base_dir + _path, folder_path) + thumb_path = os.path.join(_base_dir + _thumb_path, folder_path) + + if os.path.exists(file_path): + os.makedirs(thumb_path, exist_ok=True) + + for f in os.listdir(file_path): + logger.info(f"Bild {f} wird bearbeitet.") + 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 + + image_path = os.path.join(file_path, f) + + with Image.open(str(image_path), "r") as image: + if image._getexif() is not None: + for orientation in ExifTags.TAGS.keys(): + if ExifTags.TAGS[orientation] == "Orientation": + break + exif = dict(image._getexif().items()) + + if exif[orientation] == 3: + image = image.rotate(180, expand=True) + elif exif[orientation] == 6: + image = image.rotate(270, expand=True) + elif exif[orientation] == 8: + image = image.rotate(90, expand=True) + + thumb = ImageOps.fit(image, (256, 256), Image.ANTIALIAS) + thumb.save(thumb_file_path) + + +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 diff --git a/fet2020/gallery/views.py b/fet2020/gallery/views.py index f453705a..b8a496a5 100644 --- a/fet2020/gallery/views.py +++ b/fet2020/gallery/views.py @@ -1,47 +1,59 @@ import os - from collections import deque +from random import randint + from django.http import Http404, HttpResponse -from django.shortcuts import render +from django.shortcuts import render, redirect +from django.utils.text import slugify +from authentications.decorators import unauthenticated_user, authenticated_user 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] - if ext.lower() not in valid_images: - continue - - img_list.append(f) - - return img_list +from .utils import get_image_list, create_thumbs, get_thumb def index(request): - _base_dir = "/home/project/fet2020/fet2020" - _path = "/static/images/" + _path = "/files/uploads/gallery" + + folders = next(os.walk(_base_dir + _path))[1] if request.user.is_authenticated: - albums = deque(Album.objects.all()) + albums = deque(Album.objects.all().order_by("-event_date")) else: # show only PUBLIC albums ("20" -> PUBLIC) - albums = deque(Album.objects.filter(status="20")) + 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_path) - img_list = __get_image_list(file_path) + file_path = os.path.join(_base_dir + _path, album.folder_name) + img_list = get_image_list(file_path) if img_list: - album.thumbnail = img_list[0] + album.thumbnail = get_thumb( + album.folder_name, img_list[randint(0, len(img_list) - 1)] + ) else: albums.remove(album) + if request.user.is_authenticated: + for folder in folders: + if not Album.objects.filter(folder_name=folder): + album = Album( + title=folder, + slug=slugify(folder), + folder_name=folder, + event_date=None, + ) + + 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)] + ) + + albums.append(album) + context = { "albums": albums, "path": _path, @@ -51,16 +63,28 @@ def index(request): def show_album(request, slug): - _base_dir = "/home/project/fet2020/fet2020" - _path = "/static/images/" + _path = "/files/uploads/gallery" album = Album.objects.filter(slug=slug).first() if not album: - raise Http404("wrong album slug") + folders = next(os.walk(_base_dir + _path))[1] + for folder in folders: + if slug in slugify(folder): + album = Album( + title=folder, + slug=slugify(folder), + folder_name=folder, + event_date=None, + ) + break + else: + raise Http404("wrong album slug") - file_path = os.path.join(_base_dir + _path, album.folder_path) - img_list = __get_image_list(file_path) + file_path = os.path.join(_base_dir + _path, album.folder_name) + img_list = get_image_list(file_path) + + create_thumbs(album.folder_name) context = { "album": album, @@ -69,3 +93,8 @@ def show_album(request, slug): } return render(request, "gallery/album.html", context) + + +@authenticated_user +def show_draft_album(request, slug): + return show_album(request, slug) diff --git a/fet2020/templates/gallery/album.html b/fet2020/templates/gallery/album.html index 6e23d325..cde48eaa 100644 --- a/fet2020/templates/gallery/album.html +++ b/fet2020/templates/gallery/album.html @@ -1,6 +1,5 @@ {% extends 'layout.html' %} {% load static %} -{% load thumbnail %} {% block galleryheader %} @@ -9,24 +8,38 @@ {% block content %}
-
+ {% if request.user.is_authenticated %} + {% if album.id %} + + {% else %} + + {% endif %} + {% endif %} +
+ {{ album.title }}
{% if album.photographer %} Fotograf: {{ album.photographer | capfirst }}
{% endif %} - Datum: {{ album.public_date }}
+ {% if album.event_date %} + Datum: {{ album.event_date }}
+ {% endif %} {% if album.status == "10" %} - DRAFT
+ DRAFT
{% endif %} {% if album.description %}

- {{ album.description }} + {{ album.description|safe }} {% endif %}

@@ -45,8 +58,8 @@ diff --git a/fet2020/templates/gallery/index.html b/fet2020/templates/gallery/index.html index 8a9419e7..e2d63ceb 100644 --- a/fet2020/templates/gallery/index.html +++ b/fet2020/templates/gallery/index.html @@ -4,20 +4,30 @@ {% block content %}
+
+ {% if request.user.is_authenticated %} + + {% endif %} +
+
- {% for album in albums %} -
-{% endblock %} \ No newline at end of file +{% endblock %}