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
|
from .models import Gallery, Tag
|
||||||
|
|
||||||
|
|
||||||
class UploadForm(forms.Form):
|
class MultipleFileInput(forms.ClearableFileInput):
|
||||||
file_field = forms.FileField(
|
allow_multiple_selected = True
|
||||||
label="",
|
|
||||||
widget=forms.FileInput(
|
|
||||||
|
class MultipleFileField(forms.FileField):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
kwargs.setdefault(
|
||||||
|
"widget",
|
||||||
|
MultipleFileInput(
|
||||||
attrs={
|
attrs={
|
||||||
"accept": "image/*",
|
"accept": "image/*",
|
||||||
"multiple": True,
|
|
||||||
"class": "mb-3",
|
"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 = MultipleFileField(label="")
|
||||||
gallery = forms.ModelChoiceField(
|
gallery = forms.ModelChoiceField(
|
||||||
Gallery.objects.all(),
|
Gallery.objects.all(),
|
||||||
label=_("Gallery"),
|
label=_("Gallery"),
|
||||||
|
|
|
||||||
|
|
@ -188,10 +188,6 @@ class GalleryUpload(PermissionRequiredMixin, FormView):
|
||||||
permission_required = "photologue.add_gallery"
|
permission_required = "photologue.add_gallery"
|
||||||
|
|
||||||
def form_valid(self, form):
|
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
|
# Get or create gallery
|
||||||
gallery = form.get_or_create_gallery()
|
gallery = form.get_or_create_gallery()
|
||||||
gallery_year = Path(str(gallery.date_start.year))
|
gallery_year = Path(str(gallery.date_start.year))
|
||||||
|
|
@ -200,6 +196,7 @@ class GalleryUpload(PermissionRequiredMixin, FormView):
|
||||||
# Upload pictures
|
# Upload pictures
|
||||||
uploaded_photo_name = []
|
uploaded_photo_name = []
|
||||||
already_exists = 0
|
already_exists = 0
|
||||||
|
files = form.cleaned_data["file_field"]
|
||||||
for photo_file in files:
|
for photo_file in files:
|
||||||
# Check that we have a valid image
|
# Check that we have a valid image
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue