From 732b33539a6f8af53080f3b308b3f44c1012f4c1 Mon Sep 17 00:00:00 2001 From: Alexandre Iooss Date: Wed, 15 Dec 2021 20:01:22 +0100 Subject: [PATCH 1/3] Change upload path --- photologue_custom/views.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/photologue_custom/views.py b/photologue_custom/views.py index 81df13d..7853658 100644 --- a/photologue_custom/views.py +++ b/photologue_custom/views.py @@ -4,6 +4,7 @@ import os import zipfile from io import BytesIO +from pathlib import Path from django.contrib import messages 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 files = self.request.FILES.getlist('file_field') gallery = form.get_or_create_gallery() + gallery_year = str(gallery.extended.date_start.year) + gallery_dir = Path('photos') / gallery_year / gallery.slug failed_upload = 0 for photo_file in files: # Check that we have a valid image - print(photo_file, type(photo_file)) try: opened = Image.open(photo_file) opened.verify() @@ -137,7 +139,8 @@ class GalleryUpload(PermissionRequiredMixin, FormView): title = f"{gallery.title} - {photo_file.name}" try: photo = Photo(title=title, slug=slugify(title)) - photo.image.save(photo_file.name, photo_file) + photo_name = str(gallery_dir / photo.image.name.split("/")[-1]) + photo.image.save(photo_name, photo_file) photo.save() photo.galleries.set([gallery]) PhotoExtended.objects.create(photo=photo, owner=self.request.user) From 1ffc0934dd9f6ba3a3bad2615aa8608de8d0b303 Mon Sep 17 00:00:00 2001 From: Alexandre Iooss Date: Wed, 15 Dec 2021 20:29:45 +0100 Subject: [PATCH 2/3] Use uploaded filename for destination --- photologue_custom/views.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/photologue_custom/views.py b/photologue_custom/views.py index 7853658..04bfa69 100644 --- a/photologue_custom/views.py +++ b/photologue_custom/views.py @@ -122,8 +122,8 @@ class GalleryUpload(PermissionRequiredMixin, FormView): # We take files from the request to support multiple upload files = self.request.FILES.getlist('file_field') gallery = form.get_or_create_gallery() - gallery_year = str(gallery.extended.date_start.year) - gallery_dir = Path('photos') / gallery_year / gallery.slug + gallery_year = Path(str(gallery.extended.date_start.year)) + gallery_dir = gallery_year / gallery.slug failed_upload = 0 for photo_file in files: # Check that we have a valid image @@ -139,7 +139,7 @@ class GalleryUpload(PermissionRequiredMixin, FormView): title = f"{gallery.title} - {photo_file.name}" try: photo = Photo(title=title, slug=slugify(title)) - photo_name = str(gallery_dir / photo.image.name.split("/")[-1]) + photo_name = str(gallery_dir / photo_file.name) photo.image.save(photo_name, photo_file) photo.save() photo.galleries.set([gallery]) From a198752b2750c5600840ed60053fc6568861f60e Mon Sep 17 00:00:00 2001 From: Alexandre Iooss Date: Wed, 15 Dec 2021 20:55:06 +0100 Subject: [PATCH 3/3] Change photologue base directory --- photo21/settings.py | 1 + 1 file changed, 1 insertion(+) diff --git a/photo21/settings.py b/photo21/settings.py index 7f8299a..62eb8ec 100644 --- a/photo21/settings.py +++ b/photo21/settings.py @@ -236,3 +236,4 @@ CRISPY_TEMPLATE_PACK = 'bootstrap4' # Photologue PHOTOLOGUE_GALLERY_SAMPLE_SIZE = 1 +PHOTOLOGUE_DIR = '.'