aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pyblackbird_cc/resources/forms.py41
-rw-r--r--pyblackbird_cc/resources/urls.py1
-rw-r--r--pyblackbird_cc/resources/views.py50
-rw-r--r--pyblackbird_cc/templates/resources/resource_card.html2
-rw-r--r--pyblackbird_cc/templates/resources/resource_detail.html3
-rw-r--r--pyblackbird_cc/templates/resources/update_pdfs.html29
6 files changed, 120 insertions, 6 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})
diff --git a/pyblackbird_cc/templates/resources/resource_card.html b/pyblackbird_cc/templates/resources/resource_card.html
index 687438c..a7b03e3 100644
--- a/pyblackbird_cc/templates/resources/resource_card.html
+++ b/pyblackbird_cc/templates/resources/resource_card.html
@@ -39,6 +39,8 @@
class="btn btn-outline-primary btn-sm">Edit</a>
<a href="{% url 'resources:resource_update_thumbnails' resource.id %}"
class="btn btn-outline-primary btn-sm">Edit Feature Images</a>
+ <a href="{% url 'resources:resource_update_pdfs' resource.id %}"
+ class="btn btn-outline-primary btn-sm">Add PDFs</a>
</div>
{% endif %}
</div> \ No newline at end of file
diff --git a/pyblackbird_cc/templates/resources/resource_detail.html b/pyblackbird_cc/templates/resources/resource_detail.html
index 4a513af..67694fa 100644
--- a/pyblackbird_cc/templates/resources/resource_detail.html
+++ b/pyblackbird_cc/templates/resources/resource_detail.html
@@ -31,9 +31,6 @@
class="text-danger">Edit this resource</a>
{% endif %}
</div>
- <div>
- {% if request.user.is_authenticated %}<a href="#" class="text-danger">Replace the PDFs</a>{% endif %}
- </div>
</div>
</div>
</div>
diff --git a/pyblackbird_cc/templates/resources/update_pdfs.html b/pyblackbird_cc/templates/resources/update_pdfs.html
new file mode 100644
index 0000000..f5dab72
--- /dev/null
+++ b/pyblackbird_cc/templates/resources/update_pdfs.html
@@ -0,0 +1,29 @@
+{% extends "base.html" %}
+{% load static %}
+{% load crispy_forms_tags %}
+
+{% block content %}
+ <div class="row d-flex justify-content-center">
+ <div class="col bg-light mt-lg-4 p-4 border border-success border-opacity-25 rounded">
+ <h1>Update PDFs for <strong>{{ resource.name }}</strong> resource</h1>
+
+ <p>The current PDFs for this resource are:</p>
+
+ <ul>
+ {% for pdf in resource.get_pdf_file_names %}
+ <li><em>{{ pdf }}</em></li>
+ {% endfor %}
+ </ul>
+
+ <form action="{% url "resources:resource_update_pdfs" resource.id %}"
+ method="post" enctype="multipart/form-data">
+ {% csrf_token %}
+ {% crispy form form.helper %}
+ </form>
+
+ <button class="btn btn-danger" onclick="location.href='{{ resource.get_absolute_url }}';">Cancel</button>
+ </div>
+ </div>
+
+
+{% endblock %}