Handle database integrity error during upload

This commit is contained in:
Alexandre Iooss 2021-10-15 14:45:26 +02:00
parent 2334017abf
commit baf70d396c
3 changed files with 26 additions and 19 deletions

View file

@ -3,9 +3,9 @@ import datetime
from django import forms
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _
from photologue.models import Gallery, Photo
from photologue.models import Gallery
from .models import GalleryExtended, PhotoExtended
from .models import GalleryExtended
class UploadForm(forms.Form):
@ -71,16 +71,3 @@ class UploadForm(forms.Form):
date_end=self.cleaned_data['date_end'],
)
return gallery
def save(self, user, files):
"""
Save gallery then photos
"""
gallery = self.get_or_create_gallery()
for photo_file in files:
title = f"{gallery.title} - {photo_file.name}"
photo = Photo(title=title, slug=slugify(title))
photo.image.save(photo_file.name, photo_file)
photo.save()
photo.galleries.set([gallery])
PhotoExtended.objects.create(photo=photo, owner=user)