Skip to content

Commit

Permalink
fix: badgr upload issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali Salman authored and Ali Salman committed Jan 11, 2024
1 parent 1066897 commit c3a934a
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lms/djangoapps/badges/backends/badgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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,
Expand Down

0 comments on commit c3a934a

Please sign in to comment.