diff options
author | Matthew Lemon <y@yulqen.org> | 2024-08-03 21:39:57 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-08-03 21:39:57 +0100 |
commit | fd1bc6777df5b4c85c899e3bcdd1293a6bead630 (patch) | |
tree | f4fbb16bfdb6c5883df64d01e8bd4ea20a2875c5 /pyblackbird_cc/resources/forms.py | |
parent | a06c426edc3bb33deab2e55c2dcd5f5c3b2f3504 (diff) |
Add feature to add PDFs to resources
Implemented the ability to upload and manage PDFs for resources. Added the necessary form, view, and templates to support this functionality. Updated routes and UI elements to integrate the new feature seamlessly.
Diffstat (limited to '')
-rw-r--r-- | pyblackbird_cc/resources/forms.py | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/pyblackbird_cc/resources/forms.py b/pyblackbird_cc/resources/forms.py index ed2c1a6..5a4a606 100644 --- a/pyblackbird_cc/resources/forms.py +++ b/pyblackbird_cc/resources/forms.py @@ -181,6 +181,46 @@ class ResourceUpdateMetadataForm(forms.ModelForm): ] +class ResourceUpdatePDFsForm(forms.Form): + def __init__(self, *args, **kwargs): + try: + self.resource = kwargs.pop("resource") + except KeyError: + pass + super().__init__(*args, **kwargs) + self.helper = FormHelper(self) + self.helper.add_input(Submit("submit", "Submit")) + + pdf_files = forms.FileField( + widget=forms.TextInput( + attrs={ + "multiple": True, + "type": "File", + "required": True, + }, + ), + required=False, + help_text="You can multi-select up to 20 .pdf files here.", + label="PDF files", + ) + + pdf_files.widget.attrs.update({"class": "file_upload", "accept": ".pdf"}) + + def clean_pdf_files(self): + pdf_files = self.files.getlist("pdf_files") + if not pdf_files: + raise forms.ValidationError("Please select at least one PDF file.") + acceptable = ["application/pdf"] + for f in pdf_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 PDF files.") + if len(pdf_files) > ALLOWED_PDFS: + raise forms.ValidationError("Please select up to 20 PDF files.") + return pdf_files + + class ResourceUpdateThumbnailsForm(forms.Form): def __init__(self, *args, **kwargs): try: @@ -218,4 +258,3 @@ class ResourceUpdateThumbnailsForm(forms.Form): if len(thumbnail_files) > ALLOWED_THUMBNAILS: raise forms.ValidationError("Please select up to 5 files.") return thumbnail_files - |