add dict for image and move path strings to settings
This commit is contained in:
@@ -14,6 +14,7 @@ env = environ.Env(
|
|||||||
ETHERPAD_PORT=(str, "9001"),
|
ETHERPAD_PORT=(str, "9001"),
|
||||||
ETHERPAD_HOST=(str, ""),
|
ETHERPAD_HOST=(str, ""),
|
||||||
ETHERPAD_GROUP=(str, ""),
|
ETHERPAD_GROUP=(str, ""),
|
||||||
|
GALLERY_PATH=(str, "uploads/gallery"),
|
||||||
)
|
)
|
||||||
|
|
||||||
# Prints and logs are written to console
|
# Prints and logs are written to console
|
||||||
@@ -330,3 +331,10 @@ LOGIN_URL = "/auth/login"
|
|||||||
|
|
||||||
# MIGRATION FROM DJANGO 3.1 TO DJANGO 3.2
|
# MIGRATION FROM DJANGO 3.1 TO DJANGO 3.2
|
||||||
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
|
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
|
||||||
|
|
||||||
|
|
||||||
|
# GALLERY
|
||||||
|
GALLERY = {
|
||||||
|
"path": env("GALLERY_PATH"),
|
||||||
|
"thumb_path": env("GALLERY_PATH") + "_thumb",
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,47 +2,74 @@ import logging
|
|||||||
import os
|
import os
|
||||||
from PIL import Image, ExifTags, ImageOps
|
from PIL import Image, ExifTags, ImageOps
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
gallery_path = settings.GALLERY["path"]
|
||||||
|
gallery_thumb_path = settings.GALLERY["thumb_path"]
|
||||||
valid_images = [".jpg", ".png"]
|
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 = []
|
img_list = []
|
||||||
|
|
||||||
if os.path.exists(file_path):
|
if os.path.exists(file_path):
|
||||||
for f in os.listdir(file_path):
|
for img in os.listdir(file_path):
|
||||||
ext = os.path.splitext(f)[1]
|
ext = os.path.splitext(img)[1]
|
||||||
if ext.lower() not in valid_images:
|
if ext.lower() not in valid_images:
|
||||||
continue
|
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
|
return img_list
|
||||||
|
|
||||||
|
|
||||||
def create_thumbs(folder_path):
|
def get_folder_list():
|
||||||
_base_dir = "/home/project/fet2020/fet2020"
|
return next(os.walk(settings.MEDIA_ROOT + "/" + gallery_path))[1]
|
||||||
_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)
|
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):
|
if os.path.exists(file_path):
|
||||||
os.makedirs(thumb_path, exist_ok=True)
|
os.makedirs(thumb_path, exist_ok=True)
|
||||||
|
|
||||||
for f in os.listdir(file_path):
|
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]
|
ext = os.path.splitext(f)[1]
|
||||||
if ext.lower() not in valid_images:
|
if ext.lower() not in valid_images:
|
||||||
continue
|
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)
|
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:
|
with Image.open(str(image_path), "r") as image:
|
||||||
if image._getexif() is not None:
|
if image._getexif() is not None:
|
||||||
@@ -58,18 +85,6 @@ def create_thumbs(folder_path):
|
|||||||
elif exif[orientation] == 8:
|
elif exif[orientation] == 8:
|
||||||
image = image.rotate(90, expand=True)
|
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)
|
thumb.save(thumb_file_path)
|
||||||
logger.info(f"Thumb {thumb} wird gespeichert.")
|
logger.info(f"Save thumb 'thumb_{f}'.")
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|||||||
@@ -2,40 +2,43 @@ import os
|
|||||||
from collections import deque
|
from collections import deque
|
||||||
from random import randint
|
from random import randint
|
||||||
|
|
||||||
from django.http import Http404, HttpResponse
|
from django.conf import settings
|
||||||
from django.shortcuts import render, redirect
|
from django.http import Http404
|
||||||
|
from django.shortcuts import render
|
||||||
from django.utils.text import slugify
|
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 .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):
|
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:
|
if request.user.is_authenticated:
|
||||||
albums = deque(Album.objects.all().order_by("-event_date"))
|
albums = deque(Album.objects.all().order_by("-event_date"))
|
||||||
else:
|
else:
|
||||||
# show only PUBLIC albums ("20" -> PUBLIC)
|
# show only PUBLIC albums ("20" -> PUBLIC).
|
||||||
albums = deque(Album.objects.filter(status="20").order_by("-event_date"))
|
albums = deque(Album.objects.filter(status="20").order_by("-event_date"))
|
||||||
|
|
||||||
for album in list(albums):
|
for album in list(albums):
|
||||||
if not album.thumbnail:
|
img_list = get_image_list(album.folder_name)
|
||||||
file_path = os.path.join(_base_dir + _path, album.folder_name)
|
if img_list:
|
||||||
img_list = get_image_list(file_path)
|
if album.thumbnail:
|
||||||
if img_list:
|
for img in img_list:
|
||||||
album.thumbnail = get_thumb(
|
if album.thumbnail in img["title"]:
|
||||||
album.folder_name, img_list[randint(0, len(img_list) - 1)]
|
album.thumbnail = img["thumb_url"]
|
||||||
)
|
break
|
||||||
|
else:
|
||||||
|
value = randint(0, len(img_list) - 1)
|
||||||
|
album.thumbnail = img_list[value]["thumb_url"]
|
||||||
else:
|
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:
|
if request.user.is_authenticated:
|
||||||
for folder in folders:
|
for folder in get_folder_list():
|
||||||
if not Album.objects.filter(folder_name=folder):
|
if not Album.objects.filter(folder_name=folder):
|
||||||
album = Album(
|
album = Album(
|
||||||
title=folder,
|
title=folder,
|
||||||
@@ -44,32 +47,23 @@ def index(request):
|
|||||||
event_date=None,
|
event_date=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
file_path = os.path.join(_base_dir + _path, album.folder_name)
|
img_list = get_image_list(album.folder_name)
|
||||||
img_list = get_image_list(file_path)
|
|
||||||
|
|
||||||
if img_list:
|
if img_list:
|
||||||
album.thumbnail = get_thumb(
|
value = randint(0, len(img_list) - 1)
|
||||||
album.folder_name, img_list[randint(0, len(img_list) - 1)]
|
album.thumbnail = img_list[value]["thumb_url"]
|
||||||
)
|
albums.append(album)
|
||||||
|
|
||||||
albums.append(album)
|
|
||||||
|
|
||||||
context = {
|
context = {
|
||||||
"albums": albums,
|
"albums": albums,
|
||||||
"path": _path,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return render(request, "gallery/index.html", context)
|
return render(request, "gallery/index.html", context)
|
||||||
|
|
||||||
|
|
||||||
def show_album(request, slug):
|
def show_album(request, slug):
|
||||||
_base_dir = "/home/project/fet2020/fet2020"
|
|
||||||
_path = "/files/uploads/gallery"
|
|
||||||
|
|
||||||
album = Album.objects.filter(slug=slug).first()
|
album = Album.objects.filter(slug=slug).first()
|
||||||
if not album:
|
if not album:
|
||||||
folders = next(os.walk(_base_dir + _path))[1]
|
for folder in get_folder_list():
|
||||||
for folder in folders:
|
|
||||||
if slug in slugify(folder):
|
if slug in slugify(folder):
|
||||||
album = Album(
|
album = Album(
|
||||||
title=folder,
|
title=folder,
|
||||||
@@ -81,15 +75,12 @@ def show_album(request, slug):
|
|||||||
else:
|
else:
|
||||||
raise Http404("wrong album slug")
|
raise Http404("wrong album slug")
|
||||||
|
|
||||||
file_path = os.path.join(_base_dir + _path, album.folder_name)
|
img_list = get_image_list(album.folder_name)
|
||||||
img_list = get_image_list(file_path)
|
|
||||||
|
|
||||||
create_thumbs(album.folder_name)
|
create_thumbs(album.folder_name)
|
||||||
|
|
||||||
context = {
|
context = {
|
||||||
"album": album,
|
"album": album,
|
||||||
"images": img_list,
|
"images": img_list,
|
||||||
"path": _path,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return render(request, "gallery/album.html", context)
|
return render(request, "gallery/album.html", context)
|
||||||
|
|||||||
@@ -59,8 +59,12 @@
|
|||||||
|
|
||||||
<div id="links" class="grid-x">
|
<div id="links" class="grid-x">
|
||||||
{% for image in images %}
|
{% for image in images %}
|
||||||
<a id="{{ image }}" href="{{ path }}/{{ album.folder_name }}/{{ image }}" title="{{ image }}" class="cell large-2 medium-3 small-12">
|
<a id="{{ image.title }}" href="{{ image.image_url }}" title="{{ image.title }}" class="cell large-2 medium-3 small-12">
|
||||||
<img src="{{ path }}_thumb/{{ album.folder_name }}/thumb_{{ image }}" alt="{{ image }}">
|
{% if image.thumb_url %}
|
||||||
|
<img src="{{ image.thumb_url }}" alt="{{ image.title }}">
|
||||||
|
{% else %}
|
||||||
|
<img src="{{ image.image_url }}" alt="{{ image.title }}">
|
||||||
|
{% endif %}
|
||||||
</a>
|
</a>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -21,7 +21,7 @@
|
|||||||
{% else %}
|
{% else %}
|
||||||
<a href="{% url 'album' album.slug %}">
|
<a href="{% url 'album' album.slug %}">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<img src="{{ path }}_thumb/{{ album.folder_name }}/{{ album.thumbnail }}">
|
<img src="{{ album.thumbnail }}">
|
||||||
{{ album.title }}
|
{{ album.title }}
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user