Check upload form

This commit is contained in:
Alexandre Iooss 2021-10-15 12:43:17 +02:00
parent 727387566d
commit d2fa5ce02f
5 changed files with 54 additions and 22 deletions

View file

@ -6,7 +6,11 @@ from photologue.models import Gallery
class UploadForm(forms.Form):
file_field = forms.FileField(
label="",
widget=forms.ClearableFileInput(attrs={'multiple': True}),
widget=forms.FileInput(attrs={
'accept': 'image/*',
'multiple': True,
'class': 'mb-3',
}),
)
gallery = forms.ModelChoiceField(
Gallery.objects.all(),
@ -20,3 +24,23 @@ class UploadForm(forms.Form):
max_length=250,
required=False,
)
def clean_new_gallery_title(self):
title = self.cleaned_data['new_gallery_title']
if title and Gallery.objects.filter(title=title).exists():
raise forms.ValidationError(_('A gallery with that title already exists.'))
return title
def clean(self):
cleaned_data = super().clean()
# Check that either an existing gallery is chosen, or new_gallery_title is filled
if not (bool(cleaned_data['gallery']) ^ bool(cleaned_data.get('new_gallery_title', None))):
raise forms.ValidationError(
_('Select an existing gallery, or enter a title for a new gallery.'))
return cleaned_data
def save(self, files):
# TODO: upload
print(type(files))