Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: badgr upload issue #489

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading