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 | |
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 'pyblackbird_cc/resources')
-rw-r--r-- | pyblackbird_cc/resources/forms.py | 41 | ||||
-rw-r--r-- | pyblackbird_cc/resources/urls.py | 1 | ||||
-rw-r--r-- | pyblackbird_cc/resources/views.py | 50 |
3 files changed, 89 insertions, 3 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 - diff --git a/pyblackbird_cc/resources/urls.py b/pyblackbird_cc/resources/urls.py index 0babe83..45bfd59 100644 --- a/pyblackbird_cc/resources/urls.py +++ b/pyblackbird_cc/resources/urls.py @@ -14,6 +14,7 @@ urlpatterns = [ name="resource_update_metadata", ), path("resource/update-thumbnails/<int:pk>", views.update_resource_thumbnails, name="resource_update_thumbnails"), + path("resource/update-pdfs/<int:pk>", views.update_resource_pdfs, name="resource_update_pdfs"), ] htmx_patterns = [ diff --git a/pyblackbird_cc/resources/views.py b/pyblackbird_cc/resources/views.py index 4158a16..8958339 100644 --- a/pyblackbird_cc/resources/views.py +++ b/pyblackbird_cc/resources/views.py @@ -14,12 +14,12 @@ from django.shortcuts import redirect from django.shortcuts import render from . import services -from .forms import ResourceCreateForm, ResourceUpdateThumbnailsForm +from .forms import ResourceCreateForm, ResourceUpdateThumbnailsForm, ResourceUpdatePDFsForm from .forms import ResourceUpdateMetadataForm from .models import PDFPageSnapshot, ResourceSubcategory from .models import PDFResource from .models import Resource -from .s3 import get_presigned_obj_url, upload_files_to_s3, upload_to_s3 +from .s3 import get_presigned_obj_url, upload_files_to_s3, upload_to_s3, upload_snapshotted_pages_to_s3 logger = logging.getLogger(__name__) @@ -284,6 +284,7 @@ def resource_detail(request, resource_id): return render(request, "resources/resource_detail.html", {"resource": resource}) +@login_required() def update_resource_thumbnails(request, pk): resource = get_object_or_404(Resource, pk=pk) if request.method == "POST": @@ -337,3 +338,48 @@ def update_resource_metadata(request, pk): # Change resource_id to pk "resources/resource_metadata_update.html", {"form": form, "resource": resource}, ) + + +@login_required() +def update_resource_pdfs(request, pk): + resource = get_object_or_404(Resource, pk=pk) + if request.method == "POST": + form = ResourceUpdatePDFsForm(request.POST, request.FILES) + if form.is_valid(): + pdf_files = form.cleaned_data["pdf_files"] + + metadata_generator = create_metadata(pdf_files) + + snapshotted_pages = [] + + for metadata, snapshot_images in metadata_generator: + # TODO replace or add? This needs to be decided here + pdf_resource = PDFResource.objects.create( + resource=resource, + file_name=os.path.basename(metadata.file_name), + file_size=metadata.file_size, + ) + + for snapshot_image in snapshot_images: + PDFPageSnapshot.objects.create( + name="test", + file_name=os.path.basename(snapshot_image), + pdf_file=pdf_resource, + ) + + snapshotted_pages.append(snapshot_images) + + # Reset the file pointers for pdf_files + for pdf_file in pdf_files: + pdf_file.seek(0) + + upload_files_to_s3(pdf_files, "pdfuploads") + if not upload_snapshotted_pages_to_s3(snapshotted_pages): + raise Exception("Error uploading snapshotted pages to S3") + + return redirect("resources:resource_detail", resource_id=resource.id) + + else: + form = ResourceUpdatePDFsForm(resource=pk) + + return render(request, "resources/update_pdfs.html", {"form": form, "resource": resource}) |