Merge branch 'uploadpath' into 'master'

Change upload path

See merge request bde/photo21!19
This commit is contained in:
erdnaxe 2021-12-15 20:59:51 +01:00
commit 469d95a342
2 changed files with 6 additions and 2 deletions

View file

@ -236,3 +236,4 @@ CRISPY_TEMPLATE_PACK = 'bootstrap4'
# Photologue # Photologue
PHOTOLOGUE_GALLERY_SAMPLE_SIZE = 1 PHOTOLOGUE_GALLERY_SAMPLE_SIZE = 1
PHOTOLOGUE_DIR = '.'

View file

@ -4,6 +4,7 @@
import os import os
import zipfile import zipfile
from io import BytesIO from io import BytesIO
from pathlib import Path
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.mixins import (LoginRequiredMixin, from django.contrib.auth.mixins import (LoginRequiredMixin,
@ -121,10 +122,11 @@ class GalleryUpload(PermissionRequiredMixin, FormView):
# We take files from the request to support multiple upload # We take files from the request to support multiple upload
files = self.request.FILES.getlist('file_field') files = self.request.FILES.getlist('file_field')
gallery = form.get_or_create_gallery() gallery = form.get_or_create_gallery()
gallery_year = Path(str(gallery.extended.date_start.year))
gallery_dir = gallery_year / gallery.slug
failed_upload = 0 failed_upload = 0
for photo_file in files: for photo_file in files:
# Check that we have a valid image # Check that we have a valid image
print(photo_file, type(photo_file))
try: try:
opened = Image.open(photo_file) opened = Image.open(photo_file)
opened.verify() opened.verify()
@ -137,7 +139,8 @@ class GalleryUpload(PermissionRequiredMixin, FormView):
title = f"{gallery.title} - {photo_file.name}" title = f"{gallery.title} - {photo_file.name}"
try: try:
photo = Photo(title=title, slug=slugify(title)) photo = Photo(title=title, slug=slugify(title))
photo.image.save(photo_file.name, photo_file) photo_name = str(gallery_dir / photo_file.name)
photo.image.save(photo_name, photo_file)
photo.save() photo.save()
photo.galleries.set([gallery]) photo.galleries.set([gallery])
PhotoExtended.objects.create(photo=photo, owner=self.request.user) PhotoExtended.objects.create(photo=photo, owner=self.request.user)