Better upload system using parallelisme
This commit is contained in:
parent
debf87421f
commit
f221740228
3 changed files with 207 additions and 131 deletions
|
|
@ -12,7 +12,7 @@ from pathlib import Path
|
|||
from django.contrib import messages
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
|
||||
from django.core.mail import mail_admins
|
||||
from django.db import IntegrityError
|
||||
from django.db import transaction
|
||||
from django.http import HttpResponse
|
||||
from django.http import JsonResponse
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
|
|
@ -275,6 +275,13 @@ class GalleryUpload(PermissionRequiredMixin, FormView):
|
|||
success_url = reverse_lazy("photologue:pl-gallery-upload")
|
||||
permission_required = "photologue.add_gallery"
|
||||
|
||||
def form_invalid(self, form):
|
||||
if not self.request.accepts("text/html") and self.request.accepts("application/json"):
|
||||
errors = {field: list(errs) for field, errs in form.errors.items()}
|
||||
return JsonResponse({"code": 400, "error": errors}, status=400)
|
||||
return super().form_invalid(form)
|
||||
|
||||
|
||||
def form_valid(self, form):
|
||||
# Get or create gallery
|
||||
|
||||
|
|
@ -312,22 +319,42 @@ class GalleryUpload(PermissionRequiredMixin, FormView):
|
|||
jsondata["error"] = f"{photo_file.name} was not recognized as an image"
|
||||
continue
|
||||
|
||||
title = f"{gallery.title} - {photo_file.name}"
|
||||
try:
|
||||
photo = Photo(
|
||||
title=title,
|
||||
slug=slugify(title),
|
||||
owner=self.request.user,
|
||||
)
|
||||
photo_name = str(gallery_dir / photo_file.name)
|
||||
title = Path(photo_file.name).stem
|
||||
|
||||
already_exist = Photo.objects.filter(
|
||||
title=title,
|
||||
owner=self.request.user,
|
||||
galleries=gallery
|
||||
).exists()
|
||||
|
||||
if already_exist:
|
||||
already_exists += 1
|
||||
continue
|
||||
|
||||
# Find a uniq slug
|
||||
base_slug = slugify(title)
|
||||
slug = base_slug
|
||||
counter = 2
|
||||
while Photo.objects.filter(slug=slug).exists():
|
||||
slug = f"{base_slug}-{counter}"
|
||||
counter += 1
|
||||
photo = Photo(
|
||||
title=title,
|
||||
slug=slug,
|
||||
owner=self.request.user,
|
||||
)
|
||||
photo_name = str(gallery_dir / photo_file.name)
|
||||
|
||||
# Save photo and associate it with the gallery in a single database operation
|
||||
# Defer image file saving until the database commit succeeds
|
||||
with transaction.atomic():
|
||||
photo.save()
|
||||
photo.galleries.set([gallery])
|
||||
|
||||
# Save to disk after successful database edit
|
||||
photo.image.save(photo_name, photo_file)
|
||||
except IntegrityError:
|
||||
already_exists += 1
|
||||
continue
|
||||
def save_file():
|
||||
photo.image.save(photo_name, photo_file)
|
||||
|
||||
transaction.on_commit(save_file)
|
||||
|
||||
uploaded_photo_name.append(photo_file.name)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue