From c3a934aa431d0e0b104f4f8da17b64337169f04a Mon Sep 17 00:00:00 2001 From: Ali Salman Date: Thu, 11 Jan 2024 10:21:06 +0500 Subject: [PATCH] fix: badgr upload issue --- lms/djangoapps/badges/backends/badgr.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lms/djangoapps/badges/backends/badgr.py b/lms/djangoapps/badges/backends/badgr.py index d25bdfe822b..d00e83b93be 100644 --- a/lms/djangoapps/badges/backends/badgr.py +++ b/lms/djangoapps/badges/backends/badgr.py @@ -11,6 +11,7 @@ import mimetypes import requests +from io import BytesIO from cryptography.fernet import Fernet from django.conf import settings from django.core.exceptions import ImproperlyConfigured @@ -104,6 +105,25 @@ def _log_if_raised(self, response, data): ) raise + def _get_byte_image(self, image): + """ + Get the byte representation of an image from the given image object. + This function takes an image object and retrieves its byte representation + Parameters: + image (Image): An image object representing the image to retrieve. + Returns: + BytesIO or None: A BytesIO object containing the byte representation + of the image if successful, or None if there was an error during the + retrieval process. + """ + try: + storage = image.storage + file_object = storage.open(image.file.name, mode='rb') + return BytesIO(file_object.file.read()) + except Exception as e: # pylint: disable=broad-except + LOGGER.error(f"Error opening an image: {e}") + return None + def _create_badge(self, badge_class): """ Create the badge class on Badgr. @@ -117,7 +137,7 @@ def _create_badge(self, badge_class): "Could not determine content-type of image! Make sure it is a properly named .png file. " "Filename was: {}".format(image.name) ) - with open(image.path, 'rb') as image_file: + if image_file := self._get_byte_image(image): files = {'image': (image.name, image_file, content_type)} data = { 'name': badge_class.display_name,