aboutsummaryrefslogtreecommitdiffstats
path: root/pyblackbird_cc/resources/views.py
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-08-03 21:39:57 +0100
committerMatthew Lemon <y@yulqen.org>2024-08-03 21:39:57 +0100
commitfd1bc6777df5b4c85c899e3bcdd1293a6bead630 (patch)
treef4fbb16bfdb6c5883df64d01e8bd4ea20a2875c5 /pyblackbird_cc/resources/views.py
parenta06c426edc3bb33deab2e55c2dcd5f5c3b2f3504 (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/views.py')
-rw-r--r--pyblackbird_cc/resources/views.py50
1 files changed, 48 insertions, 2 deletions
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})