get photos from server, optimize creating thumbs, add buttons
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -6,4 +6,5 @@ from . import views
|
||||
urlpatterns = [
|
||||
path("", views.index, name="gallery"),
|
||||
path("<slug:slug>/", views.show_album, name="album"),
|
||||
path("draft/<slug:slug>/", views.show_draft_album, name="draft-album"),
|
||||
]
|
||||
|
||||
74
fet2020/gallery/utils.py
Normal file
74
fet2020/gallery/utils.py
Normal file
@@ -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
|
||||
@@ -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)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
{% extends 'layout.html' %}
|
||||
{% load static %}
|
||||
{% load thumbnail %}
|
||||
|
||||
{% block galleryheader %}
|
||||
<link rel="stylesheet" href="{% static 'Gallery-3.3.0/css/blueimp-gallery.min.css' %}">
|
||||
@@ -9,24 +8,38 @@
|
||||
{% block content %}
|
||||
|
||||
<div class="grid-container">
|
||||
|
||||
<div class="grid-x padding-top-1">
|
||||
<a class="button" href="{% url 'gallery' %}">Zurück</a>
|
||||
</div>
|
||||
<hr>
|
||||
|
||||
<div class="grid-x">
|
||||
{% if request.user.is_authenticated %}
|
||||
{% if album.id %}
|
||||
<div class="cell large-3 medium-4 small-12">
|
||||
<a class="button" href="{% url 'admin:gallery_album_change' album.id %}">Album bearbeiten</a>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="cell large-3 medium-4 small-12">
|
||||
<a class="button" href="{% url 'admin:gallery_album_add' %}">neues Album hinzufügen</a>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
|
||||
<div class="cell">
|
||||
{{ album.title }}<br>
|
||||
{% if album.photographer %}
|
||||
Fotograf:<a href="{% url 'member' album.photographer.id %}"> {{ album.photographer | capfirst }}</a><br>
|
||||
{% endif %}
|
||||
Datum: {{ album.public_date }}<br>
|
||||
{% if album.event_date %}
|
||||
Datum: {{ album.event_date }}<br>
|
||||
{% endif %}
|
||||
{% if album.status == "10" %}
|
||||
DRAFT<br>
|
||||
DRAFT<br>
|
||||
{% endif %}
|
||||
{% if album.description %}
|
||||
<p>
|
||||
{{ album.description }}
|
||||
{{ album.description|safe }}
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
@@ -45,8 +58,8 @@
|
||||
|
||||
<div id="links" class="grid-x">
|
||||
{% for image in images %}
|
||||
<a id="{{ image }}" href="{{ path }}{{ album.folder_path }}/{{ image }}" title="{{ image }}" class="cell large-2 medium-3 small-12">
|
||||
<img src="{{ path }}{{ album.folder_path }}/{{ image }}" alt="{{ image }}">
|
||||
<a id="{{ image }}" href="{{ path }}/{{ album.folder_name }}/{{ image }}" title="{{ image }}" class="cell large-2 medium-3 small-12">
|
||||
<img src="{{ path }}_thumb/{{ album.folder_name }}/thumb_{{ image }}" alt="{{ image }}">
|
||||
</a>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
@@ -4,19 +4,29 @@
|
||||
{% block content %}
|
||||
|
||||
<div class="grid-container">
|
||||
<div class="grid-x padding-top-1">
|
||||
{% if request.user.is_authenticated %}
|
||||
<div class="cell large-3 medium-4 small-12">
|
||||
<a class="button" href="{% url 'admin:gallery_album_add' %}">neues Album hinzufügen</a>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
<div class="grid-x">
|
||||
|
||||
{% for album in albums %}
|
||||
<div class="cell large-2 medium-3 small-12">
|
||||
<div class="cell large-2 medium-3 small-12" {% if album.status == "10" %}style="background-color:red;"{% endif %}>
|
||||
|
||||
<a href="{% url 'album' album.slug %}">
|
||||
<img src="{{ path }}{{ album.folder_path }}/{{ album.thumbnail }}">
|
||||
{{ album.title }}
|
||||
</a>
|
||||
{% if album.status == "10" %}
|
||||
<a href="{% url 'draft-album' album.slug %}">
|
||||
{% else %}
|
||||
<a href="{% url 'album' album.slug %}">
|
||||
{% endif %}
|
||||
<img src="{{ path }}_thumb/{{ album.folder_name }}/{{ album.thumbnail }}">
|
||||
{{ album.title }}
|
||||
</a>
|
||||
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user