aboutsummaryrefslogtreecommitdiffstats
path: root/pyblackbird_cc/resources/views.py
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-09-08 13:59:10 +0100
committerMatthew Lemon <y@yulqen.org>2024-09-08 13:59:10 +0100
commitdb531eaa027d2abcfcceabfef4b8aa9dc5ca1e5d (patch)
tree47dc0044d1a50c8ffbe689ab9f09ed3bda296ae8 /pyblackbird_cc/resources/views.py
parent50c7d5b46a9e25390ab4c4ff5dcb944a12df2bef (diff)
wip: implementing checkout
- also some formatting changes
Diffstat (limited to 'pyblackbird_cc/resources/views.py')
-rw-r--r--pyblackbird_cc/resources/views.py46
1 files changed, 26 insertions, 20 deletions
diff --git a/pyblackbird_cc/resources/views.py b/pyblackbird_cc/resources/views.py
index 1f68768..f802350 100644
--- a/pyblackbird_cc/resources/views.py
+++ b/pyblackbird_cc/resources/views.py
@@ -15,12 +15,19 @@ from django.shortcuts import redirect
from django.shortcuts import render
from . import services
-from .forms import ResourceCreateForm, ResourceUpdateThumbnailsForm, ResourceUpdatePDFsForm
+from .forms import ResourceCreateForm
from .forms import ResourceUpdateMetadataForm
-from .models import PDFPageSnapshot, ResourceSubcategory, ResourceCategory
+from .forms import ResourceUpdatePDFsForm
+from .forms import ResourceUpdateThumbnailsForm
+from .models import PDFPageSnapshot
from .models import PDFResource
from .models import Resource
-from .s3 import get_presigned_obj_url, upload_files_to_s3, upload_to_s3, upload_snapshotted_pages_to_s3
+from .models import ResourceCategory
+from .models import ResourceSubcategory
+from .s3 import get_presigned_obj_url
+from .s3 import upload_files_to_s3
+from .s3 import upload_snapshotted_pages_to_s3
+from .s3 import upload_to_s3
logger = logging.getLogger(__name__)
@@ -87,10 +94,7 @@ def _extract_metadata_from_resource(resource_obj) -> ResourceInfo | None:
for f in resource_obj.thumbnail_filenames
]
try:
- if resource_obj.subcategories:
- arc_name = resource_obj.subcategories.name
- else:
- arc_name = None
+ arc_name = resource_obj.subcategories.name if resource_obj.subcategories else None
return ResourceInfo(
id=resource_obj.id,
name=resource_obj.name,
@@ -119,7 +123,7 @@ def _extract_metadata_from_resource(resource_obj) -> ResourceInfo | None:
def index(request):
resource_objs = Resource.objects.all()
categories = ResourceCategory.objects.all()
- category = request.GET.get('category', 'all')
+ category = request.GET.get("category", "all")
resource_list = [_extract_metadata_from_resource(r) for r in resource_objs]
@@ -127,11 +131,11 @@ def index(request):
featured_resources = [r for r in resource_list if r.feature_slot]
featured_resources = sorted(featured_resources, key=lambda resource: resource.feature_slot)
- if category != 'all':
+ if category != "all":
resource_list = [r for r in resource_list if r.main_resource_category_name == category]
paginator = Paginator(resource_list, 20)
- page_number = request.GET.get('page')
+ page_number = request.GET.get("page")
page_obj = paginator.get_page(page_number)
context = {
@@ -157,7 +161,7 @@ def create_metadata(pdf_files) -> Generator[tuple[services.PDFMetadata, str], No
"""
Generates PDF metadata and snapshot images for a list of PDF files.
- This function takes a list of PDF file objects, creates temporary files for each one,
+ This function takes a list of PDF file objects, creates temporary files for each one,
and then uses the `services.get_pdf_metadata_from_path` and `services.export_pages_as_images` functions to extract metadata and snapshot images for each PDF.
The function yields a tuple containing the PDF metadata and a list of snapshot image paths for each PDF file.
@@ -166,7 +170,7 @@ def create_metadata(pdf_files) -> Generator[tuple[services.PDFMetadata, str], No
pdf_files (list[django.core.files.uploadedfile.InMemoryUploadedFile]): A list of PDF file objects.
Yields:
- tuple[services.PDFMetadata, list[str]]: A tuple containing the PDF metadata and a list of snapshot image paths for each PDF file.
+ tuple[servies.PDFMetadata, list[str]]: A tuple containing the PDF metadata and a list of snapshot image paths for each PDF file.
"""
with tempfile.TemporaryDirectory() as temp_dir:
for pdf_file in pdf_files:
@@ -250,9 +254,13 @@ def create_resource(request):
return redirect("resources:resource_detail", resource_id=resource.id)
except IntegrityError:
slot = form.cleaned_data["feature_slot"]
- messages.add_message(request, messages.ERROR, f"Feature slot {slot} is already "
- "in use. Quit this form and remove from existing "
- "resource.")
+ messages.add_message(
+ request,
+ messages.ERROR,
+ f"Feature slot {slot} is already "
+ "in use. Quit this form and remove from existing "
+ "resource.",
+ )
except Exception:
logger.exception("Error creating resource")
form.add_error(None, "An error occurred while creating the resource.")
@@ -293,9 +301,7 @@ def resource_detail(request, resource_id):
"resource_type": resource_obj.resource_type.name,
"main_resource_category": resource_obj.main_resource_category.name,
"additional_resource_category": (
- resource_obj.subcategories.name
- if resource_obj.subcategories
- else None
+ resource_obj.subcategories.name if resource_obj.subcategories else None
),
"age_range": resource_obj.age_range,
"curriculum": resource_obj.curriculum,
@@ -378,8 +384,8 @@ def add_resource_pdfs(request, pk):
"""
Adds PDF files to a resource in the system.
- This view handles the process of adding PDF files to an existing resource.
- It allows the user to upload one or more PDF files, which are then processed and associated with the resource.
+ This view handles the process of adding PDF files to an existing resource.
+ It allows the user to upload one or more PDF files, which are then processed and associated with the resource.
The view creates PDFResource and PDFPageSnapshot objects to represent the uploaded PDFs and their page snapshots, and uploads the files to S3 storage.
Args: