aboutsummaryrefslogtreecommitdiffstats
path: root/pyblackbird_cc/resources/views.py
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-05-26 20:47:03 +0100
committerMatthew Lemon <y@yulqen.org>2024-05-26 20:47:03 +0100
commitd421e56e0924487861e6cf5300f462c9e66fd376 (patch)
tree5340955b351772dfc9ec38c7a80035b5781cc93a /pyblackbird_cc/resources/views.py
parent610177be35ec791edae9577f104d0132f2a28364 (diff)
Correctly handles the unique constraint on feature_slot
Diffstat (limited to 'pyblackbird_cc/resources/views.py')
-rw-r--r--pyblackbird_cc/resources/views.py76
1 files changed, 42 insertions, 34 deletions
diff --git a/pyblackbird_cc/resources/views.py b/pyblackbird_cc/resources/views.py
index ea8953f..667fb0d 100644
--- a/pyblackbird_cc/resources/views.py
+++ b/pyblackbird_cc/resources/views.py
@@ -8,7 +8,9 @@ import boto3
from botocore.exceptions import ClientError
from botocore.utils import Path
from django.conf import settings
+from django.contrib import messages
from django.contrib.auth.decorators import login_required
+from django.db import IntegrityError
from django.db import transaction
from django.shortcuts import get_object_or_404
from django.shortcuts import redirect
@@ -216,6 +218,7 @@ def create_metadata(
yield metadata, snapshot_images
+#FIXME: Can't find a caller for this - delete it
@transaction.atomic
def create_resource_objects(resource, metadata_generator, thumbnail_files):
for metadata, snapshot_images in metadata_generator:
@@ -255,48 +258,53 @@ def create_resource(request):
feature_slot = form.cleaned_data["feature_slot"]
try:
- resource = Resource.objects.create(
- name=name,
- description=description,
- card_description=card_description,
- resource_type=resource_type,
- age_range=age_range,
- curriculum=curriculum,
- main_resource_category=main_resource_category,
- additional_resource_category=additional_resource_category,
- feature_slot=feature_slot,
- )
-
- metadata_generator = create_metadata(pdf_files)
- snapshotted_pages = []
-
- for metadata, snapshot_images in metadata_generator:
- pdf_resource = PDFResource.objects.create(
- resource=resource,
- file_name=os.path.basename(metadata.file_name),
- file_size=metadata.file_size,
+ with transaction.atomic():
+ resource = Resource.objects.create(
+ name=name,
+ description=description,
+ card_description=card_description,
+ resource_type=resource_type,
+ age_range=age_range,
+ curriculum=curriculum,
+ main_resource_category=main_resource_category,
+ additional_resource_category=additional_resource_category,
+ feature_slot=feature_slot,
)
- for snapshot_image in snapshot_images:
- PDFPageSnapshot.objects.create(
- name="test",
- file_name=os.path.basename(snapshot_image),
- pdf_file=pdf_resource,
+ metadata_generator = create_metadata(pdf_files)
+ snapshotted_pages = []
+
+ for metadata, snapshot_images in metadata_generator:
+ pdf_resource = PDFResource.objects.create(
+ resource=resource,
+ file_name=os.path.basename(metadata.file_name),
+ file_size=metadata.file_size,
)
- snapshotted_pages.append(snapshot_images)
+ 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)
- resource.thumbnail_filenames = [f.name for f in thumbnail_files]
- resource.save()
+ resource.thumbnail_filenames = [f.name for f in thumbnail_files]
+ resource.save()
- # Reset the file pointers for pdf_files
- for pdf_file in pdf_files:
- pdf_file.seek(0)
+ # Reset the file pointers for pdf_files
+ for pdf_file in pdf_files:
+ pdf_file.seek(0)
- if not upload_to_s3(pdf_files, thumbnail_files, snapshotted_pages):
- raise Exception("Error uploading files to S3")
+ if not upload_to_s3(pdf_files, thumbnail_files, snapshotted_pages):
+ raise Exception("Error uploading files to S3")
- return redirect("resources:resource_detail", resource_id=resource.id)
+ 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.")
except Exception:
logger.exception("Error creating resource")
form.add_error(None, "An error occurred while creating the resource.")