photo26/photologue_custom/forms.py
2021-10-15 12:43:17 +02:00

46 lines
1.5 KiB
Python

from django import forms
from django.utils.translation import gettext_lazy as _
from photologue.models import Gallery
class UploadForm(forms.Form):
file_field = forms.FileField(
label="",
widget=forms.FileInput(attrs={
'accept': 'image/*',
'multiple': True,
'class': 'mb-3',
}),
)
gallery = forms.ModelChoiceField(
Gallery.objects.all(),
label=_('Gallery'),
required=False,
help_text=_('Select a gallery to add these images to. Leave this empty to '
'create a new gallery from the supplied title.')
)
new_gallery_title = forms.CharField(
label=_('New gallery title'),
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))