From 732b33539a6f8af53080f3b308b3f44c1012f4c1 Mon Sep 17 00:00:00 2001 From: Alexandre Iooss Date: Wed, 15 Dec 2021 20:01:22 +0100 Subject: [PATCH] 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)