Fix multiple files upload
This commit is contained in:
parent
effb56ef1d
commit
b00c2e07fd
2 changed files with 28 additions and 14 deletions
|
|
@ -13,17 +13,34 @@ from django.utils.translation import gettext_lazy as _
|
|||
from .models import Gallery, Tag
|
||||
|
||||
|
||||
class MultipleFileInput(forms.ClearableFileInput):
|
||||
allow_multiple_selected = True
|
||||
|
||||
|
||||
class MultipleFileField(forms.FileField):
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs.setdefault(
|
||||
"widget",
|
||||
MultipleFileInput(
|
||||
attrs={
|
||||
"accept": "image/*",
|
||||
"class": "mb-3",
|
||||
}
|
||||
),
|
||||
)
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def clean(self, data, initial=None):
|
||||
single_file_clean = super().clean
|
||||
if isinstance(data, (list, tuple)):
|
||||
result = [single_file_clean(d, initial) for d in data]
|
||||
else:
|
||||
result = single_file_clean(data, initial)
|
||||
return result
|
||||
|
||||
|
||||
class UploadForm(forms.Form):
|
||||
file_field = forms.FileField(
|
||||
label="",
|
||||
widget=forms.FileInput(
|
||||
attrs={
|
||||
"accept": "image/*",
|
||||
"multiple": True,
|
||||
"class": "mb-3",
|
||||
}
|
||||
),
|
||||
)
|
||||
file_field = MultipleFileField(label="")
|
||||
gallery = forms.ModelChoiceField(
|
||||
Gallery.objects.all(),
|
||||
label=_("Gallery"),
|
||||
|
|
|
|||
|
|
@ -188,10 +188,6 @@ class GalleryUpload(PermissionRequiredMixin, FormView):
|
|||
permission_required = "photologue.add_gallery"
|
||||
|
||||
def form_valid(self, form):
|
||||
# Upload photos
|
||||
# We take files from the request to support multiple upload
|
||||
files = self.request.FILES.getlist("file_field")
|
||||
|
||||
# Get or create gallery
|
||||
gallery = form.get_or_create_gallery()
|
||||
gallery_year = Path(str(gallery.date_start.year))
|
||||
|
|
@ -200,6 +196,7 @@ class GalleryUpload(PermissionRequiredMixin, FormView):
|
|||
# Upload pictures
|
||||
uploaded_photo_name = []
|
||||
already_exists = 0
|
||||
files = form.cleaned_data["file_field"]
|
||||
for photo_file in files:
|
||||
# Check that we have a valid image
|
||||
try:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue