diff options
author | Matthew Lemon <y@yulqen.org> | 2024-08-03 16:26:16 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-08-03 16:26:16 +0100 |
commit | 7b757fcd38ca560139aa3c4c86ae57b4c6afe2dd (patch) | |
tree | 76f63bd0f576660154f90dd5f2119edd9b3d1006 | |
parent | a75aca148ba7db29e14e30f2e0707b1efac1ff94 (diff) |
wip: adding the update thumbnails form
-rw-r--r-- | pyblackbird_cc/resources/forms.py | 26 | ||||
-rw-r--r-- | pyblackbird_cc/resources/models.py | 4 | ||||
-rw-r--r-- | pyblackbird_cc/resources/urls.py | 1 | ||||
-rw-r--r-- | pyblackbird_cc/resources/views.py | 19 | ||||
-rw-r--r-- | pyblackbird_cc/templates/resources/resource_detail.html | 2 |
5 files changed, 47 insertions, 5 deletions
diff --git a/pyblackbird_cc/resources/forms.py b/pyblackbird_cc/resources/forms.py index 41ad3c8..1a5b51a 100644 --- a/pyblackbird_cc/resources/forms.py +++ b/pyblackbird_cc/resources/forms.py @@ -179,3 +179,29 @@ class ResourceUpdateMetadataForm(forms.ModelForm): "subcategories", "feature_slot", ] + + +class ResourceUpdateThumbnailsForm(forms.Form): + def __init__(self, *args, **kwargs): + self.resource = kwargs.pop("resource") + super().__init__(*args, **kwargs) + self.helper = FormHelper(self) + self.helper.add_input(Submit("submit", "Submit")) + + thumbnail_files = forms.FileField( + widget=forms.TextInput( + attrs={ + "multiple": True, + "type": "File", + "required": True, + }, + ), + 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.", + ) + diff --git a/pyblackbird_cc/resources/models.py b/pyblackbird_cc/resources/models.py index 05a848c..31085c0 100644 --- a/pyblackbird_cc/resources/models.py +++ b/pyblackbird_cc/resources/models.py @@ -1,4 +1,5 @@ from django.db import models +from django.urls import reverse CURRICULUM_CHOICES = [ ("No curriculum", "No curriculum"), @@ -98,6 +99,9 @@ class Resource(models.Model): sh = [sh for sh in rs.values_list("pdf_page_snapshots__file_name", flat=True)] return [s for s in sh if s] + def get_absolute_url(self): + return reverse("resources:resource_detail", kwargs={"resource_id": self.pk}) + class ResourceType(models.Model): name = models.CharField(max_length=255, null=False) diff --git a/pyblackbird_cc/resources/urls.py b/pyblackbird_cc/resources/urls.py index 01c9e2e..0babe83 100644 --- a/pyblackbird_cc/resources/urls.py +++ b/pyblackbird_cc/resources/urls.py @@ -13,6 +13,7 @@ urlpatterns = [ views.update_resource_metadata, name="resource_update_metadata", ), + path("resource/update-thumbnails/<int:pk>", views.update_resource_thumbnails, name="resource_update_thumbnails"), ] htmx_patterns = [ diff --git a/pyblackbird_cc/resources/views.py b/pyblackbird_cc/resources/views.py index 07227e4..2feb655 100644 --- a/pyblackbird_cc/resources/views.py +++ b/pyblackbird_cc/resources/views.py @@ -17,7 +17,7 @@ from django.shortcuts import redirect from django.shortcuts import render from . import services -from .forms import ResourceCreateForm +from .forms import ResourceCreateForm, ResourceUpdateThumbnailsForm from .forms import ResourceUpdateMetadataForm from .models import PDFPageSnapshot, ResourceSubcategory from .models import PDFResource @@ -185,7 +185,7 @@ def upload_to_s3(pdf_files, thumbnail_files, snapshotted_pages) -> bool: f"thumbnails/{f.name}", ) if _get_pdf_collection_type(snapshotted_pages) == "SINGLE_PDF_SINGLE_PAGE" \ - or _get_pdf_collection_type(snapshotted_pages) == "SINGLE_PDF_MULTI_PAGE": + or _get_pdf_collection_type(snapshotted_pages) == "SINGLE_PDF_MULTI_PAGE": for img in snapshotted_pages[0]: logger.info("Uploading {img} to S3") client.upload_file( @@ -195,7 +195,7 @@ def upload_to_s3(pdf_files, thumbnail_files, snapshotted_pages) -> bool: ) return True if _get_pdf_collection_type(snapshotted_pages) == "MULTI_PDF_SINGLE_PAGE" \ - or _get_pdf_collection_type(snapshotted_pages) == "MULTI_PDF_MULTI_PAGE": + or _get_pdf_collection_type(snapshotted_pages) == "MULTI_PDF_MULTI_PAGE": for pdf in snapshotted_pages: for img in pdf: logger.info("Uploading {img} to S3") @@ -221,7 +221,7 @@ def _write_pdf_to_tempdir(f) -> str: def create_metadata( - pdf_files, + pdf_files, ) -> Generator[tuple[services.PDFMetadata, str], None, None]: with tempfile.TemporaryDirectory() as temp_dir: for pdf_file in pdf_files: @@ -372,6 +372,17 @@ def resource_detail(request, resource_id): return render(request, "resources/resource_detail.html", {"resource": resource}) +def update_resource_thumbnails(request, pk): + resource = get_object_or_404(Resource, pk=pk) + if request.method == "POST": + form = ResourceUpdateThumbnailsForm(request.POST, request.FILES) + + else: + form = ResourceUpdateThumbnailsForm(resource=pk) + + return render(request, "resources/update_thumbnails.html", {"form": form, "resource": resource}) + + @login_required def hx_download_button(request): """ diff --git a/pyblackbird_cc/templates/resources/resource_detail.html b/pyblackbird_cc/templates/resources/resource_detail.html index f6de740..f520340 100644 --- a/pyblackbird_cc/templates/resources/resource_detail.html +++ b/pyblackbird_cc/templates/resources/resource_detail.html @@ -35,7 +35,7 @@ {% if request.user.is_authenticated %}<a href="#" class="text-danger">Replace the PDFs</a>{% endif %} </div> <div> - {% if request.user.is_authenticated %}<a href="#" class="text-danger">Replace the preview images</a>{% endif %} + {% if request.user.is_authenticated %}<a href="{% url "resources:resource_update_thumbnails" resource.id %}" class="text-danger">Replace the preview images</a>{% endif %} </div> </div> </div> |