Change upload path

This commit is contained in:
Alexandre Iooss 2021-12-15 20:01:22 +01:00
parent f5ce880fe2
commit 732b33539a

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 = str(gallery.extended.date_start.year)
gallery_dir = Path('photos') / 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.image.name.split("/")[-1])
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)