Skip to content

Commit

Permalink
Merge pull request #489 from edly-io/alisalman/fix-badgr-upload
Browse files Browse the repository at this point in the history
fix: badgr upload issue
  • Loading branch information
Ali-Salman29 authored Jan 11, 2024
2 parents 1066897 + c3a934a commit 4f64833
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 4f64833

Please sign in to comment.