diff options
author | Matthew Lemon <y@yulqen.org> | 2024-12-23 17:09:51 +0000 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-12-23 17:09:51 +0000 |
commit | 44ee395693980cd33413a10287bb4189afbbc74d (patch) | |
tree | a092aac821b72f599d8d718ca12cd223f2a6a2a2 | |
parent | 867e09f694fce71dc8b3d7e086190507e47c3a0f (diff) |
testing llms ability to liberally add comments to a func
-rw-r--r-- | alphabetlearning/resources/views.py | 77 |
1 files changed, 50 insertions, 27 deletions
diff --git a/alphabetlearning/resources/views.py b/alphabetlearning/resources/views.py index 773b1c2..8b0f8c5 100644 --- a/alphabetlearning/resources/views.py +++ b/alphabetlearning/resources/views.py @@ -63,61 +63,84 @@ def create_featured(request): def _extract_metadata_from_resource(resource_obj) -> ResourceInfo | None: """ This function extracts the resource information from the model object and returns it as a - ResourceInfo object - :param resource_obj: - :return: + ResourceInfo object. + + :param resource_obj: An instance of the resource model from which metadata is extracted. + :return: ResourceInfo object containing the extracted metadata, or None if an error occurs. """ + + # Fetch and create a list of filenames for all PDF resources linked to the given resource object. pdf_resource_filenames = [ x.file_name for x in PDFResource.objects.filter(resource=resource_obj).all() ] + + # Retrieve all PDFResource objects associated with the resource_obj. pdf_resources = PDFResource.objects.filter(resource=resource_obj).all() + + # Initialize a dictionary to hold mappings of PDF filenames to their associated page snapshot filenames. snapshot_dict = {} + + # Iterate through each PDF resource to build the snapshot_dict mapping. for p in pdf_resources: + # For each PDF resource, populate the dictionary with its filename as the key + # and a list of its associated snapshot filenames as the value. snapshot_dict[p.file_name] = [ x.file_name for x in PDFPageSnapshot.objects.filter(pdf_file=p).all() ] + + # Initialize a dictionary to hold the URLs for the snapshots. snapshot_url_dict = {} - # Iterate through the snapshot dict and generate the URLs + + # Generate the URLs for each snapshot based on the snapshot_dict. for k, v in snapshot_dict.items(): snapshot_url_dict[k] = [ get_presigned_obj_url( - settings.AWS_STORAGE_BUCKET_NAME, - f"snapshotted_pages/{f}", + settings.AWS_STORAGE_BUCKET_NAME, # Specify the bucket name for S3 storage. + f"snapshotted_pages/{f}", # Build the path to the snapshot image in S3. ) - for f in v + for f in v # Iterate through the filenames in the snapshot list. ] + + # Generate pre-signed URLs for the PDF files collected. pdf_urls = [ get_presigned_obj_url(settings.AWS_STORAGE_BUCKET_NAME, f"pdfuploads/{f}") - for f in pdf_resource_filenames + for f in pdf_resource_filenames # Iterate through the PDF filenames. ] + + # Generate pre-signed URLs for the thumbnail images associated with the resource. thumbnail_urls = [ get_presigned_obj_url(settings.AWS_STORAGE_BUCKET_NAME, f"thumbnails/{f}") - for f in resource_obj.thumbnail_filenames + for f in resource_obj.thumbnail_filenames # Iterate through the thumbnail filenames. ] + try: + # Safely retrieve the subcategory name from resource_obj; handle the case where subcategories might be None. arc_name = resource_obj.subcategories.name if resource_obj.subcategories else None + + # Create and return a ResourceInfo object, populating it with various properties from resource_obj. return ResourceInfo( - id=resource_obj.id, - name=resource_obj.name, - description=resource_obj.description, - card_description=resource_obj.card_description, - main_resource_category_name=resource_obj.main_resource_category.name, - main_resource_category_colour_css_class=resource_obj.main_resource_category.colour_css_class, - main_resource_badge_foreground_colour=resource_obj.main_resource_category.badge_foreground_colour, - subcategories=arc_name, - age_range=resource_obj.age_range, - pdf_filenames=pdf_resource_filenames, - pdf_urls=pdf_urls, - snapshot_urls=snapshot_url_dict, - thumbnail_filenames=resource_obj.thumbnail_filenames, - thumbnail_urls=thumbnail_urls, - feature_slot=resource_obj.feature_slot, - created=resource_obj.created_at, - updated=resource_obj.updated_at, + id=resource_obj.id, # Resource ID + name=resource_obj.name, # Resource name + description=resource_obj.description, # Resource description + card_description=resource_obj.card_description, # Description for card visuals + main_resource_category_name=resource_obj.main_resource_category.name, # Main category name + main_resource_category_colour_css_class=resource_obj.main_resource_category.colour_css_class, # CSS class for category color + main_resource_badge_foreground_colour=resource_obj.main_resource_category.badge_foreground_colour, # Badge color for category + subcategories=arc_name, # Subcategory if it exists + age_range=resource_obj.age_range, # Target age range for the resource + pdf_filenames=pdf_resource_filenames, # List of PDF filenames + pdf_urls=pdf_urls, # List of URLs for PDFs + snapshot_urls=snapshot_url_dict, # Dictionary of snapshot filenames mapped to their URLs + thumbnail_filenames=resource_obj.thumbnail_filenames, # List of thumbnail filenames + thumbnail_urls=thumbnail_urls, # List of URLs for thumbnails + feature_slot=resource_obj.feature_slot, # Feature slot information + created=resource_obj.created_at, # Creation timestamp + updated=resource_obj.updated_at, # Last updated timestamp ) except Exception: + # Log an exception if there was an error during the extraction process. logging.exception("Error extracting resource information: ") - return None + return None # Return None in case of an error. @login_required |