diff options
Diffstat (limited to 'pyblackbird_cc/resources/forms.py')
-rw-r--r-- | pyblackbird_cc/resources/forms.py | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/pyblackbird_cc/resources/forms.py b/pyblackbird_cc/resources/forms.py index 1a5b51a..4b7df38 100644 --- a/pyblackbird_cc/resources/forms.py +++ b/pyblackbird_cc/resources/forms.py @@ -183,7 +183,10 @@ class ResourceUpdateMetadataForm(forms.ModelForm): class ResourceUpdateThumbnailsForm(forms.Form): def __init__(self, *args, **kwargs): - self.resource = kwargs.pop("resource") + try: + self.resource = kwargs.pop("resource") + except KeyError: + pass super().__init__(*args, **kwargs) self.helper = FormHelper(self) self.helper.add_input(Submit("submit", "Submit")) @@ -197,11 +200,22 @@ class ResourceUpdateThumbnailsForm(forms.Form): }, ), required=False, - label="Cover images", - help_text="Your cover image will be displayed in the search results and as " - "the first image on your resource page in the preview function. " - "It is important to add an eye catching cover image that gives " - "other teachers an idea about what your resource contains. " - "You can multi-select up to 5 .png or .jpg files here.", + label="Thumbnail files", + help_text="You can provide X number of files here." ) + thumbnail_files.widget.attrs.update({"class": "file_upload", "accept": ".png,.jpg"}) + + def clean_thumbnail_files(self): + thumbnail_files = self.files.getlist("thumbnail_files") + if not thumbnail_files: + raise forms.ValidationError("Please select at least one thumbnail file.") + acceptable = ["image/png", "image/jpeg"] + for f in thumbnail_files: + content_type = magic.from_buffer(f.file.read(), mime=True) + f.file.seek(0) + if content_type not in acceptable: + raise forms.ValidationError("Please select only PNG or JPG files.") + if len(thumbnail_files) > ALLOWED_THUMBNAILS: + raise forms.ValidationError("Please select up to 5 files.") + return thumbnail_files |